Skip to content

Commit

Permalink
config: override --save-config default with COLIMA_SAVE_CONFIG env var
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Aug 25, 2024
1 parent d99bb82 commit c2595d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -118,6 +119,7 @@ const (
)

var defaultK3sArgs = []string{"--disable=traefik"}
var envSaveConfig = osutil.EnvVar("COLIMA_SAVE_CONFIG")

var startCmdArgs struct {
config.Config
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
21 changes: 21 additions & 0 deletions util/osutil/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2595d4

Please sign in to comment.