11package main
22
33import (
4+ "fmt"
45 "os"
56 "strings"
67
@@ -9,73 +10,84 @@ import (
910 "gopkg.in/yaml.v3"
1011)
1112
12- func init () {
13- cmd .AddCommand (cmdConfig )
14- cmdConfig .AddCommand (cmdConfigGet )
15- cmdConfig .AddCommand (cmdConfigSet )
16- cmdConfig .AddCommand (cmdConfigShow )
17- }
18-
19- var (
20- cmdConfig = & cobra.Command {
13+ func newConfigCMD () * cobra.Command {
14+ cmd := & cobra.Command {
2115 Use : "config" ,
2216 Short : "Configure your DSS instance" ,
2317 }
2418
25- cmdConfigGet = & cobra.Command {
19+ cmd .AddCommand (newConfigGetCMD ())
20+ cmd .AddCommand (newConfigSetCMD ())
21+ cmd .AddCommand (newConfigShowCMD ())
22+
23+ return cmd
24+ }
25+
26+ func newConfigGetCMD () * cobra.Command {
27+ cmd := & cobra.Command {
2628 Use : "get" ,
2729 Short : "Get a config value" ,
2830 Example : " dss config get nameservers" ,
2931 Args : cobra .ExactArgs (1 ),
30- Run : func (command * cobra.Command , args []string ) {
32+ Run : func (_ * cobra.Command , args []string ) {
3133 switch args [0 ] {
3234 case "nameservers" :
3335 printToConsole ("nameservers: " + cast .ToString (cfg .Nameservers ))
3436 default :
35- log .Fatal ().Msg ("unknown config key" )
37+ log .Fatal ().Msg ("Unknown config key" )
3638 }
3739 },
3840 }
3941
40- cmdConfigSet = & cobra.Command {
42+ return cmd
43+ }
44+
45+ func newConfigSetCMD () * cobra.Command {
46+ cmd := & cobra.Command {
4147 Use : "set" ,
4248 Short : "Set a config value" ,
4349 Example : " dss config set nameservers 8.8.8.8,9.9.9.9" ,
4450 Args : cobra .ExactArgs (2 ),
45- Run : func (command * cobra.Command , args []string ) {
51+ Run : func (_ * cobra.Command , args []string ) {
4652 switch args [0 ] {
4753 case "nameservers" :
4854 cfg .Nameservers = strings .Split (args [1 ], "," )
4955 default :
50- log .Fatal ().Msg ("unknown config key" )
56+ log .Fatal ().Msg ("Unknown config key" )
5157 }
5258
5359 if err := cfg .Save (); err != nil {
54- log .Fatal ().Err (err ).Msg ("unable to save config" )
60+ log .Fatal ().Err (err ).Msg ("Unable to save config" )
5561 }
5662
5763 log .Info ().Msg ("Config updated" )
5864 },
5965 }
6066
61- cmdConfigShow = & cobra.Command {
67+ return cmd
68+ }
69+
70+ func newConfigShowCMD () * cobra.Command {
71+ cmd := & cobra.Command {
6272 Use : "show" ,
6373 Short : "Print full config" ,
6474 Example : " dss config show" ,
6575 Args : cobra .ExactArgs (0 ),
66- Run : func (command * cobra.Command , args []string ) {
76+ Run : func (_ * cobra.Command , _ []string ) {
6777 printToConsole (cfg )
6878 },
6979 }
70- )
80+
81+ return cmd
82+ }
7183
7284type Config struct {
7385 dir string
7486 path string
7587 Nameservers []string `json:"nameservers" yaml:"nameservers"`
7688}
7789
78- func NewConfig (directory string ) (* Config , error ) {
90+ func newConfig (directory string ) (* Config , error ) {
7991 config := Config {
8092 dir : directory ,
8193 path : directory + slash + "config.yml" ,
@@ -90,25 +102,25 @@ func NewConfig(directory string) (*Config, error) {
90102}
91103
92104func (c * Config ) Load () error {
93- // create config if it doesn't exist
105+ // Create config if it doesn't exist.
94106 if _ , err := os .Stat (c .path ); os .IsNotExist (err ) {
95- if err = os .MkdirAll (c .dir , os . ModePerm ); err != nil {
96- log .Fatal ().Err (err ).Msg ("failed to create config directory" )
107+ if err = os .MkdirAll (c .dir , 0o750 ); err != nil {
108+ log .Fatal ().Err (err ).Msg ("Failed to create config directory" )
97109 }
98110
99111 if err = c .Save (); err != nil {
100112 return err
101113 }
102114 }
103115
104- // read config
116+ // Read config.
105117 configData , err := os .ReadFile (c .path )
106118 if err != nil {
107- log .Fatal ().Err (err ).Msg ("unable to read config file" )
119+ log .Fatal ().Err (err ).Msg ("Unable to read config file" )
108120 }
109121
110122 if err = yaml .Unmarshal (configData , & c ); err != nil {
111- log .Fatal ().Err (err ).Msg ("unable to unmarshal config values" )
123+ log .Fatal ().Err (err ).Msg ("Unable to unmarshal config values" )
112124 }
113125
114126 return nil
@@ -117,8 +129,12 @@ func (c *Config) Load() error {
117129func (c * Config ) Save () error {
118130 configData , err := yaml .Marshal (c )
119131 if err != nil {
120- log .Fatal ().Err (err ).Msg ("unable to marshal default config" )
132+ log .Fatal ().Err (err ).Msg ("Unable to marshal default config" )
121133 }
122134
123- return os .WriteFile (c .path , configData , os .ModePerm )
135+ if err = os .WriteFile (c .path , configData , os .ModePerm ); err != nil {
136+ return fmt .Errorf ("write file: %w" , err )
137+ }
138+
139+ return nil
124140}
0 commit comments