Skip to content

Commit

Permalink
config: add local configurations (#16)
Browse files Browse the repository at this point in the history
Closes (#16)
  • Loading branch information
craftamap committed Sep 8, 2021
1 parent 7b8aba4 commit 8042fb3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
57 changes: 41 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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]
Expand Down

0 comments on commit 8042fb3

Please sign in to comment.