mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-16 12:39:15 +08:00
c880caa770
Refs https://github.com/1Panel-dev/1Panel/issues/1245 Refs https://github.com/1Panel-dev/1Panel/issues/263
36 lines
866 B
Go
36 lines
866 B
Go
package files
|
|
|
|
import (
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
)
|
|
|
|
type ShellArchiver interface {
|
|
Extract(filePath, dstDir string) error
|
|
Compress(sourcePaths []string, dstFile string) error
|
|
}
|
|
|
|
func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
|
|
switch compressType {
|
|
case Tar:
|
|
if err := checkCmdAvailability("tar"); err != nil {
|
|
return nil, err
|
|
}
|
|
return NewTarArchiver(compressType), nil
|
|
case Zip:
|
|
if err := checkCmdAvailability("zip"); err != nil {
|
|
return nil, err
|
|
}
|
|
return NewZipArchiver(), nil
|
|
default:
|
|
return nil, buserr.New("unsupported compress type")
|
|
}
|
|
}
|
|
|
|
func checkCmdAvailability(cmdStr string) error {
|
|
if cmd.Which(cmdStr) {
|
|
return nil
|
|
}
|
|
return buserr.WithName(cmdStr, constant.ErrCmdNotFound)
|
|
}
|