Skip to content

Commit

Permalink
Resave config on every run to insert missing config options
Browse files Browse the repository at this point in the history
  • Loading branch information
yamnikov-oleg committed Feb 3, 2017
1 parent 88af5b9 commit e0d4728
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,45 @@ func DefaultConfig() *ProjektorConfig {
return c
}

func OpenConfig() (*ProjektorConfig, error) {
f, err := os.Open(ConfigFilePath)
func SaveConfig(cfg *ProjektorConfig) error {
err := os.MkdirAll(AppDir, 0700)
if err != nil {
return nil, err
return err
}
defer f.Close()

contents, err := ioutil.ReadAll(f)
f, err := os.Create(ConfigFilePath)
if err != nil {
return nil, err
return err
}
defer f.Close()

config := &ProjektorConfig{}
err = yaml.Unmarshal(contents, config)
buf, err := yaml.Marshal(cfg)
if err != nil {
return nil, err
return err
}

return config, nil
}

func CreateConfig() (*ProjektorConfig, error) {
err := os.MkdirAll(AppDir, 0700)
_, err = f.Write(buf)
if err != nil {
return nil, err
return err
}

f, err := os.Create(ConfigFilePath)
return nil
}

func OpenConfig() (*ProjektorConfig, error) {
f, err := os.Open(ConfigFilePath)
if err != nil {
return nil, err
}
defer f.Close()

config := DefaultConfig()
buf, err := yaml.Marshal(config)
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}

_, err = f.Write(buf)
config := DefaultConfig()
err = yaml.Unmarshal(contents, config)
if err != nil {
return nil, err
}
Expand All @@ -87,17 +86,19 @@ func CreateConfig() (*ProjektorConfig, error) {
}

func MustLoadConfig() *ProjektorConfig {
config, err := OpenConfig()
if err == nil {
return config
var config *ProjektorConfig
var err error

config, err = OpenConfig()
if err != nil {
errduring("opening config file at %q", err, "Attempting to create one", ConfigFilePath)
config = DefaultConfig()
}

errduring("opening config file at %q", err, "Attempting to create one", ConfigFilePath)
config, err = CreateConfig()
if err == nil {
if err := SaveConfig(config); err != nil {
errduring("creating config file at %q", err, "Using default options", ConfigFilePath)
return config
}

errduring("creating config file at %q", err, "Using default options", ConfigFilePath)
return DefaultConfig()
return config
}

0 comments on commit e0d4728

Please sign in to comment.