Skip to content

Commit 4c7d8ac

Browse files
committed
use os.OpenFile over os.Stat to create config if not exist. fix creating global config entry
1 parent dce2c12 commit 4c7d8ac

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

cmd/skaffold/app/cmd/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package config
1818

1919
type Config struct {
20-
Global *ContextConfig
20+
Global *ContextConfig `yaml:"global,omitempty"`
2121
ContextConfigs []*ContextConfig `yaml:"kubeContexts"`
2222
}
2323

cmd/skaffold/app/cmd/config/util.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ func resolveConfigFile() error {
6767
}
6868
configFile = filepath.Join(home, defaultConfigLocation)
6969
}
70-
_, err = os.Stat(configFile)
71-
// TODO(nkubala): create default config?
72-
if err != nil {
73-
return err
74-
}
75-
76-
return nil
70+
_, err = os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
71+
return err
7772
}
7873

7974
func ReadConfigForFile(filename string) (*Config, error) {
@@ -120,6 +115,13 @@ func getOrCreateConfigForKubectx() (*ContextConfig, error) {
120115
return nil, err
121116
}
122117
if global {
118+
if cfg.Global == nil {
119+
newCfg := &ContextConfig{}
120+
cfg.Global = newCfg
121+
if err := writeFullConfig(cfg); err != nil {
122+
return nil, err
123+
}
124+
}
123125
return cfg.Global, nil
124126
}
125127
for _, contextCfg := range cfg.ContextConfigs {

integration/run_test.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build integration
2-
31
/*
42
Copyright 2018 The Skaffold Authors
53
@@ -22,7 +20,6 @@ import (
2220
"bytes"
2321
"flag"
2422
"fmt"
25-
"math/rand"
2623
"os"
2724
"os/exec"
2825
"strings"
@@ -246,7 +243,7 @@ func TestListConfig(t *testing.T) {
246243
},
247244
ContextConfigs: []*config.ContextConfig{
248245
{
249-
Kubectx: "test-context",
246+
Kubecontext: "test-context",
250247
DefaultRepo: "context-local-repository",
251248
},
252249
},
@@ -273,7 +270,7 @@ func TestListConfig(t *testing.T) {
273270
expectedOutput: []string{
274271
"global:",
275272
"default-repo: global-repository",
276-
"kubectx: test-context",
273+
"kube-context: test-context",
277274
"default-repo: context-local-repository",
278275
},
279276
},
@@ -309,7 +306,7 @@ func TestSetConfig(t *testing.T) {
309306
},
310307
ContextConfigs: []*config.ContextConfig{
311308
{
312-
Kubectx: "test-context",
309+
Kubecontext: "test-context",
313310
DefaultRepo: "context-local-repository",
314311
},
315312
},
@@ -345,7 +342,7 @@ func TestSetConfig(t *testing.T) {
345342

346343
for _, test := range tests {
347344
t.Run(test.description, func(t *testing.T) {
348-
value := randomString()
345+
value := util.RandomID()
349346
args := []string{"config", "set", test.key, value}
350347
args = append(args, "-c", cfg)
351348
if test.kubectx != "" {
@@ -379,12 +376,3 @@ func TestSetConfig(t *testing.T) {
379376
})
380377
}
381378
}
382-
383-
func randomString() string {
384-
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
385-
b := make([]rune, 16)
386-
for i := range b {
387-
b[i] = letters[rand.Intn(len(letters))]
388-
}
389-
return string(b)
390-
}

0 commit comments

Comments
 (0)