summaryrefslogtreecommitdiff
path: root/internal/config/config.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-11-28 11:18:14 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-11-28 11:18:42 +0100
commite9f8db8ee8fccf60e58deb1d72766f94a053bb16 (patch)
treeffa5a9be67717ecc8ff7bdc03d5f96028facb0e3 /internal/config/config.go
parentb4ff890ec2b459148d893499a34a6d2954530369 (diff)
Document: Add comments for most functions and packages
Errors and test files still need to be done. Also some getters are changed by removing the 'get' prefix
Diffstat (limited to 'internal/config/config.go')
-rw-r--r--internal/config/config.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 180b881..fa3045b 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -1,3 +1,5 @@
+// Package config implements functions for saving a struct to a file
+// It then provides functions to later read it such that we can restore the same struct
package config
import (
@@ -10,21 +12,29 @@ import (
"github.com/eduvpn/eduvpn-common/types"
)
+// Config represents a configuration that saves the client's struct as JSON
type Config struct {
+ // Directory represents the path to where the data is saved
Directory string
+
+ // Name defines the name of file excluding the .json extension
Name string
}
+// 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 (config *Config) GetFilename() string {
+// 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)
}
+// Save saves a structure 'readStruct' to the configuration
+// If it was unusuccessful, an an error is returned
func (config *Config) Save(readStruct interface{}) error {
errorMessage := "failed saving configuration"
configDirErr := util.EnsureDirectory(config.Directory)
@@ -35,11 +45,13 @@ func (config *Config) Save(readStruct interface{}) error {
if marshalErr != nil {
return types.NewWrappedError(errorMessage, marshalErr)
}
- return ioutil.WriteFile(config.GetFilename(), jsonString, 0o600)
+ return ioutil.WriteFile(config.filename(), jsonString, 0o600)
}
+// 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.GetFilename())
+ bytes, readErr := ioutil.ReadFile(config.filename())
if readErr != nil {
return types.NewWrappedError("failed loading configuration", readErr)
}