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

Commit

Permalink
implement global saveConfig flag
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 16, 2020
1 parent 20d0677 commit ac08947
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 27 deletions.
13 changes: 5 additions & 8 deletions internal/commands/configcmd/set.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"
)
Expand All @@ -31,14 +30,18 @@ var setCmd = &cobra.Command{

setConfigVar(varname[0])
}

if len(selected) > 0 {
config.Save()
}
},
}

func init() {
ConfigCommand.AddCommand(setCmd)
}

func setConfigVar(varname string) error {
func setConfigVar(varname string) {
switch varname {
case "hidesubcommandoutput":
hide := survey.Confirm("Hide output of external commands [true: show output only on error, false: always pipe output]", viper.GetBool("hidesubcommandoutput"))
Expand All @@ -55,10 +58,4 @@ func setConfigVar(varname string) error {

viper.Set("username", username)
}

err := config.Save()

utils.ErrorAndExit(err)

return nil
}
3 changes: 1 addition & 2 deletions internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ var createCmd = &cobra.Command{

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

err = config.Save()

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

utils.Success(fmt.Sprintf("Successfully saved profile: %s [%s]", profileName, activeEnv.GetName()))
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ var deleteCmd = &cobra.Command{

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

err := config.Save()

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

utils.Success(fmt.Sprintf("Successfully deleted profile: %s [%s]", profileName, activeEnv.GetName()))
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ var renameCmd = &cobra.Command{

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

err := config.Save()

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

utils.Success(fmt.Sprintf("Successfully renamed profile from %s to %s [%s]", renameProfile, profileName, activeEnv.GetName()))
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ var updateCmd = &cobra.Command{

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

err = config.Save()

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

utils.Success(fmt.Sprintf("Successfully updated profile: %s [%s]", profileName, activeEnv.GetName()))
Expand Down
17 changes: 13 additions & 4 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/martinnirtl/dockma/internal/commands/pscmd"
"github.com/martinnirtl/dockma/internal/commands/upcmd"
"github.com/martinnirtl/dockma/internal/commands/versioncmd"
"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand All @@ -26,10 +27,11 @@ import (

// RootCommand is the root command of dockma
var RootCommand = &cobra.Command{
Use: "dockma",
Short: "Dockma is bringing your docker-compose game to the next level.",
Long: `A fast and flexible CLI tool to boost your productivity during development with docker containers built with Go. Full documentation is available at https://dockma.dev`,
PersistentPreRun: rootPreRunHook,
Use: "dockma",
Short: "Dockma is bringing your docker-compose game to the next level.",
Long: `A fast and flexible CLI tool to boost your productivity during development with docker containers built with Go. Full documentation is available at https://dockma.dev`,
PersistentPreRun: rootPreRunHook,
PersistentPostRun: rootPostRunHook,
}

func init() {
Expand Down Expand Up @@ -99,6 +101,13 @@ func rootPreRunHook(cmd *cobra.Command, args []string) {
}
}

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

// Execute starts cobra command execution
func Execute() {
RootCommand.Execute()
Expand Down
4 changes: 1 addition & 3 deletions internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ var UpCommand = &cobra.Command{
viper.Set(fmt.Sprintf("envs.%s.latest", activeEnv.GetName()), selectedServices)
}

err = config.Save()

utils.ErrorAndExit(err)
config.Save()
}

err = envvars.SetEnvVars(services.All, selectedServices)
Expand Down
19 changes: 15 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (

// NOTE viper gets initialized in commands/root.go.

// SaveConfig indicates whether config should be saved or not
var SaveConfig bool

type env struct {
name string
}

// Env provides an interface for easier access to more complex environment config
type Env interface {
GetName() string
GetHomeDir() string
Expand All @@ -29,12 +33,19 @@ type Env interface {
GetLatest() (Profile, error)
}

// Save saves the config.
func Save() error {
// Save sets the config to be saved at end of command execution. Use SaveNow func to save config immediately.
func Save() {
SaveConfig = true
}

// SaveNow saves the config immediately.
func SaveNow() error {
SaveConfig = false

err := viper.WriteConfig()

if err != nil {
return errors.New("Could not save changes to dockma config")
return errors.New("Could not save changes")
}

return nil
Expand Down Expand Up @@ -71,7 +82,7 @@ func GetLogfile() string {
return GetFile(filename)
}

// GetEnvs returns configured envs.
// GetEnvNames returns configured envs.
func GetEnvNames() (envs []string) {
envsMap := viper.GetStringMap("envs")

Expand Down

0 comments on commit ac08947

Please sign in to comment.