Skip to content

Commit c2595d4

Browse files
committed
config: override --save-config default with COLIMA_SAVE_CONFIG env var
Signed-off-by: Abiola Ibrahim <[email protected]>
1 parent d99bb82 commit c2595d4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

cmd/start.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/abiosoft/colima/environment/container/docker"
2121
"github.com/abiosoft/colima/environment/container/kubernetes"
2222
"github.com/abiosoft/colima/util"
23+
"github.com/abiosoft/colima/util/osutil"
2324
log "github.com/sirupsen/logrus"
2425
"github.com/spf13/cobra"
2526
)
@@ -118,6 +119,7 @@ const (
118119
)
119120

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

122124
var startCmdArgs struct {
123125
config.Config
@@ -142,6 +144,11 @@ func init() {
142144
mounts := strings.Join([]string{defaultMountTypeQEMU, "9p", "virtiofs"}, ", ")
143145
types := strings.Join([]string{defaultVMType, "vz"}, ", ")
144146

147+
saveConfigDefault := true
148+
if envSaveConfig.Exists() {
149+
saveConfigDefault = envSaveConfig.Bool()
150+
}
151+
145152
root.Cmd().AddCommand(startCmd)
146153
startCmd.Flags().StringVarP(&startCmdArgs.Runtime, "runtime", "r", docker.Name, "container runtime ("+runtimes+")")
147154
startCmd.Flags().BoolVar(&startCmdArgs.Flags.ActivateRuntime, "activate", true, "set as active Docker/Kubernetes context on startup")
@@ -178,7 +185,7 @@ func init() {
178185
// config
179186
startCmd.Flags().BoolVarP(&startCmdArgs.Flags.Edit, "edit", "e", false, "edit the configuration file before starting")
180187
startCmd.Flags().StringVar(&startCmdArgs.Flags.Editor, "editor", "", `editor to use for edit e.g. vim, nano, code (default "$EDITOR" env var)`)
181-
startCmd.Flags().BoolVar(&startCmdArgs.Flags.SaveConfig, "save-config", true, "persist and overwrite config file with (newly) specified flags")
188+
startCmd.Flags().BoolVar(&startCmdArgs.Flags.SaveConfig, "save-config", saveConfigDefault, "persist and overwrite config file with (newly) specified flags")
182189

183190
// mounts
184191
startCmd.Flags().StringSliceVarP(&startCmdArgs.Flags.Mounts, "mount", "V", nil, "directories to mount, suffix ':w' for writable")

util/osutil/os.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,32 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"strconv"
89
"strings"
910

1011
"github.com/sirupsen/logrus"
1112
)
1213

14+
// EnvVar is environment variable
15+
type EnvVar string
16+
17+
// Exists checks if the environment variable has been set.
18+
func (e EnvVar) Exists() bool {
19+
_, ok := os.LookupEnv(string(e))
20+
return ok
21+
}
22+
23+
// Bool returns the environment variable value as boolean.
24+
func (e EnvVar) Bool() bool {
25+
ok, _ := strconv.ParseBool(e.Val())
26+
return ok
27+
}
28+
29+
// Bool returns the environment variable value.
30+
func (e EnvVar) Val() string {
31+
return os.Getenv(string(e))
32+
}
33+
1334
const EnvColimaBinary = "COLIMA_BINARY"
1435

1536
// Executable returns the path name for the executable that started

0 commit comments

Comments
 (0)