2021-11-20 10:34:31 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"html/template"
|
2021-11-28 00:04:20 +00:00
|
|
|
"ledo/app/helper"
|
|
|
|
"ledo/app/modules/compose"
|
2021-11-20 10:34:31 +00:00
|
|
|
"ledo/app/modules/context"
|
2021-11-28 00:04:20 +00:00
|
|
|
"ledo/app/modules/dockerfile"
|
2021-11-20 10:34:31 +00:00
|
|
|
"ledo/app/modules/interact"
|
|
|
|
"ledo/app/templates"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var CmdInit = cli.Command{
|
|
|
|
Name: "init",
|
|
|
|
Category: catSetup,
|
|
|
|
Usage: "Init ledo in project",
|
|
|
|
Description: `Initialize LeadDocker in current project`,
|
|
|
|
Action: runInitLedo,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInitLedo(cmd *cli.Context) error {
|
2021-11-28 00:04:20 +00:00
|
|
|
var advRun bool
|
|
|
|
config, err := context.LoadConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-20 10:34:31 +00:00
|
|
|
data, err := interact.InitLedoProject(config.Docker)
|
2021-11-28 00:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-20 10:34:31 +00:00
|
|
|
tpl, err := template.New("tpl").Parse(templates.LedoConfigurationFileTemplate)
|
2021-11-28 00:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-20 10:34:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-11-28 00:04:20 +00:00
|
|
|
f, err := os.Create("./.ledo.yml")
|
2021-11-20 10:34:31 +00:00
|
|
|
if err != nil {
|
2021-11-28 00:04:20 +00:00
|
|
|
return err
|
2021-11-20 10:34:31 +00:00
|
|
|
}
|
2021-11-28 00:04:20 +00:00
|
|
|
err = tpl.Execute(f, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
advRun = false
|
|
|
|
// advRun := interact.InitAdvancedConfigurationAsk("Run advanced docker mode configuration?")
|
|
|
|
if advRun == true {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
dConf, _ := interact.InitDocker()
|
|
|
|
err = dockerfile.CreateDockerFile(dConf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectComposeConfig := helper.DockerProjectCfg{}
|
|
|
|
projectComposeConfig.DockerBaseImage = dConf.DockerBaseImage
|
|
|
|
|
|
|
|
var dockerComposeServices []helper.DockerProjectAdditionalServiceCfg
|
|
|
|
var dockerComposeModeConfig []helper.DockerComposeModeCfg
|
|
|
|
|
|
|
|
|
|
|
|
for _, composeMode := range interact.PredefinedDockerComposeModes {
|
|
|
|
var configMode bool
|
2021-11-20 10:34:31 +00:00
|
|
|
|
2021-11-28 00:04:20 +00:00
|
|
|
if composeMode == "base" {
|
|
|
|
configMode = true
|
|
|
|
} else {
|
|
|
|
configMode = interact.InitAdvancedConfigurationAsk("Configure "+composeMode+" stack?")
|
|
|
|
}
|
2021-11-20 10:34:31 +00:00
|
|
|
|
2021-11-28 00:04:20 +00:00
|
|
|
if configMode == true {
|
|
|
|
for {
|
|
|
|
serviceCfg, _ := interact.CreateDockerService()
|
|
|
|
serviceCfg.DockerServiceMode = composeMode
|
|
|
|
dockerComposeServices = append(dockerComposeServices, serviceCfg)
|
|
|
|
addAnother := interact.InitAdvancedConfigurationAsk("Add another service do " + composeMode + " stack?")
|
|
|
|
if addAnother == false {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
composeFilename := "./docker/docker-compose.yml"
|
|
|
|
if composeMode != "base" {
|
|
|
|
composeFilename = "./docker/docker-compose."+composeMode+".yml"
|
|
|
|
}
|
|
|
|
mdCfg := helper.DockerComposeModeCfg{
|
|
|
|
DockerComposeName: composeFilename,
|
|
|
|
DockerComposeServices: dockerComposeServices,
|
|
|
|
}
|
|
|
|
dockerComposeModeConfig = append(dockerComposeModeConfig, mdCfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
projectComposeConfig.DockerComposeModes = dockerComposeModeConfig
|
|
|
|
|
|
|
|
err = compose.CreateComposeFile(ctx, projectComposeConfig, "base")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|