summaryrefslogtreecommitdiff
path: root/internal/config.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-22 16:29:59 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-22 16:29:59 +0200
commitb1d92b395322f2164ccfb44b0f7caebbaece6b62 (patch)
tree2133e4045b4af4d07a98674b7ae3a234670f0305 /internal/config.go
parent3a4ae2942b43923ff98fd2eca8878c3cf145686c (diff)
Refactor: Restructure project
- Add an internal folder where all the internal code lives - Make a state.go and state_test.go for the public interface This gives a more clear separation between functions and modules. It also makes this a more typical Go project setup.
Diffstat (limited to 'internal/config.go')
-rw-r--r--internal/config.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/config.go b/internal/config.go
new file mode 100644
index 0000000..47f773e
--- /dev/null
+++ b/internal/config.go
@@ -0,0 +1,43 @@
+package internal
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "path"
+)
+
+type Config struct {
+ Name string
+ Directory string
+}
+
+func (config *Config) Init(name string, directory string) {
+ config.Name = name
+ config.Directory = directory
+}
+
+func (config *Config) GetFilename() string {
+ pathString := path.Join(config.Directory, config.Name)
+ return fmt.Sprintf("%s.json", pathString)
+}
+
+func (config *Config) Save(readStruct interface{}) error {
+ configDirErr := EnsureDirectory(config.Directory)
+ if configDirErr != nil {
+ return configDirErr
+ }
+ jsonString, marshalErr := json.Marshal(readStruct)
+ if marshalErr != nil {
+ return marshalErr
+ }
+ return ioutil.WriteFile(config.GetFilename(), jsonString, 0o644)
+}
+
+func (config *Config) Load(writeStruct interface{}) error {
+ bytes, readErr := ioutil.ReadFile(config.GetFilename())
+ if readErr != nil {
+ return readErr
+ }
+ return json.Unmarshal(bytes, writeStruct)
+}