From 9eddb7da321753d65cc508f279433a8df5a85c1b Mon Sep 17 00:00:00 2001 From: SwayKh Date: Sun, 8 Sep 2024 23:01:30 +0530 Subject: [PATCH] Create a Config package, that handles initialising a basic empty config, loading the config for use in other pacakge. Will add function to append data to the record field in yaml config --- go.mod | 2 + go.sum | 3 ++ pkg/config/config.go | 102 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 go.sum create mode 100644 pkg/config/config.go diff --git a/go.mod b/go.mod index c1eaa43..0ab8313 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/SwayKh/linksym go 1.23.0 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..af4fe33 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,102 @@ +package config + +import ( + "errors" + "fmt" + "io" + "os" + + "gopkg.in/yaml.v3" +) + +// This package should, create the default config with the initialised directory +// Read the config for every other command to work and use the variable vales +// Add data under the "Links" variable whenever a new config is add via the +// project. +// The config file can be either .json .ini .toml .yaml +// I think yaml is a good file format for this + +type Config struct { + InitDirectory string `yaml:"init_directory"` + Record [][]string `yaml:"record"` +} + +var ( + homeDirectory string + currentWorkingDirectory string +) + +func setupDirectories() error { + var err error + homeDirectory, err = os.UserHomeDir() + if err != nil { + return errors.New("Couldn't get the home directory") + } + + currentWorkingDirectory, err = os.Getwd() + if err != nil { + return errors.New("Couldn't get the current working directory") + } + return nil +} + +// Create a init function, that create the config files, stores the working +// directory, and other stuff, every other command needs to check if the config +// file exists, before it works. +// The config package will be separates, that adds and reads config, the init +// function should probably call that package + +func Initialise() error { + err := setupDirectories() + if err != nil { + return err + } + + cfg := Config{ + InitDirectory: currentWorkingDirectory, + Record: [][]string{}, + } + + configPath := currentWorkingDirectory + "/.linksym.yaml" + data, err := yaml.Marshal(&cfg) + if err != nil { + return err + } + + err = os.WriteFile(configPath, data, 0o644) + if err != nil { + return err + } + return nil +} + +func LoadConfig(configPath string) (*Config, error) { + // Check if config file exists + _, err := os.Stat(configPath) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, errors.New("Config file doesn't exist") + } else { + return nil, err + } + } + + file, err := os.Open(configPath) + if err != nil { + return nil, fmt.Errorf("error opening config file: %s, %w", configPath, err) + } + defer file.Close() + + data, err := io.ReadAll(file) + if err != nil { + return nil, err + } + + config := Config{} + + err = yaml.Unmarshal(data, &config) + if err != nil { + return nil, err + } + return &config, nil +}