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
+35 -5
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()
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")
}