ledo/app/modules/context/context.go

66 lines
1.3 KiB
Go

package context
import (
"fmt"
"github.com/urfave/cli/v2"
"ledo/app/modules/config"
"ledo/app/modules/mode"
"os"
"os/exec"
"strings"
)
type LedoContext struct {
*cli.Context
Config *config.LedoFile
ComposeArgs []string
Mode mode.Mode
Output string
}
func InitCommand(ctx *cli.Context) *LedoContext {
var (
c LedoContext
cfg *config.LedoFile
)
configYml := ".ledo.yml"
modeYml := ".ledo-mode"
// Compat with jzcli (StreamSage and Jazzy deployment tool)
if _, err := os.Stat(".jz-project.yml"); err == nil {
configYml = ".jz-project.yml"
modeYml = ".jz-mode"
}
mode := mode.InitMode(modeYml, configYml)
c.Mode = mode
c.Output = ctx.String("output")
cfg, _ = config.NewLedoFile(configYml)
c.Config = cfg
args := []string{"--env-file", ".env"}
args = append(args, "--project-name", strings.ToLower(strings.Replace(c.Config.Docker.Name, "/", "-", -1)))
composes, _ := mode.GetModeConfig()
for _, element := range composes {
args = append(args, "-f")
args = append(args, element)
}
c.ComposeArgs = args
return &c
}
func (lx *LedoContext) ExecCmd(cmd string, cmdArgs []string) error {
fmt.Printf("Execute: %v %v\n", cmd, strings.Join(cmdArgs, " "))
command := exec.Command(cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}