Skip to content

Commit

Permalink
Update SetupDirectories and InitiasliseConfig function to fix global …
Browse files Browse the repository at this point in the history
…variable issues

ConfigName var is only used in the setup directories function so remove
it as a global variable
InitDirectory var is only used during initialising config. All
references to InitDirectory now use the variable directly from the
Global Configuration Struct. Setup directories no longer defines a
InitDirectory variable
  • Loading branch information
SwayKh committed Sep 18, 2024
1 parent 1f5d2a5 commit 9eccb26
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Add(args []string) error {
}

filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(config.InitDirectory, filename)
destinationPath = filepath.Join(config.Configuration.InitDirectory, filename)

case 2:
// set first and second args as source and destination path, get absolute
Expand Down
22 changes: 20 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (

var (
HomeDirectory string
InitDirectory string
ConfigPath string
ConfigName string
Configuration appConfig
)

Expand All @@ -33,12 +31,32 @@ type record struct {
Paths []string `yaml:"paths"`
}

func SetupDirectories() error {
var err error
HomeDirectory, err = os.UserHomeDir()
if err != nil {
return fmt.Errorf("Couldn't get the home directory")
}

ConfigName := ".linksym.yaml"
ConfigPath = filepath.Join(Configuration.InitDirectory, ConfigName)

return nil
}

func InitialiseConfig() error {
err := SetupDirectories()
if err != nil {
return fmt.Errorf("Initialising Env: \n%w", err)
}

InitDirectory, err := os.Getwd()
if err != nil {
return fmt.Errorf("Couldn't get the current working directory")
}

InitDirectory = aliasPath(InitDirectory, true)

configuration := appConfig{
InitDirectory: InitDirectory,
Records: []record{},
Expand Down
19 changes: 0 additions & 19 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)

func SetupDirectories() error {
var err error
HomeDirectory, err = os.UserHomeDir()
if err != nil {
return errors.New("Couldn't get the home directory")
}

InitDirectory, err = os.Getwd()
if err != nil {
return errors.New("Couldn't get the current working directory")
}

ConfigName := ".linksym.yaml"

ConfigPath = filepath.Join(InitDirectory, ConfigName)
return nil
}

func CheckFile(path string) (bool, os.FileInfo, error) {
fileInfo, err := os.Stat(path)
if err != nil {
Expand Down

0 comments on commit 9eccb26

Please sign in to comment.