From ff9b8739cb284635272ddad9ed09e2423b9623cf Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Tue, 3 Jun 2025 19:47:04 +0200 Subject: [PATCH] refactor: change config to be optional and create default state --- src/cmd/cmd_test.go | 16 +----------- src/cmd/tui/tui_test.go | 20 +-------------- src/cmd/utils/author_file_utils.go | 40 ++++++++++++++++++++++++++---- src/cmd/utils/config.go | 13 ++++++---- src/cmd/utils/util_test.go | 15 +++++------ 5 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/cmd/cmd_test.go b/src/cmd/cmd_test.go index b11dfa5..825d413 100644 --- a/src/cmd/cmd_test.go +++ b/src/cmd/cmd_test.go @@ -34,25 +34,11 @@ const author_data = ` } }` -const config_data = `[settings] -author_file = "author_file_test" -starting_scope = "git" -editor = "built-in" -` - var envVar = utils.Find_authorfile() func setup() { - // setup config file - err := os.WriteFile("config.toml", []byte(config_data), 0644) - if err != nil { - panic(err) - } - os.Setenv("COCOMMIT_CONFIG", "config.toml") - utils.Find_authorfile() - // setup test data - err = os.WriteFile("author_file_test", []byte(author_data), 0644) + err := os.WriteFile("author_file_test", []byte(author_data), 0644) if err != nil { panic(err) } diff --git a/src/cmd/tui/tui_test.go b/src/cmd/tui/tui_test.go index 3ec66be..4059476 100644 --- a/src/cmd/tui/tui_test.go +++ b/src/cmd/tui/tui_test.go @@ -38,28 +38,11 @@ const author_data = ` } }` -const config_data = `[settings] -author_file = "author_file_test" -starting_scope = "git" -editor = "built-in" -` - var envVar string -var configVar string func setup() { - err := os.WriteFile("test_config.toml", []byte(config_data), 0644) - if err != nil { - panic(err) - } - - os.Setenv("COCOMMIT_CONFIG", "test_config.toml") - configVar = os.Getenv("COCOMMIT_CONFIG") - utils.Find_authorfile() - - // setup test data - err = os.WriteFile("author_file_test", []byte(author_data), 0644) + err := os.WriteFile("author_file_test", []byte(author_data), 0644) if err != nil { panic(err) } @@ -73,7 +56,6 @@ func teardown() { // remove test data os.Remove("author_file_test") os.Setenv("author_file", envVar) - os.Remove("test_config.toml") } func keyPress(tm *teatest.TestModel, key string) { diff --git a/src/cmd/utils/author_file_utils.go b/src/cmd/utils/author_file_utils.go index 58bee17..e8ebe05 100644 --- a/src/cmd/utils/author_file_utils.go +++ b/src/cmd/utils/author_file_utils.go @@ -13,15 +13,45 @@ import ( // that contains the names and emails of the users that are allowed to commit // An example of the author file can be found in the examples folder of the repo func Find_authorfile() string { + var file string + if os.Getenv("author_file") == "" { if ConfigVar == nil { cfg, _ := LoadConfig() - cfg.SetGlobalConfig() + if cfg == nil { + // mimic the default config structure + cfg = &Config{ + Settings: struct { + AuthorFile string `mapstructure:"author_file"` + StartingScope string `mapstructure:"starting_scope"` + Editor string `mapstructure:"editor"` + }{ + AuthorFile: "", + StartingScope: "git", + Editor: "built-in", + }, + } + cfg.SetGlobalConfig() + } } - if ConfigVar.Settings.AuthorFile == "" { - panic("No author file found, please set the author_file in the config file or set the environment variable 'author_file'") - } - return ConfigVar.Settings.AuthorFile + + if ConfigVar.Settings.AuthorFile != "" { + file = ConfigVar.Settings.AuthorFile + } else if os.Getenv("author_file") != "" { + file = os.Getenv("author_file") + } else { + userconf, err :=os.UserConfigDir() + if err != nil { + panic(fmt.Sprintf("Error getting user config dir: %v", err)) + } + + if _, err := os.Stat(userconf+"/cocommit/authors.json"); os.IsNotExist(err) { + panic(fmt.Sprintf("No author file set, please set the author_file environment variable or create a config file using the command: cocommit config -c")) + } else { + file = userconf + "/cocommit/authors.json" + } + } + return file } else { return os.Getenv("author_file") } diff --git a/src/cmd/utils/config.go b/src/cmd/utils/config.go index 8ae52e8..d151e2d 100644 --- a/src/cmd/utils/config.go +++ b/src/cmd/utils/config.go @@ -62,12 +62,15 @@ func LoadConfig() (*Config, error) { // Try to read config if err := v.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { - if err := handleMissingConfig(v); err != nil { - return nil, err - } - } else { - return nil, fmt.Errorf("config error: %w", err) + return nil, nil } + // if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // if err := handleMissingConfig(v); err != nil { + // return nil, err + // } + // } else { + // return nil, fmt.Errorf("config error: %w", err) + // } } var cfg Config diff --git a/src/cmd/utils/util_test.go b/src/cmd/utils/util_test.go index 8b392c4..4a624d9 100644 --- a/src/cmd/utils/util_test.go +++ b/src/cmd/utils/util_test.go @@ -48,15 +48,17 @@ editor = "built-in" var envVar = os.Getenv("author_file") func setup() { + os.Setenv("author_file", "") + // setup test data - err := os.WriteFile("test_config.toml", []byte(config_data), 0644) + err := os.WriteFile("config.toml", []byte(config_data), 0644) if err != nil { panic(err) } - os.Setenv("COCOMMIT_CONFIG", "test_config.toml") + os.Setenv("COCOMMIT_CONFIG", "config.toml") - utils.Find_authorfile() + os.WriteFile("author_file_test", []byte(author_data), 0644) os.Setenv("author_file", "author_file_test") } @@ -65,7 +67,7 @@ func teardown() { // remove test data os.Remove("author_file_test") os.Setenv("author_file", envVar) - os.Remove("test_config.toml") + os.Remove("config.toml") } // Author tests BEGIN @@ -140,16 +142,16 @@ func Test_CreateAuthor(t *testing.T) { } func Test_FindAuthorFilePanic(t *testing.T) { + setup() + defer teardown() // Save original environment variables originalAuthorFile := os.Getenv("author_file") originalHome := os.Getenv("HOME") orignalXDG := os.Getenv("XDG_CONFIG_HOME") - originalAuthorFile_Config := utils.ConfigVar.Settings.AuthorFile // Test Find_authorfile panic defer func() { // Reset environment variables - utils.ConfigVar.Settings.AuthorFile = originalAuthorFile_Config os.Setenv("author_file", originalAuthorFile) os.Setenv("HOME", originalHome) os.Setenv("XDG_CONFIG_HOME", orignalXDG) @@ -161,7 +163,6 @@ func Test_FindAuthorFilePanic(t *testing.T) { // Set environment variables to empty strings // to trigger the panic - utils.ConfigVar.Settings.AuthorFile = "" os.Setenv("author_file", "") os.Setenv("HOME", "") os.Setenv("XDG_CONFIG_HOME", "")