diff options
| author | Jeff Carr <[email protected]> | 2025-03-10 03:12:09 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-10 07:59:25 -0500 |
| commit | fba2d24625d844b7f6115a6cb86f7fc6e1f9e3d5 (patch) | |
| tree | f1e787166b7a9f9d54c9551b9bf3db6d89fc1e46 /config.go | |
| parent | dfbb8090acaba51ab29368202e5c97fd18856558 (diff) | |
start making it work in the real world
Diffstat (limited to 'config.go')
| -rw-r--r-- | config.go | 65 |
1 files changed, 41 insertions, 24 deletions
@@ -10,7 +10,6 @@ import ( "path/filepath" "go.wit.com/log" - "google.golang.org/protobuf/proto" ) func (m *Portmaps) ConfigSave() error { @@ -19,15 +18,25 @@ func (m *Portmaps) ConfigSave() error { } s := m.FormatTEXT() log.Info("proto.Marshal() worked len", len(s)) - configWrite([]byte(s)) + configWrite(argv.Config, []byte(s)) return nil } func ConfigLoad() *Portmaps { - if os.Getenv("CLOUD_HOME") == "" { + var fullname string + if os.Getenv("CLOUD_HOME") != "" { + fullname = filepath.Join(os.Getenv("CLOUD_HOME"), "gus.text") + if argv.Config == "" { + argv.Config = fullname + } + } + if argv.Config != "" { + fullname = argv.Config + } + if fullname == "" { homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/cloud") - os.Setenv("CLOUD_HOME", fullpath) + fullname = filepath.Join(homeDir, ".config/cloud", "gus.text") + argv.Config = fullname } var data []byte @@ -51,19 +60,30 @@ func ConfigLoad() *Portmaps { return p } +/* func (m *Portmaps) ConfigLoad() error { + var fullname string if m == nil { return errors.New("It's not safe to run ConfigLoad() on a nil ?") } - if os.Getenv("CLOUD_HOME") == "" { + if os.Getenv("CLOUD_HOME") != "" { + fullname = filepath.Join(os.Getenv("CLOUD_HOME"), "gus.text") + if argv.Config == "" { + argv.Config = fullname + } + } + if argv.Config != "" { + fullname = argv.Config + } + if fullname == "" { homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/cloud") - os.Setenv("CLOUD_HOME", fullpath) + fullname = filepath.Join(homeDir, ".config/cloud", "gus.text") + argv.Config = fullname } var data []byte var err error - if data, err = loadFile("gus.text"); err != nil { + if data, err = loadFile(); err != nil { // something went wrong loading the file return err } @@ -79,11 +99,9 @@ func (m *Portmaps) ConfigLoad() error { log.Log(INFO, "gus.ConfigLoad() has", m.Len(), "port mappings") return nil } +*/ -func loadFile(filename string) ([]byte, error) { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/cloud") - fullname := filepath.Join(p, filename) +func loadFile(fullname string) ([]byte, error) { data, err := os.ReadFile(fullname) if errors.Is(err, os.ErrNotExist) { // if file does not exist, just return nil. this @@ -96,9 +114,7 @@ func loadFile(filename string) ([]byte, error) { return data, nil } -func (m *Portmaps) loadFile(fname string) ([]byte, error) { - fullname := filepath.Join(os.Getenv("CLOUD_HOME"), fname) - +func (m *Portmaps) loadFile(fullname string) ([]byte, error) { data, err := os.ReadFile(fullname) if err != nil { // log.Info("open config file :", err) @@ -107,11 +123,11 @@ func (m *Portmaps) loadFile(fname string) ([]byte, error) { return data, nil } -func configWrite(data []byte) error { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/cloud") - fname := filepath.Join(p, "gus.text") - cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) +func configWrite(fullname string, data []byte) error { + if _, base := filepath.Split(fullname); base == "" { + return fmt.Errorf("--config option not set") + } + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) defer cfgfile.Close() if err != nil { log.Warn("open config file :", err) @@ -121,9 +137,10 @@ func configWrite(data []byte) error { return nil } -func (m *Portmaps) configWrite(fname string, data []byte) error { - fullname := filepath.Join(os.Getenv("CLOUD_HOME"), fname) - +func (m *Portmaps) configWrite(fullname string, data []byte) error { + if _, base := filepath.Split(fullname); base == "" { + return fmt.Errorf("--config option not set") + } cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) defer cfgfile.Close() if err != nil { |
