From e9f8db8ee8fccf60e58deb1d72766f94a053bb16 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 28 Nov 2022 11:18:14 +0100 Subject: 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 --- internal/config/config.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'internal/config') 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) } -- cgit v1.2.3