2022-09-22 16:16:04 +08:00
|
|
|
package compose
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
import (
|
2022-12-06 15:05:13 +08:00
|
|
|
"os/exec"
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
"github.com/compose-spec/compose-go/loader"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
)
|
2022-09-22 16:16:04 +08:00
|
|
|
|
|
|
|
func Up(filePath string) (string, error) {
|
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, "up", "-d")
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Down(filePath string) (string, error) {
|
2022-12-06 15:59:25 +08:00
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, "down")
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
return string(stdout), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop(filePath string) (string, error) {
|
2022-12-04 12:59:09 +08:00
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, "stop")
|
2022-09-22 16:16:04 +08:00
|
|
|
stdout, err := cmd.CombinedOutput()
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Restart(filePath string) (string, error) {
|
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, "restart")
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-26 22:54:38 +08:00
|
|
|
|
2022-12-06 15:05:13 +08:00
|
|
|
func Operate(filePath, operation string) (string, error) {
|
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, operation)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
return string(stdout), err
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:54:38 +08:00
|
|
|
func Rmf(filePath string) (string, error) {
|
|
|
|
cmd := exec.Command("docker-compose", "-f", filePath, "rm", "-f")
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-26 22:54:38 +08:00
|
|
|
}
|
2022-10-11 16:27:58 +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
|
|
|
|
}
|