1Panel/backend/utils/compose/compose.go

59 lines
1.5 KiB
Go
Raw Normal View History

package compose
import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
)
func Up(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
return string(stdout), err
}
func Down(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s down", filePath)
2022-12-06 15:59:25 +08:00
return string(stdout), err
}
func Stop(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
return string(stdout), err
}
func Restart(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s restart", filePath)
return string(stdout), err
}
2022-09-26 22:54:38 +08:00
2022-12-06 15:05:13 +08:00
func Operate(filePath, operation string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s %s", filePath, operation)
2022-12-06 15:05:13 +08:00
return string(stdout), err
}
2022-09-26 22:54:38 +08:00
func Rmf(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s rm -f", filePath)
return string(stdout), err
2022-09-26 22:54:38 +08:00
}
func GetComposeProject(yml []byte, env map[string]string) (*types.Project, error) {
var configFiles []types.ConfigFile
configFiles = append(configFiles, types.ConfigFile{
Filename: "docker-compose.yml",
Content: yml},
)
details := types.ConfigDetails{
WorkingDir: "",
ConfigFiles: configFiles,
Environment: env,
}
project, err := loader.Load(details, func(options *loader.Options) {
})
if err != nil {
return nil, err
}
return project, nil
}