1Panel/backend/utils/compose/compose.go
2022-12-07 18:18:45 +08:00

66 lines
1.6 KiB
Go

package compose
import (
"os/exec"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
)
func Up(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "up", "-d")
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
func Down(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "down")
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
func Stop(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "stop")
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
func Restart(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "restart")
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
func Operate(filePath, operation string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, operation)
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
func Rmf(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "rm", "-f")
stdout, err := cmd.CombinedOutput()
return string(stdout), err
}
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
}