Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ func ensureConfiguration() error {

defaultContextPath := filepath.Join(contextsPath, defaultContextFileName)
if _, err := os.Stat(defaultContextPath); os.IsNotExist(err) {
_, err = os.Create(defaultContextPath)
f, err := os.Create(defaultContextPath)
if err != nil {
return fmt.Errorf("error creating default.yaml context file %v", err)
}
defer f.Close()

err = WriteYamlFile(defaultContextPath, DefaultContextConfigContents())
if err != nil {
Expand Down Expand Up @@ -273,13 +274,7 @@ func WriteConfigValueToConfigFile(key, value string) error {
home := GetHomeDir()

configFilePath := filepath.Join(home, rootConfigPathName, contextConfigFileName)
f, err := os.OpenFile(configFilePath, os.O_RDWR, ReadWritePerms)
if err != nil {
return err
}
defer f.Close()

file, err := DecodeYAMLFile(f.Name())
file, err := DecodeYAMLFile(configFilePath)
if err != nil {
return err
}
Expand All @@ -294,7 +289,7 @@ func WriteConfigValueToConfigFile(key, value string) error {
}
configValues[key] = value

err = atomicwrite(f.Name(), &configValues)
err = atomicwrite(configFilePath, &configValues)
if err != nil {
return err
}
Expand All @@ -311,13 +306,14 @@ func atomicwrite(file string, c *ContextMap) (err error) {
return fmt.Errorf("cannot create temp file: %v", err)
}

defer f.Close()
defer os.Remove(f.Name())

err = WriteYamlFile(f.Name(), c)
if err != nil {
f.Close()
return err
}
f.Close()

info, err := os.Stat(file)
if err != nil {
Expand All @@ -326,6 +322,8 @@ func atomicwrite(file string, c *ContextMap) (err error) {
_ = os.Chmod(f.Name(), info.Mode())
}

os.Remove(file)

// replace file with the tempfile
err = os.Rename(f.Name(), file)
if err != nil {
Expand Down