From cf5aeb6fba500ef5f79be16c54eddbaf19319d7f Mon Sep 17 00:00:00 2001 From: Fabian Siegel Date: Tue, 31 Aug 2021 17:10:13 +0200 Subject: [PATCH] config: add local configurations (#16) Closes (#16) --- cmd/root.go | 57 ++++++++++++++++++++++++++++++++++++++--------------- git/git.go | 10 ++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bbaf408..3aca2ce 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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()) + } diff --git a/git/git.go b/git/git.go index 54a86e1..8be561d 100644 --- a/git/git.go +++ b/git/git.go @@ -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]