mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
refactor: change config to be optional and create default state
This commit is contained in:
+1
-15
@@ -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()
|
var envVar = utils.Find_authorfile()
|
||||||
|
|
||||||
func setup() {
|
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
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-19
@@ -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 envVar string
|
||||||
var configVar string
|
|
||||||
|
|
||||||
func setup() {
|
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
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -73,7 +56,6 @@ func teardown() {
|
|||||||
// remove test data
|
// remove test data
|
||||||
os.Remove("author_file_test")
|
os.Remove("author_file_test")
|
||||||
os.Setenv("author_file", envVar)
|
os.Setenv("author_file", envVar)
|
||||||
os.Remove("test_config.toml")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyPress(tm *teatest.TestModel, key string) {
|
func keyPress(tm *teatest.TestModel, key string) {
|
||||||
|
|||||||
@@ -13,15 +13,45 @@ import (
|
|||||||
// that contains the names and emails of the users that are allowed to commit
|
// 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
|
// An example of the author file can be found in the examples folder of the repo
|
||||||
func Find_authorfile() string {
|
func Find_authorfile() string {
|
||||||
|
var file string
|
||||||
|
|
||||||
if os.Getenv("author_file") == "" {
|
if os.Getenv("author_file") == "" {
|
||||||
if ConfigVar == nil {
|
if ConfigVar == nil {
|
||||||
cfg, _ := LoadConfig()
|
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()
|
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 {
|
} else {
|
||||||
return os.Getenv("author_file")
|
return os.Getenv("author_file")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,12 +62,15 @@ func LoadConfig() (*Config, error) {
|
|||||||
// Try to read config
|
// Try to read config
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
if err := handleMissingConfig(v); err != nil {
|
return nil, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("config error: %w", err)
|
|
||||||
}
|
}
|
||||||
|
// 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
|
var cfg Config
|
||||||
|
|||||||
@@ -48,15 +48,17 @@ editor = "built-in"
|
|||||||
var envVar = os.Getenv("author_file")
|
var envVar = os.Getenv("author_file")
|
||||||
|
|
||||||
func setup() {
|
func setup() {
|
||||||
|
os.Setenv("author_file", "")
|
||||||
|
|
||||||
// setup test data
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
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.WriteFile("author_file_test", []byte(author_data), 0644)
|
||||||
os.Setenv("author_file", "author_file_test")
|
os.Setenv("author_file", "author_file_test")
|
||||||
}
|
}
|
||||||
@@ -65,7 +67,7 @@ func teardown() {
|
|||||||
// remove test data
|
// remove test data
|
||||||
os.Remove("author_file_test")
|
os.Remove("author_file_test")
|
||||||
os.Setenv("author_file", envVar)
|
os.Setenv("author_file", envVar)
|
||||||
os.Remove("test_config.toml")
|
os.Remove("config.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Author tests BEGIN
|
// Author tests BEGIN
|
||||||
@@ -140,16 +142,16 @@ func Test_CreateAuthor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_FindAuthorFilePanic(t *testing.T) {
|
func Test_FindAuthorFilePanic(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
// Save original environment variables
|
// Save original environment variables
|
||||||
originalAuthorFile := os.Getenv("author_file")
|
originalAuthorFile := os.Getenv("author_file")
|
||||||
originalHome := os.Getenv("HOME")
|
originalHome := os.Getenv("HOME")
|
||||||
orignalXDG := os.Getenv("XDG_CONFIG_HOME")
|
orignalXDG := os.Getenv("XDG_CONFIG_HOME")
|
||||||
originalAuthorFile_Config := utils.ConfigVar.Settings.AuthorFile
|
|
||||||
|
|
||||||
// Test Find_authorfile panic
|
// Test Find_authorfile panic
|
||||||
defer func() {
|
defer func() {
|
||||||
// Reset environment variables
|
// Reset environment variables
|
||||||
utils.ConfigVar.Settings.AuthorFile = originalAuthorFile_Config
|
|
||||||
os.Setenv("author_file", originalAuthorFile)
|
os.Setenv("author_file", originalAuthorFile)
|
||||||
os.Setenv("HOME", originalHome)
|
os.Setenv("HOME", originalHome)
|
||||||
os.Setenv("XDG_CONFIG_HOME", orignalXDG)
|
os.Setenv("XDG_CONFIG_HOME", orignalXDG)
|
||||||
@@ -161,7 +163,6 @@ func Test_FindAuthorFilePanic(t *testing.T) {
|
|||||||
|
|
||||||
// Set environment variables to empty strings
|
// Set environment variables to empty strings
|
||||||
// to trigger the panic
|
// to trigger the panic
|
||||||
utils.ConfigVar.Settings.AuthorFile = ""
|
|
||||||
os.Setenv("author_file", "")
|
os.Setenv("author_file", "")
|
||||||
os.Setenv("HOME", "")
|
os.Setenv("HOME", "")
|
||||||
os.Setenv("XDG_CONFIG_HOME", "")
|
os.Setenv("XDG_CONFIG_HOME", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user