Skip to content

Commit

Permalink
fix(*): update path with absolute + more
Browse files Browse the repository at this point in the history
  • Loading branch information
Wabri committed Aug 30, 2024
1 parent 7d48322 commit 61c3fce
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 65 deletions.
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rm -r <project_root>/bin &>/dev/null
----- Start BUILD-TEST-DEV -----

# Building daje for development environment...
go build -ldflags "-X github.com/Schrodinger-Hat/Daje/constants.DajeConfigBaseDir=<project_root>/testdata -X
go build -ldflags "-X github.com/Schrodinger-Hat/Daje/constants.ConfigBasepath=<project_root>/testdata -X
github.com/Schrodinger-Hat/Daje/constants.Version=<version>" -o ./bin/daje .

----- End BUILD-TEST-DEV -----
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CURRENT_DIR := $(shell pwd)
BRANCH := $(shell git branch --show-current)
DAJE_TEST_NAME := daje-test-$(BRANCH)

clean:
@echo "\n----- Start CLEAN -----\n"
Expand All @@ -17,11 +19,11 @@ build: clean
build-test-dev: clean
@echo "\n----- Start BUILD-TEST-DEV -----\n"
@echo "# Building daje for development environment..."
go build -ldflags "-X github.com/Schroedinger-Hat/Daje/constants.DajeBasePath=$(CURRENT_DIR)/testdata -X github.com/Schroedinger-Hat/Daje/constants.Version=$(shell git branch --show-current)+$(shell git rev-parse --short origin/main)+$(shell git status --porcelain | wc -l | tr -d ' ')" -o ./bin/daje .
go build -ldflags "-X github.com/Schroedinger-Hat/Daje/constants.ConfigBasepath=$(CURRENT_DIR)/testdata -X github.com/Schroedinger-Hat/Daje/constants.Version=$(BRANCH)+$(shell git rev-parse --short origin/main)+$(shell git status --porcelain | wc -l | tr -d ' ')" -o ./bin/$(DAJE_TEST_NAME) .
@echo "\n----- End BUILD-TEST-DEV -----"

checkhealth: build-test-dev
@echo "\n----- Start CHECKHEALTH -----\n"
@echo "# run checkhealth command..."
./bin/daje checkhealth
./bin/$(DAJE_TEST_NAME) checkhealth
@echo "\n----- End CHECKHEALTH -----"
16 changes: 10 additions & 6 deletions constants/contants.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package constants

var DajeBasePath = "~"
var DajeConfigFileName = ".dajerc"
var DajeConfigPathOrder = []string{
var ConfigBasepath = ""
var ConfigFileName = ".dajerc"
var ConfigPathOrder = []string{
".config/daje/",
".",
}

var DajeConfigParameters = []string{
"dotfiles_directory",
"dotfiles_remote",
var ConfigParameters = []string{
"dotfiles.local",
"dotfiles.remote",
}

var ConfigParametersPath = []string{
"dotfiles.local",
}

var Version = ""
65 changes: 48 additions & 17 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,64 @@ import (
"github.com/spf13/viper"
)

func ExtractConfigParameter(elementName string) (string, error) {
return viper.GetString(elementName), nil
type configReturn struct {
Value string
Error error
}

func LoadConfig() error {
configFilePath, err := getConfigFilePath()
if err != nil {
errorMessage := "LoadConfig:getConfigFilePath->" + err.Error()
log.Fatal(errorMessage)
return errors.New(errorMessage)
configFilePath := ""
viperReadInConfig := func() configReturn {
viper.SetConfigFile(configFilePath)
return configReturn{"", viper.ReadInConfig()}
}
viper.SetConfigFile(configFilePath)
if err = viper.ReadInConfig(); err != nil {
errorMessage := "LoadConfig:SetConfigFile->" + err.Error()
log.Fatal(errorMessage)
return errors.New(errorMessage)
functionOrder := []func() configReturn{
checkBasePath,
getConfigFilePath,
viperReadInConfig,
}

for _, function := range functionOrder {
result := function()
if result.Value != "" {
configFilePath = result.Value
}
if result.Error != nil {
errorMessage := "LoadConfig->" + result.Error.Error()
log.Fatal(errorMessage)
return errors.New(errorMessage)
}
}

pathRelativeToAbsolute(path.Dir(configFilePath))
return nil
}

func getConfigFilePath() (string, error) {
for _, value := range constants.DajeConfigPathOrder {
currentFilepath := path.Join(constants.DajeBasePath, value, constants.DajeConfigFileName)
func checkBasePath() configReturn {
if constants.ConfigBasepath == "" {
homepath, err := os.UserHomeDir()
if err != nil {
return configReturn{"", errors.New("getConfigFilePath: User home not found")}
}
constants.ConfigBasepath = homepath
}
return configReturn{"", nil}
}

func pathRelativeToAbsolute(configDirectory string) {
for _, value := range constants.ConfigParametersPath {
configValue := viper.GetString(value)
viper.Set(value, path.Join(configDirectory, configValue))
}
}

func getConfigFilePath() configReturn {
for _, value := range constants.ConfigPathOrder {
currentFilepath := path.Join(constants.ConfigBasepath, value, constants.ConfigFileName)
_, err := os.Stat(currentFilepath)
if err == nil {
return currentFilepath, nil
return configReturn{currentFilepath, nil}
}
}
return "", errors.New("getConfigFilePath: Configuration not found")
return configReturn{"", errors.New("getConfigFilePath: Configuration not found")}
}
34 changes: 17 additions & 17 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ const configNotExistsFileName = ".confignotexists"

func Test_getConfigFilePathShouldPassWithNoErrorsWhenFindConfigurationFile(t *testing.T) {
workingDirectory, _ := os.Getwd()
constants.DajeBasePath = workingDirectory + "/../../testdata"
constants.ConfigBasepath = workingDirectory + "/../../testdata"

_, err := getConfigFilePath()
result := getConfigFilePath()

if err != nil {
t.Fatalf("getConfigFilePath should pass with no errors when find configuration file: expected=\"\" - get=\"%v\"", err)
if result.Error != nil {
t.Fatalf("getConfigFilePath should pass with no errors when find configuration file: expected=\"\" - get=\"%v\"", result.Error)
}
}

func Test_getConfigFilePathShouldReturnTheExactPathWhenItFindTheConfigurationFile(t *testing.T) {
workingDirectory, _ := os.Getwd()
constants.DajeBasePath = workingDirectory + "/../../testdata"
expectedConfigPath := path.Join(constants.DajeBasePath, "../testdata/.config/daje", constants.DajeConfigFileName)
constants.ConfigBasepath = workingDirectory + "/../../testdata"
expectedConfigPath := path.Join(constants.ConfigBasepath, "../testdata/.config/daje", constants.ConfigFileName)

path, _ := getConfigFilePath()
result := getConfigFilePath()

if path != expectedConfigPath {
t.Fatalf("getConfigFilePath should return the exact path when find configuration file: expected=%v - get=%v", expectedConfigPath, path)
if result.Value != expectedConfigPath {
t.Fatalf("getConfigFilePath should return the exact path when find configuration file: expected=%v - get=%v", expectedConfigPath, result.Value)
}
}

func Test_getConfigFilePathShouldFailWhenDontFindConfigurationFile(t *testing.T) {
constants.DajeConfigFileName = configNotExistsFileName
constants.ConfigFileName = configNotExistsFileName

_, err := getConfigFilePath()
result := getConfigFilePath()
errExpected := errors.New("getConfigFilePath: Configuration not found")

if err.Error() != errExpected.Error() {
t.Fatalf("getConfigFilePath should fail when don't find configuration file: expected=%v - get=%v", errExpected, err)
if result.Error.Error() != errExpected.Error() {
t.Fatalf("getConfigFilePath should fail when don't find configuration file: expected=%v - get=%v", errExpected, result.Error)
}
}

func Test_getConfigFilePathShouldHaveEmptyPathWhenDontFindConfigurationFile(t *testing.T) {
constants.DajeConfigFileName = configNotExistsFileName
path, _ := getConfigFilePath()
constants.ConfigFileName = configNotExistsFileName
result := getConfigFilePath()

if path != "" {
t.Fatalf("getConfigFilePath should have empty path when don't find configuration file: expected=\"\" - get=%v", path)
if result.Value != "" {
t.Fatalf("getConfigFilePath should have empty path when don't find configuration file: expected=\"\" - get=%v", result.Value)
}
}
9 changes: 3 additions & 6 deletions internal/dotfiles/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ import (
"os"
"path"

"github.com/Schroedinger-Hat/Daje/constants"
"github.com/spf13/viper"
)

func InitializeDotfiles() error {
err := os.MkdirAll(path.Join(
constants.DajeBasePath,
viper.GetString("dotfiles_directory"),
), os.ModePerm)

err := os.MkdirAll(path.Join(viper.GetString("dotfiles.local")), os.ModePerm)
if err != nil {
errorMessage := "InitializeDotfiles->" + err.Error()
log.Fatal(errorMessage)
return errors.New(errorMessage)
}

// TODO: clone dotfiles.remote
return nil
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"log"
"os"

"github.com/Schroedinger-Hat/Daje/constants"
"github.com/Schroedinger-Hat/Daje/internal/config"
"github.com/Schroedinger-Hat/Daje/pkg/cmd/root"
"github.com/spf13/viper"
)
Expand All @@ -17,7 +19,11 @@ const (

func main() {
viper.SetConfigType("yaml")
viper.SetConfigName(constants.DajeConfigFileName)
viper.SetConfigName(constants.ConfigFileName)
if err := config.LoadConfig(); err != nil {
errorMessage := "main->" + err.Error()
log.Fatal(errorMessage)
}
code := mainRun()
os.Exit(int(code))
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/cmd/checkhealth/checkhealth.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package checkhealth

import (
"errors"
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/Schroedinger-Hat/Daje/constants"
"github.com/Schroedinger-Hat/Daje/internal/config"
)

func CmdCheckhealth() *cobra.Command {
Expand All @@ -24,17 +22,10 @@ func CmdCheckhealth() *cobra.Command {
}

func submitCmdCheckhealth() error {
err := config.LoadConfig()
if err != nil {
errorMessage := "Checkhealth->" + err.Error()
log.Fatal(errorMessage)
return errors.New(errorMessage)
}
log.Println("[Checkhealth]:[LoadConfig]", "Configuration Path:"+viper.GetViper().ConfigFileUsed())

log.Println("[Checkhealth]:[ConfigValues]")
for _, value := range constants.DajeConfigParameters {
log.Println(value + ": " + viper.GetString(value))
for _, value := range constants.ConfigParameters {
log.Println("[Checkhealth]:[LoadConfig]", value+": "+viper.GetString(value))
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions testdata/.config/daje/.dajerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dotfiles_directory: ../dotfiles
dotfiles_remote: https://github.com/Wabri/dotfiles
dotfiles:
local: ../dotfiles
remote: https://github.com/Wabri/dotfiles
5 changes: 3 additions & 2 deletions testdata/.dajerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dotfiles_directory: .config/dotfiles
dotfiles_remote: https://github.com/Wabri/dotfiles
dotfiles:
local: .config/dotfiles
remote: https://github.com/Wabri/dotfiles

0 comments on commit 61c3fce

Please sign in to comment.