From c2595d464d81a29ebf2e1cf41786c1f05295980c Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 25 Aug 2024 10:06:24 +0100 Subject: [PATCH] config: override --save-config default with COLIMA_SAVE_CONFIG env var Signed-off-by: Abiola Ibrahim --- cmd/start.go | 9 ++++++++- util/osutil/os.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cmd/start.go b/cmd/start.go index dc85f259..0c2d3d9f 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -20,6 +20,7 @@ import ( "github.com/abiosoft/colima/environment/container/docker" "github.com/abiosoft/colima/environment/container/kubernetes" "github.com/abiosoft/colima/util" + "github.com/abiosoft/colima/util/osutil" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -118,6 +119,7 @@ const ( ) var defaultK3sArgs = []string{"--disable=traefik"} +var envSaveConfig = osutil.EnvVar("COLIMA_SAVE_CONFIG") var startCmdArgs struct { config.Config @@ -142,6 +144,11 @@ func init() { mounts := strings.Join([]string{defaultMountTypeQEMU, "9p", "virtiofs"}, ", ") types := strings.Join([]string{defaultVMType, "vz"}, ", ") + saveConfigDefault := true + if envSaveConfig.Exists() { + saveConfigDefault = envSaveConfig.Bool() + } + root.Cmd().AddCommand(startCmd) startCmd.Flags().StringVarP(&startCmdArgs.Runtime, "runtime", "r", docker.Name, "container runtime ("+runtimes+")") startCmd.Flags().BoolVar(&startCmdArgs.Flags.ActivateRuntime, "activate", true, "set as active Docker/Kubernetes context on startup") @@ -178,7 +185,7 @@ func init() { // config startCmd.Flags().BoolVarP(&startCmdArgs.Flags.Edit, "edit", "e", false, "edit the configuration file before starting") startCmd.Flags().StringVar(&startCmdArgs.Flags.Editor, "editor", "", `editor to use for edit e.g. vim, nano, code (default "$EDITOR" env var)`) - startCmd.Flags().BoolVar(&startCmdArgs.Flags.SaveConfig, "save-config", true, "persist and overwrite config file with (newly) specified flags") + startCmd.Flags().BoolVar(&startCmdArgs.Flags.SaveConfig, "save-config", saveConfigDefault, "persist and overwrite config file with (newly) specified flags") // mounts startCmd.Flags().StringSliceVarP(&startCmdArgs.Flags.Mounts, "mount", "V", nil, "directories to mount, suffix ':w' for writable") diff --git a/util/osutil/os.go b/util/osutil/os.go index 6f986e4c..220f7480 100644 --- a/util/osutil/os.go +++ b/util/osutil/os.go @@ -5,11 +5,32 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "strings" "github.com/sirupsen/logrus" ) +// EnvVar is environment variable +type EnvVar string + +// Exists checks if the environment variable has been set. +func (e EnvVar) Exists() bool { + _, ok := os.LookupEnv(string(e)) + return ok +} + +// Bool returns the environment variable value as boolean. +func (e EnvVar) Bool() bool { + ok, _ := strconv.ParseBool(e.Val()) + return ok +} + +// Bool returns the environment variable value. +func (e EnvVar) Val() string { + return os.Getenv(string(e)) +} + const EnvColimaBinary = "COLIMA_BINARY" // Executable returns the path name for the executable that started