-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a Config package, that handles initialising a basic empty conf…
…ig, loading the config for use in other pacakge. Will add function to append data to the record field in yaml config
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/SwayKh/linksym | ||
|
||
go 1.23.0 | ||
|
||
require gopkg.in/yaml.v3 v3.0.1 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |