Skip to content

Commit 3b3764f

Browse files
authored
Do not pollute home dir (#21)
* Use xdg.Home instead of os.UserHomeDir No error from xdg.Home. * Only use ~/.mctl.yml if it already exists Otherwise use xdg which is smart about giving us the platform specific cache dir. See https://github.com/adrg/xdg#xdg-base-directory for values used. * Change default config path in usage string and regenerate docs
1 parent e9a1e92 commit 3b3764f

33 files changed

+61
-45
lines changed

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func Execute() {
4545
}
4646

4747
func init() {
48-
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mctl.yml)")
48+
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/mctl/config.yml)")
4949
RootCmd.PersistentFlags().BoolVar(&reAuth, "reauth", false, "re-authenticate with oauth services")
5050
}

docs/mctl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mctl is a CLI utility to interact with metal toolbox services
77
### Options
88

99
```
10-
--config string config file (default is $HOME/.mctl.yml)
10+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
1111
-h, --help help for mctl
1212
--reauth re-authenticate with oauth services
1313
```

docs/mctl_completion.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ See each sub-command's help for details on how to use the generated script.
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
--reauth re-authenticate with oauth services
2424
```
2525

docs/mctl_completion_bash.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mctl completion bash
4242
### Options inherited from parent commands
4343

4444
```
45-
--config string config file (default is $HOME/.mctl.yml)
45+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
4646
--reauth re-authenticate with oauth services
4747
```
4848

docs/mctl_completion_fish.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mctl completion fish [flags]
3333
### Options inherited from parent commands
3434

3535
```
36-
--config string config file (default is $HOME/.mctl.yml)
36+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
3737
--reauth re-authenticate with oauth services
3838
```
3939

docs/mctl_completion_powershell.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mctl completion powershell [flags]
3030
### Options inherited from parent commands
3131

3232
```
33-
--config string config file (default is $HOME/.mctl.yml)
33+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
3434
--reauth re-authenticate with oauth services
3535
```
3636

docs/mctl_completion_zsh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mctl completion zsh [flags]
4444
### Options inherited from parent commands
4545

4646
```
47-
--config string config file (default is $HOME/.mctl.yml)
47+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
4848
--reauth re-authenticate with oauth services
4949
```
5050

docs/mctl_create.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl create [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

docs/mctl_create_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mctl create firmware-set [flags]
2020
### Options inherited from parent commands
2121

2222
```
23-
--config string config file (default is $HOME/.mctl.yml)
23+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2424
--reauth re-authenticate with oauth services
2525
```
2626

docs/mctl_create_firmware.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl create firmware [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
--reauth re-authenticate with oauth services
2323
```
2424

docs/mctl_delete.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl delete [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

docs/mctl_delete_condition.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mctl delete condition [flags]
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
--reauth re-authenticate with oauth services
2424
```
2525

docs/mctl_delete_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl delete firmware-set [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
--reauth re-authenticate with oauth services
2323
```
2424

docs/mctl_edit.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl edit [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

docs/mctl_edit_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mctl edit firmware-set [flags]
2222
### Options inherited from parent commands
2323

2424
```
25-
--config string config file (default is $HOME/.mctl.yml)
25+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2626
--reauth re-authenticate with oauth services
2727
```
2828

docs/mctl_gendocs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl gendocs [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

docs/mctl_get.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl get [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
--reauth re-authenticate with oauth services
2323
```
2424

docs/mctl_get_bios-config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl get bios-config [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
-o, --output string {json|text} (default "json")
2323
--reauth re-authenticate with oauth services
2424
```

docs/mctl_get_component.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mctl get component [flags]
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
-o, --output string {json|text} (default "json")
2424
--reauth re-authenticate with oauth services
2525
```

docs/mctl_get_condition.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mctl get condition [flags]
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
-o, --output string {json|text} (default "json")
2424
--reauth re-authenticate with oauth services
2525
```

docs/mctl_get_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mctl get firmware-set [flags]
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
-o, --output string {json|text} (default "json")
2424
--reauth re-authenticate with oauth services
2525
```

docs/mctl_get_firmware.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl get firmware [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
-o, --output string {json|text} (default "json")
2323
--reauth re-authenticate with oauth services
2424
```

docs/mctl_install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl install [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

docs/mctl_install_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mctl install firmware-set [flags]
2121
### Options inherited from parent commands
2222

2323
```
24-
--config string config file (default is $HOME/.mctl.yml)
24+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2525
--reauth re-authenticate with oauth services
2626
```
2727

docs/mctl_install_status.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl install status --server | -s <server uuid> [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
--reauth re-authenticate with oauth services
2323
```
2424

docs/mctl_list.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mctl list [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--config string config file (default is $HOME/.mctl.yml)
21+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2222
--reauth re-authenticate with oauth services
2323
```
2424

docs/mctl_list_condition.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mctl list condition [flags]
1919
### Options inherited from parent commands
2020

2121
```
22-
--config string config file (default is $HOME/.mctl.yml)
22+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2323
--output-json Output listing as JSON
2424
--reauth re-authenticate with oauth services
2525
```

docs/mctl_list_firmware-set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mctl list firmware-set [flags]
2020
### Options inherited from parent commands
2121

2222
```
23-
--config string config file (default is $HOME/.mctl.yml)
23+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2424
--output-json Output listing as JSON
2525
--reauth re-authenticate with oauth services
2626
```

docs/mctl_list_firmware.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl list firmware [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--output-json Output listing as JSON
2222
--reauth re-authenticate with oauth services
2323
```

docs/mctl_version.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mctl version [flags]
1717
### Options inherited from parent commands
1818

1919
```
20-
--config string config file (default is $HOME/.mctl.yml)
20+
--config string config file (default is $XDG_CONFIG_HOME/mctl/config.yml)
2121
--reauth re-authenticate with oauth services
2222
```
2323

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/metal-toolbox/mctl
33
go 1.19
44

55
require (
6+
github.com/adrg/xdg v0.4.0
67
github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d
78
github.com/coreos/go-oidc v2.2.1+incompatible
89
github.com/davecgh/go-spew v1.1.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:H
509509
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
510510
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
511511
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
512+
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
513+
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
512514
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
513515
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
514516
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

internal/app/app.go

+28-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"net/url"
66
"os"
7+
"path/filepath"
78

9+
"github.com/adrg/xdg"
810
"github.com/metal-toolbox/mctl/pkg/model"
911
"github.com/pkg/errors"
1012
"github.com/spf13/viper"
@@ -36,30 +38,41 @@ func New(_ context.Context, cfgFile string, reauth bool) (app *App, err error) {
3638
return &App{Config: cfg, Reauth: reauth}, nil
3739
}
3840

39-
func loadConfig(cfgFile string) (*model.Config, error) {
40-
cfg := &model.Config{}
41-
42-
if cfgFile != "" {
43-
cfg.File = cfgFile
44-
} else {
45-
homedir, err := os.UserHomeDir()
46-
if err != nil {
47-
return nil, err
48-
}
41+
func openConfig(path string) (*os.File, error) {
42+
if path != "" {
43+
return os.Open(path)
44+
}
45+
path = viper.GetString("mctlconfig")
46+
if path != "" {
47+
return os.Open(path)
48+
}
4949

50-
cfg.File = homedir + "/" + ".mctl.yml"
50+
path = filepath.Join(xdg.Home, ".mctl.yml")
51+
f, err := os.Open(path)
52+
if err != nil {
53+
return f, nil
54+
}
55+
if !errors.Is(err, os.ErrNotExist) {
56+
return nil, err
5157
}
5258

53-
viper.AutomaticEnv()
54-
if viper.GetString("mctlconfig") != "" {
55-
cfg.File = viper.GetString("mctlconfig")
59+
path, err = xdg.ConfigFile("mctl/config.yaml")
60+
if err != nil {
61+
return nil, err
5662
}
5763

58-
h, err := os.Open(cfg.File)
64+
return os.Open(path)
65+
}
66+
67+
func loadConfig(cfgFile string) (*model.Config, error) {
68+
cfg := &model.Config{}
69+
viper.AutomaticEnv()
70+
h, err := openConfig(cfgFile)
5971
if err != nil {
6072
return nil, err
6173
}
6274

75+
cfg.File = h.Name()
6376
viper.SetConfigFile(cfg.File)
6477

6578
err = viper.ReadConfig(h)

0 commit comments

Comments
 (0)