ledo/app/modules/context/context.go

92 lines
1.9 KiB
Go
Raw Normal View History

2021-10-21 00:51:32 +00:00
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
2021-11-28 00:04:20 +00:00
Config *config.LedoFile
2021-10-21 00:51:32 +00:00
ComposeArgs []string
2021-11-28 00:04:20 +00:00
Mode mode.Mode
Output string
2021-10-21 00:51:32 +00:00
}
func InitCommand(ctx *cli.Context) *LedoContext {
var (
2021-11-28 00:04:20 +00:00
c LedoContext
2021-10-21 00:51:32 +00:00
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"
}
2021-11-20 10:34:31 +00:00
if _, err := os.Stat(configYml); err != nil {
fmt.Printf("Config file not found. Please run ledo init\n")
os.Exit(1)
}
2021-11-28 00:04:20 +00:00
ledoMode := mode.InitMode(modeYml, configYml)
c.Mode = ledoMode
2021-10-21 00:51:32 +00:00
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)))
2021-11-28 00:04:20 +00:00
composes, _ := ledoMode.GetModeConfig()
2021-10-21 00:51:32 +00:00
for _, element := range composes {
args = append(args, "-f")
2021-11-28 00:04:20 +00:00
args = append(args, element)
2021-10-21 00:51:32 +00:00
}
c.ComposeArgs = args
return &c
}
2021-11-20 10:34:31 +00:00
func LoadConfigFile() (*config.LedoFile, error) {
configYml := ".ledo.yml"
if _, err := os.Stat(configYml); err != nil {
nilCfg := &config.LedoFile{}
2021-11-28 00:04:20 +00:00
return nilCfg, err
2021-11-20 10:34:31 +00:00
}
cfg, _ := config.NewLedoFile(configYml)
return cfg, nil
}
2021-10-21 00:51:32 +00:00
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()
2021-11-28 00:04:20 +00:00
}
2021-11-30 16:58:09 +00:00
func (lx *LedoContext) ExecCmdSilent(cmd string, cmdArgs []string) error {
command := exec.Command(cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}