mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 20:55:47 +00:00
refactor: change config to be optional and create default state
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user