Skip to content

Commit

Permalink
Create a Config package, that handles initialising a basic empty conf…
Browse files Browse the repository at this point in the history
…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
SwayKh committed Sep 8, 2024
1 parent 6c2f4cc commit 9eddb7d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
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
3 changes: 3 additions & 0 deletions go.sum
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=
102 changes: 102 additions & 0 deletions pkg/config/config.go
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
}

0 comments on commit 9eddb7d

Please sign in to comment.