diff options
author | Sean E. Russell <ser@ser1.net> | 2020-04-29 10:25:21 -0500 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-04-29 10:26:14 -0500 |
commit | 3f26ebef76575849f99c2097d38152317746b8e0 (patch) | |
tree | 210ebcc3018cdcf2a157ad7ceb6939dbf67b4f49 /config.go | |
parent | 3487bbfaed43d53faf8d80c0fce488f113820660 (diff) |
Mostly adds the -C option. KNOWN: it must be the first argument.
Doc
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 52 |
1 files changed, 38 insertions, 14 deletions
@@ -5,7 +5,9 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "log" + "os" "path/filepath" "strconv" "strings" @@ -16,6 +18,9 @@ import ( "github.com/xxxserxxx/gotop/v4/widgets" ) +// CONFFILE is the name of the default config file +const CONFFILE = "gotop.conf" + type Config struct { ConfigDir configdir.ConfigDir GraphHorizontalScale int @@ -34,6 +39,7 @@ type Config struct { Temps []string Test bool ExtensionVars map[string]string + ConfigFile string } func NewConfig() Config { @@ -54,21 +60,25 @@ func NewConfig() Config { ExtensionVars: make(map[string]string), } conf.Colorscheme, _ = colorschemes.FromName(conf.ConfigDir, "default") + folder := conf.ConfigDir.QueryFolderContainsFile(CONFFILE) + if folder != nil { + conf.ConfigFile = filepath.Join(folder.Path, CONFFILE) + } return conf } func (conf *Config) Load() error { var in []byte + if conf.ConfigFile == "" { + return nil + } var err error - cfn := "gotop.conf" - folder := conf.ConfigDir.QueryFolderContainsFile(cfn) - if folder != nil { - if in, err = folder.ReadFile(cfn); err != nil { - return err - } - } else { + if _, err = os.Stat(conf.ConfigFile); os.IsNotExist(err) { return nil } + if in, err = ioutil.ReadFile(conf.ConfigFile); err != nil { + return err + } return load(bytes.NewReader(in), conf) } @@ -169,21 +179,35 @@ func load(in io.Reader, conf *Config) error { return nil } +// Write serializes the configuration to a file. +// The configuration written is based on the loaded configuration, plus any +// command-line changes, so it can be used to update an existing configuration +// file. The file will be written to the specificed `--config` argument file, +// if one is set; otherwise, it'll create one in the user's config directory. func (conf *Config) Write() (string, error) { - cfn := "gotop.conf" - ds := conf.ConfigDir.QueryFolders(configdir.Global) - if len(ds) == 0 { - ds = conf.ConfigDir.QueryFolders(configdir.Local) + var dir *configdir.Config + var file string = CONFFILE + if conf.ConfigFile == "" { + ds := conf.ConfigDir.QueryFolders(configdir.Global) if len(ds) == 0 { - return "", fmt.Errorf("error locating config folders") + ds = conf.ConfigDir.QueryFolders(configdir.Local) + if len(ds) == 0 { + return "", fmt.Errorf("error locating config folders") + } } + ds[0].CreateParentDir(CONFFILE) + dir = ds[0] + } else { + dir = &configdir.Config{} + dir.Path = filepath.Dir(conf.ConfigFile) + file = filepath.Base(conf.ConfigFile) } marshalled := marshal(conf) - err := ds[0].WriteFile(cfn, marshalled) + err := dir.WriteFile(file, marshalled) if err != nil { return "", err } - return filepath.Join(ds[0].Path, cfn), nil + return filepath.Join(dir.Path, file), nil } func marshal(c *Config) []byte { |