diff --git a/Makefile b/Makefile index ffc3e30..72a6fc4 100644 --- a/Makefile +++ b/Makefile @@ -3,21 +3,21 @@ CURRENT_DIR := $(shell pwd) clean: @echo "\n----- Start CLEAN -----\n" @echo "# Remove all the daje metadata and builds..." - rm -r $(CURRENT_DIR)/bin &>/dev/null + rm -r $(CURRENT_DIR)/bin &2>/dev/null @echo "\n----- End CLEAN -----" # The version in this build has the form `|` build: clean @echo "\n----- Start BUILD -----\n" @echo "# Building daje for production environment..." - go build -ldflags "-X github.com/Schrodinger-Hat/Daje/constants.Version=$(shell git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || git rev-parse --short HEAD)" -o ./bin/daje . + go build -ldflags "-X github.com/Schroedinger-Hat/Daje/constants.Version=$(shell git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || git rev-parse --short HEAD)" -o ./bin/daje . @echo "\n----- End BUILD -----" # For debugging reason the version in this build has the form `++` build-test-dev: clean @echo "\n----- Start BUILD-TEST-DEV -----\n" @echo "# Building daje for development environment..." - go build -ldflags "-X github.com/Schrodinger-Hat/Daje/constants.DajeConfigBaseDir=$(CURRENT_DIR)/testdata -X github.com/Schrodinger-Hat/Daje/constants.Version=$(shell git branch --show-current)+$(shell git rev-parse --short origin/main)+$(shell git status --porcelain | wc -l)" -o ./bin/daje . + 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 . @echo "\n----- End BUILD-TEST-DEV -----" checkhealth: build-test-dev diff --git a/constants/contants.go b/constants/contants.go index c5220fe..910d44a 100644 --- a/constants/contants.go +++ b/constants/contants.go @@ -1,9 +1,15 @@ package constants -import "path" +var DajeBasePath = "~" +var DajeConfigFileName = ".dajerc" +var DajeConfigPathOrder = []string{ + ".config/daje/", + ".", +} -var DajeConfigBaseDir = "~/.config/" -var DajeDotfileName = "daje.conf" -var DajeDotfilePath = path.Join(DajeConfigBaseDir, DajeDotfileName) +var DajeConfigParameters = []string{ + "dotfiles_directory", + "dotfiles_remote", +} var Version = "" diff --git a/go.mod b/go.mod index af74985..5bb2a07 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,31 @@ -module github.com/Schrodinger-Hat/Daje +module github.com/Schroedinger-Hat/Daje go 1.18 -require github.com/spf13/cobra v1.8.0 +require ( + github.com/spf13/cobra v1.8.0 + github.com/spf13/viper v1.19.0 +) require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d0e8c2c..4966411 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,69 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +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/internal/config/config.go b/internal/config/config.go index 07df96e..f7f8b11 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,36 +1,41 @@ package config import ( + "errors" "log" "os" - "path/filepath" + "path" - "github.com/Schrodinger-Hat/Daje/constants" + "github.com/Schroedinger-Hat/Daje/constants" + "github.com/spf13/viper" ) -func InitEmptyDaje() error { - dajeConfigPath := filepath.Join(constants.DajeConfigBaseDir) - if err := os.Mkdir(dajeConfigPath, 0755); err != nil { - return err - } +func ExtractConfigParameter(elementName string) (string, error) { + return viper.GetString(elementName), nil +} - dajeConfigFile := filepath.Join(constants.DajeConfigBaseDir, constants.DajeDotfileName) - if _, err := os.Create(dajeConfigFile); err != nil { - return err +func LoadConfig() error { + configFilePath, err := getConfigFilePath() + if err != nil { + errorMessage := "LoadConfig:getConfigFilePath->" + err.Error() + log.Fatal(errorMessage) + return errors.New(errorMessage) + } + viper.SetConfigFile(configFilePath) + if err = viper.ReadInConfig(); err != nil { + errorMessage := "LoadConfig:SetConfigFile->" + err.Error() + log.Fatal(errorMessage) + return errors.New(errorMessage) } - return nil } -func IsDajeInitialized() bool { - dotFile := filepath.Join(constants.DajeConfigBaseDir, constants.DajeDotfileName) - if _, err := os.Stat(dotFile); err != nil { - if os.IsNotExist(err) { - return false +func getConfigFilePath() (string, error) { + for _, value := range constants.DajeConfigPathOrder { + currentFilepath := path.Join(constants.DajeBasePath, value, constants.DajeConfigFileName) + if _, err := os.Stat(currentFilepath); err == nil { + return currentFilepath, nil } - - log.Fatal(err) } - - return true + return "", errors.New("getConfigFilePath: Configuration not found") } diff --git a/internal/dotfiles/init.go b/internal/dotfiles/init.go new file mode 100644 index 0000000..02d52d0 --- /dev/null +++ b/internal/dotfiles/init.go @@ -0,0 +1,25 @@ +package dotfiles + +import ( + "errors" + "log" + "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) + + if err != nil { + errorMessage := "InitializeDotfiles->" + err.Error() + log.Fatal(errorMessage) + return errors.New(errorMessage) + } + return nil +} diff --git a/internal/tuning/tuning.go b/internal/tuning/tuning.go deleted file mode 100644 index 8c26411..0000000 --- a/internal/tuning/tuning.go +++ /dev/null @@ -1,6 +0,0 @@ -package tuning - -// TODO: when the first tuning options will be available we need something to prove that the system is tuned accordingly to the configuration -func IsSystemTuned() bool { - return true -} diff --git a/main.go b/main.go index 254ea12..5347506 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,9 @@ package main import ( "os" - "github.com/Schrodinger-Hat/Daje/pkg/cmd/root" + "github.com/Schroedinger-Hat/Daje/constants" + "github.com/Schroedinger-Hat/Daje/pkg/cmd/root" + "github.com/spf13/viper" ) type exitCode int @@ -14,6 +16,8 @@ const ( ) func main() { + viper.SetConfigType("yaml") + viper.SetConfigName(constants.DajeConfigFileName) code := mainRun() os.Exit(int(code)) } diff --git a/pkg/cmd/checkhealth/checkhealth.go b/pkg/cmd/checkhealth/checkhealth.go index d1eb47e..a706689 100644 --- a/pkg/cmd/checkhealth/checkhealth.go +++ b/pkg/cmd/checkhealth/checkhealth.go @@ -1,31 +1,41 @@ package checkhealth import ( - "fmt" + "errors" + "log" "github.com/spf13/cobra" + "github.com/spf13/viper" - "github.com/Schrodinger-Hat/Daje/constants" - "github.com/Schrodinger-Hat/Daje/internal/tuning" + "github.com/Schroedinger-Hat/Daje/constants" + "github.com/Schroedinger-Hat/Daje/internal/config" ) -func NewCmdCheckhealth() *cobra.Command { +func CmdCheckhealth() *cobra.Command { cmd := &cobra.Command{ Use: "checkhealth [flags]", Short: "Check Daje health", - RunE: func(cmd *cobra.Command, args []string) error { - return submitAction() + RunE: func(cmd *cobra.Command, args []string) error { //nolint:all + return submitCmdCheckhealth() }, } return cmd } -func submitAction() error { +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()) - fmt.Println("Version: ", constants.Version) - fmt.Println("Configuration path: ", constants.DajeDotfilePath) - fmt.Println("Tuning: ", tuning.IsSystemTuned()) + log.Println("[Checkhealth]:[ConfigValues]") + for _, value := range constants.DajeConfigParameters { + log.Println(value + ": " + viper.GetString(value)) + } return nil } diff --git a/pkg/cmd/init/init.go b/pkg/cmd/init/init.go index c5279d5..0e7048a 100644 --- a/pkg/cmd/init/init.go +++ b/pkg/cmd/init/init.go @@ -1,38 +1,42 @@ package init import ( - "fmt" + "errors" + "log" "github.com/spf13/cobra" - "github.com/Schrodinger-Hat/Daje/internal/config" + "github.com/Schroedinger-Hat/Daje/internal/config" + "github.com/Schroedinger-Hat/Daje/internal/dotfiles" ) -func NewCmdInit() *cobra.Command { +func CmdInit() *cobra.Command { cmd := &cobra.Command{ Use: "init [flags]", Short: "Initialize daje on your system", RunE: func(cmd *cobra.Command, args []string) error { //nolint:all - return submitAction() + return submitCmdInit() }, } return cmd } -func submitAction() error { - if !config.IsDajeInitialized() { - err := config.InitEmptyDaje() - if err != nil { - fmt.Println(err) - return nil - } - - fmt.Println("Daje has been initialized successfully!") - } else { - fmt.Println("Daje has been already initialized in the system.") - return nil +func submitCmdInit() error { + funcErrorHandle := func(err error) error { + errorMessage := "CmdInit->" + err.Error() + log.Fatal(errorMessage) + return errors.New(errorMessage) + } + functionOrder := []func() error{ + config.LoadConfig, + dotfiles.InitializeDotfiles, } + for _, function := range functionOrder { + if err := function(); err != nil { + return funcErrorHandle(err) + } + } return nil } diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 2068490..1599880 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -3,8 +3,8 @@ package root import ( "github.com/spf13/cobra" - "github.com/Schrodinger-Hat/Daje/pkg/cmd/checkhealth" - initCmd "github.com/Schrodinger-Hat/Daje/pkg/cmd/init" + "github.com/Schroedinger-Hat/Daje/pkg/cmd/checkhealth" + initCmd "github.com/Schroedinger-Hat/Daje/pkg/cmd/init" ) func NewCmdRoot() *cobra.Command { @@ -18,8 +18,8 @@ $ daje init `, } - cmd.AddCommand(initCmd.NewCmdInit()) - cmd.AddCommand(checkhealth.NewCmdCheckhealth()) + cmd.AddCommand(initCmd.CmdInit()) + cmd.AddCommand(checkhealth.CmdCheckhealth()) return cmd } diff --git a/testdata/.config/daje/.dajerc b/testdata/.config/daje/.dajerc new file mode 100644 index 0000000..344ba0d --- /dev/null +++ b/testdata/.config/daje/.dajerc @@ -0,0 +1,2 @@ +dotfiles_directory: ../dotfiles +dotfiles_remote: https://github.com/Wabri/dotfiles diff --git a/testdata/.dajerc b/testdata/.dajerc new file mode 100644 index 0000000..55feb50 --- /dev/null +++ b/testdata/.dajerc @@ -0,0 +1,2 @@ +dotfiles_directory: .config/dotfiles +dotfiles_remote: https://github.com/Wabri/dotfiles