Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
add latest and skip flag
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Mar 4, 2020
1 parent 16bab12 commit d95ab02
Showing 1 changed file with 79 additions and 50 deletions.
129 changes: 79 additions & 50 deletions internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package upcmd

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -20,10 +21,13 @@ import (
"github.com/ttacon/chalk"
)

var latestFlag bool
var skipFlag bool

// GetUpCommand returns the top level up command
func GetUpCommand() *cobra.Command {
return &cobra.Command{
Use: "up",
upCommand := &cobra.Command{
Use: "up [profile]",
Aliases: []string{"run"},
Short: "Runs active environment with profile or service selection",
Long: "Runs active environment with profile or service selection",
Expand All @@ -32,48 +36,19 @@ func GetUpCommand() *cobra.Command {
PreRun: hooks.RequiresActiveEnv,
Run: runUpCommand,
}

upCommand.Flags().BoolVarP(&latestFlag, "latest", "l", false, "use latest service selection")
upCommand.Flags().BoolVarP(&skipFlag, "select", "s", false, "select services")

return upCommand
}

func runUpCommand(cmd *cobra.Command, args []string) {
filepath := config.GetSubcommandLogfile()
activeEnv := config.GetActiveEnv()
envHomeDir := activeEnv.GetHomeDir()

pull := activeEnv.GetPullSetting()

var doPull bool
switch pull {
case "auto":
doPull = true
case "optional":
doPull = survey.Confirm("Pull changes from git", true)
case "manual":
timePassed, err := activeEnv.LastUpdate()

if err != nil {
doPull = survey.Confirm(fmt.Sprintf("Environment %s not yet updated (%s). Pull now", chalk.Cyan.Color(activeEnv.GetName()), chalk.Cyan.Color("dockma env pull")), true)
} else if timePassed.Hours() > 24*7 {
doPull = survey.Confirm(fmt.Sprintf("Some time has passed since last %s. Pull now", chalk.Cyan.Color("git pull")), true)
}
case "off":
doPull = false
default:
doPull = false
}

hideCmdOutput := config.GetHideSubcommandOutputSetting()

if doPull {
output, err := envcmd.Pull(envHomeDir, hideCmdOutput, false)
if err != nil && hideCmdOutput {
fmt.Print(string(output))
utils.Warn(fmt.Sprintf("Could not execute command %s.", chalk.Underline.TextStyle("git pull")))

fmt.Println() // Add empty line for better readability
} else {
utils.Success(fmt.Sprintf("Pulled environment: %s", activeEnv.GetName()))
}
}
pull(activeEnv)

services, err := dockercompose.GetServices(envHomeDir)
utils.ErrorAndExit(err)
Expand All @@ -84,61 +59,76 @@ func runUpCommand(cmd *cobra.Command, args []string) {

profileNames := activeEnv.GetProfileNames()

// default
profileName := "$latest"
var preselected []string

// default
profileName := "latest"
if len(args) > 0 {
if latestFlag {
profile, err := activeEnv.GetLatest()
utils.ErrorAndExit(err)

preselected = profile.Selected
} else if len(args) > 0 {
profileName = args[0]

profile, err := activeEnv.GetProfile(profileName)
utils.ErrorAndExit(err)

preselected = profile.Selected
} else if len(profileNames) > 0 {
profileNames = append(profileNames, "latest")
profileNames = append(profileNames, "$custom")
profileNames = append(profileNames, "$latest")

profileName = survey.Select(fmt.Sprintf("Select profile to run"), profileNames)

if profileName != "latest" {
profile, err := activeEnv.GetProfile(profileName)
if profileName == "$custom" {
preselected = services.All
} else if profileName == "$latest" {
profile, err := activeEnv.GetLatest()
utils.ErrorAndExit(err)

preselected = profile.Selected
} else {
profile, err := activeEnv.GetLatest()
profile, err := activeEnv.GetProfile(profileName)
utils.ErrorAndExit(err)

preselected = profile.Selected
}
}

if len(preselected) == 0 {
preselected = services.All
}
// if len(preselected) == 0 {
// preselected = services.All
// }

selectedServices := survey.MultiSelect("Select services to start", services.All, preselected)
var selectedServices []string
if skipFlag {
selectedServices = preselected
} else {
selectedServices = survey.MultiSelect("Select services to start", services.All, preselected)
}

if profileName == "latest" {
if profileName == "$custom" || (profileName == "$latest" && !skipFlag) {
saveProfile := survey.Confirm("Save as profile", false)

if saveProfile {
profileName = survey.InputName("Enter profile name", "")

viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv.GetName(), profileName), selectedServices)

config.Save(fmt.Sprintf("Saved profile: %s", chalk.Cyan.Color(profileName)), fmt.Errorf("Failed to save profile: %s", profileName))
}
}

viper.Set(fmt.Sprintf("envs.%s.latest", activeEnv.GetName()), selectedServices)
config.Save("", errors.New("Failed to save latest selection"))

err = envvars.SetEnvVars("", services.All, selectedServices)
utils.ErrorAndExit(err)

err = os.Chdir(envHomeDir)
utils.ErrorAndExit(err)

hideCmdOutput := config.GetHideSubcommandOutputSetting()

var timebridger externalcommand.Timebridger
if hideCmdOutput {
timebridger = spinnertimebridger.Default(fmt.Sprintf("Running %s", chalk.Cyan.Color("docker-compose up")))
Expand All @@ -157,3 +147,42 @@ func runUpCommand(cmd *cobra.Command, args []string) {

utils.Success("Executed command: docker-compose up")
}

func pull(activeEnv config.Env) {
envHomeDir := activeEnv.GetHomeDir()
pull := activeEnv.GetPullSetting()

var doPull bool
switch pull {
case "auto":
doPull = true
case "optional":
doPull = survey.Confirm("Pull changes from git", true)
case "manual":
timePassed, err := activeEnv.LastUpdate()

if err != nil {
doPull = survey.Confirm(fmt.Sprintf("Environment %s not yet updated (%s). Pull now", chalk.Cyan.Color(activeEnv.GetName()), chalk.Cyan.Color("dockma env pull")), true)
} else if timePassed.Hours() > 24*7 {
doPull = survey.Confirm(fmt.Sprintf("Some time has passed since last %s. Pull now", chalk.Cyan.Color("git pull")), true)
}
case "off":
doPull = false
default:
doPull = false
}

hideCmdOutput := config.GetHideSubcommandOutputSetting()

if doPull {
output, err := envcmd.Pull(envHomeDir, hideCmdOutput, false)
if err != nil && hideCmdOutput {
fmt.Print(string(output))
utils.Warn(fmt.Sprintf("Could not execute command %s.", chalk.Underline.TextStyle("git pull")))

fmt.Println() // Add empty line for better readability
} else {
utils.Success(fmt.Sprintf("Pulled environment: %s", activeEnv.GetName()))
}
}
}

0 comments on commit d95ab02

Please sign in to comment.