diff options
| author | Aleksandar Pesic <peske.nis@gmail.com> | 2022-12-04 21:48:20 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-12-12 13:26:51 +0100 |
| commit | 3ac1d35257b56cca92ad0eb7f4d18abb366cf105 (patch) | |
| tree | 432db14d1f92a252518f371be420fa0d3ef044c8 /internal/config | |
| parent | 37bca013bd4405548b274ac473acf959ad661ee6 (diff) | |
simplify error handling
fixes #6
Signed-off-by: Aleksandar Pesic <peske.nis@gmail.com>
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 6761d62..ae023ff 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,12 +4,11 @@ package config import ( "encoding/json" - "fmt" - "io/ioutil" + "os" "path" "github.com/eduvpn/eduvpn-common/internal/util" - "github.com/eduvpn/eduvpn-common/types" + "github.com/go-errors/errors" ) // Config represents a configuration that saves the client's struct as JSON. @@ -22,38 +21,41 @@ type Config struct { } // Init initializes the configuration using the provided directory and name. -func (config *Config) Init(directory string, name string) { - config.Directory = directory - config.Name = name +func (c *Config) Init(directory string, name string) { + c.Directory = directory + c.Name = name } // filename returns the filename of the configuration as a full path. -func (config *Config) filename() string { - pathString := path.Join(config.Directory, config.Name) - return fmt.Sprintf("%s.json", pathString) +func (c *Config) filename() string { + return path.Join(c.Directory, c.Name) + ".json" } // Save saves a structure 'readStruct' to the configuration -// If it was unusuccessful, an error is returned. -func (config *Config) Save(readStruct interface{}) error { - errorMessage := "failed saving configuration" - configDirErr := util.EnsureDirectory(config.Directory) - if configDirErr != nil { - return types.NewWrappedError(errorMessage, configDirErr) +// If it was unsuccessful, an error is returned. +func (c *Config) Save(readStruct interface{}) error { + if err := util.EnsureDirectory(c.Directory); err != nil { + return err + } + cfg, err := json.Marshal(readStruct) + if err != nil { + return errors.WrapPrefix(err, "json.Marshal failed", 0) } - jsonString, marshalErr := json.Marshal(readStruct) - if marshalErr != nil { - return types.NewWrappedError(errorMessage, marshalErr) + if err = os.WriteFile(c.filename(), cfg, 0o600); err != nil { + return errors.WrapPrefix(err, "os.WriteFile failed", 0) } - return ioutil.WriteFile(config.filename(), jsonString, 0o600) + return nil } // Load loads the configuration and writes the structure to 'writeStruct' // If it was unsuccessful, an error is returned. -func (config *Config) Load(writeStruct interface{}) error { - bytes, readErr := ioutil.ReadFile(config.filename()) - if readErr != nil { - return types.NewWrappedError("failed loading configuration", readErr) +func (c *Config) Load(writeStruct interface{}) error { + bts, err := os.ReadFile(c.filename()) + if err != nil { + return errors.WrapPrefix(err, "failed loading configuration", 0) + } + if err = json.Unmarshal(bts, writeStruct); err != nil { + return errors.WrapPrefix(err, "json.Unmarshal failed", 0) } - return json.Unmarshal(bytes, writeStruct) + return nil } |
