Skip to content

Commit

Permalink
chore(config): Add param to skip saving config file
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 13, 2024
1 parent cf94bc6 commit 43d4aa6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func preRun(cmd *cobra.Command, _ []string) error {
return nil
}

conf, err := config.Load(cmd)
conf, err := config.Load(cmd, true)
if err != nil {
return err
}
Expand Down
32 changes: 17 additions & 15 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func flagTable() map[string]string {
}
}

func Load(cmd *cobra.Command) (*Config, error) {
func Load(cmd *cobra.Command, save bool) (*Config, error) {
k := koanf.New(".")
conf := NewDefault()

Expand Down Expand Up @@ -62,24 +62,26 @@ func Load(cmd *cobra.Command) (*Config, error) {
return nil, err
}

if err := k.UnmarshalWithConf("", conf, koanf.UnmarshalConf{Tag: "toml"}); err != nil {
return nil, err
}
if save {
if err := k.UnmarshalWithConf("", conf, koanf.UnmarshalConf{Tag: "toml"}); err != nil {
return nil, err
}

newCfg, err := toml.Marshal(conf)
if err != nil {
return nil, err
}
newCfg, err := toml.Marshal(conf)
if err != nil {
return nil, err
}

if !bytes.Equal(cfgContents, newCfg) {
if cfgNotExists {
if err := os.MkdirAll(filepath.Dir(cfgFile), 0o777); err != nil {
return nil, err
if !bytes.Equal(cfgContents, newCfg) {
if cfgNotExists {
if err := os.MkdirAll(filepath.Dir(cfgFile), 0o777); err != nil {
return nil, err
}
}
}

if err := os.WriteFile(cfgFile, newCfg, 0o666); err != nil {
return nil, err
if err := os.WriteFile(cfgFile, newCfg, 0o666); err != nil {
return nil, err
}
}
}

Expand Down

0 comments on commit 43d4aa6

Please sign in to comment.