Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve configuration system #21

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Next Next commit
config: add local configurations (#16)
Closes (#16)
craftamap committed Sep 8, 2021
commit 8042fb3c0f7c3c0deb1075dcbc8094beea82680f
57 changes: 41 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -119,30 +119,55 @@ func init() {
}

func initConfig() {
if cfgFile == "" {
configDir := configdir.LocalConfig("bb")
err := configdir.MakePath(configDir)
viper.SetEnvPrefix("bb")
viper.AutomaticEnv()

// We support setting the config file manually by running bb with --config.
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
}
} else {
// create global config directory, first
configDirectory := configdir.LocalConfig("bb")
err := configdir.MakePath(configDirectory)
if err != nil {
panic(err)
}
cfgFile = filepath.Join(configDir, "configuration.toml")
if _, err = os.Stat(cfgFile); os.IsNotExist(err) {
fh, err := os.Create(cfgFile)
globalConfigFilePath := filepath.Join(configDirectory, "configuration.toml")
// create global config directory, first
if _, err = os.Stat(globalConfigFilePath); os.IsNotExist(err) {
fh, err := os.Create(globalConfigFilePath)
if err != nil {
panic(err)
}
defer fh.Close()
}
}

viper.SetConfigFile(cfgFile)

viper.SetEnvPrefix("bb")
viper.AutomaticEnv()

err := viper.ReadInConfig()
viper.SetConfigType("toml")
viper.SetConfigName("configuration.toml")
viper.AddConfigPath(configDirectory)
err = viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
}

if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
// also read in local configuration
if repoPath, err := bbgit.RepoPath(); err == nil {
// the local configuration can be found in the root of a repository
// If we in a repository, check for the file
if _, err = os.Stat(filepath.Join(repoPath, ".bb")); err == nil {
viper.SetConfigType("toml")
viper.SetConfigName(".bb")
viper.AddConfigPath(repoPath)
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
}
}
}
}
logging.Debugf("%+v", viper.AllSettings())

}
10 changes: 10 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -62,6 +62,16 @@ func CurrentHead() (string, error) {
output, err := run.PrepareCmd(headCmd).Output()
return firstLine(output), err
}

func RepoPath() (string, error) {
pathCmd, err := git.GitCommand("rev-parse", "--show-toplevel")
if err != nil {
return "", err
}
output, err := run.PrepareCmd(pathCmd).Output()
return firstLine(output), err
}

func firstLine(output []byte) string {
if i := bytes.IndexAny(output, "\n"); i >= 0 {
return string(output)[0:i]