2023-10-16 15:48:06 +08:00
|
|
|
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 {
|
2024-06-04 16:48:31 +08:00
|
|
|
Extract(filePath, dstDir string, secret string) error
|
|
|
|
Compress(sourcePaths []string, dstFile string, secret string) error
|
2023-10-16 15:48:06 +08:00
|
|
|
}
|
|
|
|
|
2023-10-16 16:34:15 +08:00
|
|
|
func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
|
2023-10-16 15:48:06 +08:00
|
|
|
switch compressType {
|
|
|
|
case Tar:
|
|
|
|
if err := checkCmdAvailability("tar"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewTarArchiver(compressType), nil
|
2024-06-04 16:48:31 +08:00
|
|
|
case TarGz:
|
|
|
|
return NewTarGzArchiver(), nil
|
2023-10-16 16:34:15 +08:00
|
|
|
case Zip:
|
|
|
|
if err := checkCmdAvailability("zip"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewZipArchiver(), nil
|
2023-10-16 15:48:06 +08:00
|
|
|
default:
|
|
|
|
return nil, buserr.New("unsupported compress type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCmdAvailability(cmdStr string) error {
|
|
|
|
if cmd.Which(cmdStr) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-08 10:33:29 +08:00
|
|
|
return buserr.WithName(constant.ErrCmdNotFound, cmdStr)
|
2023-10-16 15:48:06 +08:00
|
|
|
}
|