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

Commit

Permalink
write config file only once - add message and error buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 17, 2020
1 parent 75d20c5 commit 9d1e28f
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 58 deletions.
7 changes: 4 additions & 3 deletions internal/commands/configcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/martinnirtl/dockma/internal/survey"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

var setCmd = &cobra.Command{
Expand All @@ -30,10 +31,10 @@ var setCmd = &cobra.Command{
varname := strings.Split(varnameRaw, ":")

setConfigVar(varname[0])
}

if len(selected) > 0 {
config.Save()
message := fmt.Sprintf("Set %s%s%s: %s%s%s", chalk.Cyan, varname[0], chalk.ResetColor, chalk.Cyan, viper.GetString(varname[0]), chalk.ResetColor)
err := fmt.Errorf("Failed to set '%s'", varname[0])
config.Save(message, err)
}
},
}
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/downcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var DownCommand = &cobra.Command{
os.Exit(0)
}

viper.Set(fmt.Sprintf("envs.%s.running", activeEnv), false)
config.Save("", fmt.Errorf("Failed to set running to 'false' [%s]", activeEnv))

utils.Success("Successfully executed 'docker-compose down'")
},
}
18 changes: 13 additions & 5 deletions internal/commands/envcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -61,16 +62,23 @@ var initCmd = &cobra.Command{

viper.Set(fmt.Sprintf("envs.%s.home", env), workingDir)
viper.Set(fmt.Sprintf("envs.%s.pull", env), pull)
viper.Set(fmt.Sprintf("envs.%s.running", env), false)

oldEnv := viper.GetString("active")
activeEnv := config.GetActiveEnv()
oldEnv := activeEnv.GetName()

viper.Set("active", env)
var set bool
if activeEnv.IsRunning() {
message := fmt.Sprintf("Current active environment running: %s. Set newly initialized environment active", oldEnv)

if err := viper.WriteConfig(); err != nil {
utils.ErrorAndExit(fmt.Errorf("Initializing environment failed: %s", env))
set = survey.Confirm(message, false)
}

fmt.Printf("%sSet active environment: %s%s(old: %s)\n", chalk.Cyan, env, chalk.ResetColor, oldEnv)
if set {
viper.Set("active", env)
}

config.Save(fmt.Sprintf("Initialized new environment: %s%s%s\n", chalk.Cyan, env, chalk.ResetColor), fmt.Errorf("Failed to save newly created environment"))
},
}

Expand Down
22 changes: 16 additions & 6 deletions internal/commands/envcmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/martinnirtl/dockma/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

Expand All @@ -19,14 +18,25 @@ var listCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
envs := config.GetEnvNames()

activeEnv := viper.GetString("active")
activeEnv := config.GetActiveEnv()
activeEnvName := activeEnv.GetName()

if len(envs) > 0 {
for _, env := range envs {
if env == activeEnv {
fmt.Printf("%s%s [active]%s\n", chalk.Cyan, env, chalk.ResetColor)
for _, envName := range envs {
if envName == activeEnvName {
fmt.Printf("%s%s [active]%s", chalk.Cyan, envName, chalk.ResetColor)
} else {
fmt.Println(env)
fmt.Print(envName)
}

if env, err := config.GetEnv(envName); err != nil {

} else {
if env.IsRunning() {
fmt.Printf("%s%s%s\n", chalk.Green, " running", chalk.ResetColor)
} else {
fmt.Println()
}
}
}
} else {
Expand Down
15 changes: 8 additions & 7 deletions internal/commands/envcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package envcmd
import (
"fmt"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/internal/utils/helpers"
"github.com/spf13/cobra"
Expand All @@ -24,21 +25,21 @@ var setCmd = &cobra.Command{
env = helpers.GetEnvironment(args[0])
}

activeEnv := viper.GetString("active")
activeEnv := config.GetActiveEnv()

if env == activeEnv {
fmt.Printf("%sActive environment already set: %s%s\n", chalk.Yellow, activeEnv, chalk.ResetColor)
if env == activeEnv.GetName() {
fmt.Printf("%sEnvironment already set as active: %s%s\n", chalk.Yellow, activeEnv.GetName(), chalk.ResetColor)

return
}

fmt.Printf("%sNew active environment: %s%s (old: %s)\n", chalk.Green, env, chalk.ResetColor, activeEnv)
if activeEnv.IsRunning() {
utils.Warn(fmt.Sprintf("Switching from running environment."))
}

viper.Set("active", env)

if err := viper.WriteConfig(); err != nil {
utils.ErrorAndExit(fmt.Errorf("Setting active environment failed: %s", env))
}
config.Save(fmt.Sprintf("New active environment: %s%s%s (old: %s)\n", chalk.Green, env, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to set active environment"))
},
}

Expand Down
5 changes: 3 additions & 2 deletions internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"os/user"
"path"
"strings"
"time"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +49,8 @@ func initCommandHandler(cmd *cobra.Command, args []string) {
username = sysUser.Username
}

username = survey.InputName("What is your name", strings.Title(username))
username = survey.InputName("What is your name", username)
username = survey.Select("Select primary color", config.PrimaryColors)

viper.Set("username", username)
viper.Set("init", time.Now())
Expand Down
5 changes: 1 addition & 4 deletions internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ var createCmd = &cobra.Command{

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

err = config.SaveNow()
utils.ErrorAndExit(err)

utils.Success(fmt.Sprintf("Successfully saved profile: %s [%s]", profileName, activeEnv.GetName()))
config.Save(fmt.Sprintf("Saved profile: %s%s%s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to save profile '%s'", profileName))
},
}

Expand Down
6 changes: 1 addition & 5 deletions internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand Down Expand Up @@ -37,10 +36,7 @@ var deleteCmd = &cobra.Command{

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

err := config.SaveNow()
utils.ErrorAndExit(err)

utils.Success(fmt.Sprintf("Successfully deleted profile: %s [%s]", profileName, activeEnv.GetName()))
config.Save(fmt.Sprintf("Deleted profile: %s%s%s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to delete profile '%s'", profileName))
},
}

Expand Down
5 changes: 1 addition & 4 deletions internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ var renameCmd = &cobra.Command{

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

err := config.SaveNow()
utils.ErrorAndExit(err)

utils.Success(fmt.Sprintf("Successfully renamed profile from %s to %s [%s]", renameProfile, profileName, activeEnv.GetName()))
config.Save(fmt.Sprintf("Renamed profile from %s to %s%s%s [%s]", renameProfile, chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to rename profile '%s'", renameProfile))
},
}

Expand Down
5 changes: 1 addition & 4 deletions internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ var updateCmd = &cobra.Command{

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

err = config.SaveNow()
utils.ErrorAndExit(err)

utils.Success(fmt.Sprintf("Successfully updated profile: %s [%s]", profileName, activeEnv.GetName()))
config.Save(fmt.Sprintf("Updated profile: %s [%s]", chalk.Cyan, profileName, chalk.ResetColor, activeEnv.GetName()), fmt.Errorf("Failed to update profile '%s'", profileName))
},
}

Expand Down
9 changes: 8 additions & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/martinnirtl/dockma/internal/commands/versioncmd"
"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/internal/utils/helpers"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -104,8 +105,14 @@ func rootPreRunHook(cmd *cobra.Command, args []string) {

func rootPostRunHook(cmd *cobra.Command, args []string) {
if config.SaveConfig {
err := config.SaveNow()
messages, errors, err := config.SaveNow()
utils.Error(err)

if err != nil {
helpers.PrintErrorList(errors)
} else {
helpers.PrintMessageList(messages)
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var UpCommand = &cobra.Command{
viper.Set(fmt.Sprintf("envs.%s.latest", activeEnv.GetName()), selectedServices)
}

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

err = envvars.SetEnvVars(services.All, selectedServices)
Expand All @@ -144,6 +144,9 @@ var UpCommand = &cobra.Command{
os.Exit(0)
}

viper.Set(fmt.Sprintf("envs.%s.running", activeEnv.GetName()), true)
config.Save("", fmt.Errorf("Failed to set running to 'true' [%s]", activeEnv.GetName()))

utils.Success("Successfully executed 'docker-compose up'")
},
}
Loading

0 comments on commit 9d1e28f

Please sign in to comment.