refactor: change config to be optional and create default state

This commit is contained in:
Slug-Boi
2025-06-03 19:47:04 +02:00
parent 97dac5169a
commit ff9b8739cb
5 changed files with 53 additions and 51 deletions
+1 -15
View File
@@ -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)
}
+1 -19
View File
@@ -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) {
+33 -3
View File
@@ -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()
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")
}
+8 -5
View File
@@ -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
+8 -7
View File
@@ -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", "")