Skip to content

Commit

Permalink
Fix config file initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Jul 20, 2020
1 parent 440692b commit 1f1fd31
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/config/config.go
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"fmt"
"io"
"os"
"os/user"
Expand Down Expand Up @@ -32,7 +33,14 @@ type Binary struct {

func CheckAndLoad() error {
u, _ := user.Current()
f, err := os.OpenFile(filepath.Join(u.HomeDir, ".bin/config.json"), os.O_RDWR|os.O_CREATE, 0666)

configDir := filepath.Join(u.HomeDir, ".bin")

if err := os.Mkdir(configDir, 0755); err != nil && !os.IsExist(err) {
return fmt.Errorf("Error creating config directory [%v]", err)
}

f, err := os.OpenFile(filepath.Join(configDir, "config.json"), os.O_RDWR|os.O_CREATE, 0666)
if err != nil && !os.IsNotExist(err) {
return err
}
Expand All @@ -49,7 +57,13 @@ func CheckAndLoad() error {
if err != nil && err != io.EOF {
return err
} else if err == io.EOF {
// Config file doesn't exist. Initialize it
cfg.Bins = map[string]*Binary{}
f.Close()
if werr := write(); werr != nil {
return werr
}

}

return nil
Expand Down Expand Up @@ -116,7 +130,7 @@ func RemoveBinaries(paths []string) error {

func write() error {
u, _ := user.Current()
f, err := os.OpenFile(filepath.Join(u.HomeDir, ".bin/config.json"), os.O_RDWR|os.O_CREATE, 0666)
f, err := os.OpenFile(filepath.Join(u.HomeDir, ".bin", "config.json"), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
Expand Down

0 comments on commit 1f1fd31

Please sign in to comment.