Skip to content

Commit

Permalink
config: use constants instead of magic strings for config keys (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Sep 8, 2021
1 parent 568d4e0 commit 84a70f1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
12 changes: 6 additions & 6 deletions cmd/commands/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func Add(authCmd *cobra.Command, globalOpts *options.GlobalOptions) {
tmpVp.SetConfigFile(path)
tmpVp.ReadInConfig()

oldPw := tmpVp.GetString("password")
oldPw := tmpVp.GetString(config.CONFIG_KEY_AUTH_PASSWORD)

if oldPw != "" {
logging.Warning("You are already logged in as ", tmpVp.GetString("username"))
logging.Warning("You are already logged in as ", tmpVp.GetString(config.CONFIG_KEY_AUTH_USERNAME))
cont := false
err := survey.AskOne(&survey.Confirm{Message: "Do you want to overwrite this?"}, &cont)
if err != nil {
Expand Down Expand Up @@ -70,19 +70,19 @@ func Add(authCmd *cobra.Command, globalOpts *options.GlobalOptions) {
logging.Error(err)
return
}
username, err := config.BbConfigurationValidation.ValidateEntry("username", answers.Username)
username, err := config.BbConfigurationValidation.ValidateEntry(config.CONFIG_KEY_AUTH_USERNAME, answers.Username)
if err != nil {
logging.Error(err)
return
}
password, err := config.BbConfigurationValidation.ValidateEntry("password", answers.Password)
password, err := config.BbConfigurationValidation.ValidateEntry(config.CONFIG_KEY_AUTH_PASSWORD, answers.Password)
if err != nil {
logging.Error(err)
return
}

tmpVp.Set("username", username)
tmpVp.Set("password", password)
tmpVp.Set(config.CONFIG_KEY_AUTH_USERNAME, username)
tmpVp.Set(config.CONFIG_KEY_AUTH_PASSWORD, password)

err = tmpVp.WriteConfig()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/commands/pr/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cli/cli/utils"
"github.com/cli/safeexec"
"github.com/craftamap/bb/cmd/options"
"github.com/craftamap/bb/config"
"github.com/craftamap/bb/internal/run"
"github.com/craftamap/bb/util/logging"
"github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -38,7 +39,7 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
},
PreRunE: func(cmd *cobra.Command, _ []string) error {
// In order to check if the method exists in the config, we need to check here
syncMethodIsSet := viper.IsSet("sync-method")
syncMethodIsSet := viper.IsSet(config.CONFIG_KEY_PR_SYNC_SYNC_METHOD)
if !syncMethodIsSet {
logging.Note(
"You can configure your preferred way of syncing by adding the following line to your configuration: ",
Expand All @@ -48,7 +49,7 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
)
}
if syncMethodIsSet && !cmd.Flags().Lookup("method").Changed {
Method = viper.GetString("sync-method")
Method = viper.GetString(config.CONFIG_KEY_PR_SYNC_SYNC_METHOD)
}

if Method != MethodOptionRebase && Method != MethodOptionMerge {
Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/repo/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Add(repoCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Run: func(cmd *cobra.Command, args []string) {
c := globalOpts.Client

gitProtocol := viper.GetString("git_protocol")
gitProtocol := viper.GetString(config.CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL)
if gitProtocol == "" || (gitProtocol != "ssh" && gitProtocol != "https") {
err := survey.AskOne(&survey.Select{
Message: "Please select a prefered protocol of cloning repositories",
Expand All @@ -45,14 +45,14 @@ func Add(repoCmd *cobra.Command, globalOpts *options.GlobalOptions) {
tmpVp.SetConfigFile(path)
tmpVp.ReadInConfig()

gitProtocolI, err := config.BbConfigurationValidation.ValidateEntry("git_protocol", gitProtocol)
gitProtocolI, err := config.BbConfigurationValidation.ValidateEntry(config.CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL, gitProtocol)
if err != nil {
logging.Error(err)
return
}
gitProtocol = gitProtocolI.(string)

tmpVp.Set("git_protocol", gitProtocol)
tmpVp.Set(config.CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL, gitProtocol)
tmpVp.WriteConfig()
}

Expand Down
23 changes: 16 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/craftamap/bb/cmd/commands/pr"
"github.com/craftamap/bb/cmd/commands/repo"
"github.com/craftamap/bb/cmd/options"
configuration "github.com/craftamap/bb/config"
bbgit "github.com/craftamap/bb/git"
"github.com/kirsle/configdir"
"github.com/logrusorgru/aurora"
Expand All @@ -34,9 +35,9 @@ var (
Long: "Work seamlessly with Bitbucket.org from the command line.",
Example: `$ bb pr list`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
username := viper.GetString("username")
password := viper.GetString("password")
remoteName := viper.GetString("remote")
username := viper.GetString(configuration.CONFIG_KEY_AUTH_USERNAME)
password := viper.GetString(configuration.CONFIG_KEY_AUTH_PASSWORD)
remoteName := viper.GetString(configuration.CONFIG_KEY_GIT_REMOTE)

if _, ok := cmd.Annotations["RequiresRepository"]; ok {
bbrepo, err := bbgit.GetBitbucketRepo(remoteName)
Expand Down Expand Up @@ -87,21 +88,21 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/bb)")
rootCmd.PersistentFlags().StringVar(&username, "username", "", "username")
rootCmd.PersistentFlags().StringVar(&password, "password", "", "app password")
rootCmd.PersistentFlags().StringVar(&remoteName, "remote", "", "if you are in a repository and don't want to interact with the default origin, you can change it")
rootCmd.PersistentFlags().StringVar(&remoteName, "remote", "origin", "if you are in a repository and don't want to interact with the default remote, you can change it")
rootCmd.PersistentFlags().BoolVar(&logging.PrintDebugLogs, "debug", false, "enabling this flag allows debug logs to be printed")

err := viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
err := viper.BindPFlag(configuration.CONFIG_KEY_AUTH_USERNAME, rootCmd.PersistentFlags().Lookup("username"))
if err != nil {
logging.Error(err)
return
}
err = viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
err = viper.BindPFlag(configuration.CONFIG_KEY_AUTH_PASSWORD, rootCmd.PersistentFlags().Lookup("password"))
if err != nil {
logging.Error(err)
return
}

err = viper.BindPFlag("remote", rootCmd.PersistentFlags().Lookup("remote"))
err = viper.BindPFlag(configuration.CONFIG_KEY_GIT_REMOTE, rootCmd.PersistentFlags().Lookup("remote"))
if err != nil {
logging.Error(err)
return
Expand Down Expand Up @@ -177,6 +178,14 @@ func initConfig() {
}
}
}

// Register Aliases for backward compability
viper.RegisterAlias("username", configuration.CONFIG_KEY_AUTH_USERNAME)
viper.RegisterAlias("password", configuration.CONFIG_KEY_AUTH_PASSWORD)
viper.RegisterAlias("remote", configuration.CONFIG_KEY_GIT_REMOTE)
viper.RegisterAlias("git_protocol", configuration.CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL)
viper.RegisterAlias("sync-method", configuration.CONFIG_KEY_PR_SYNC_SYNC_METHOD)

logging.Debugf("%+v", viper.AllSettings())

}
19 changes: 14 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,29 @@ func (c Configuration) ValidateEntry(key string, value interface{}) (interface{}
return e.Validator(value)
}

const (
CONFIG_KEY_AUTH_USERNAME = "auth.username"
CONFIG_KEY_AUTH_PASSWORD = "auth.password"
CONFIG_KEY_GIT_REMOTE = "git.remote"
CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL = "repo.clone.git_protocol"
CONFIG_KEY_PR_SYNC_SYNC_METHOD = "pr.sync.sync_method"
)


var BbConfigurationValidation Configuration = map[string]Entry{
"username": {
CONFIG_KEY_AUTH_USERNAME: {
Validator: SimpleStringValidator(),
},
"password": {
CONFIG_KEY_AUTH_PASSWORD: {
Validator: SimpleStringValidator(),
},
"remote": {
CONFIG_KEY_GIT_REMOTE: {
Validator: SimpleStringValidator(),
},
"git_protocol": {
CONFIG_KEY_REPO_CLONE_GIT_PROTOCOL: {
Validator: EnumValidator("ssh", "https"),
},
"sync-method": {
CONFIG_KEY_PR_SYNC_SYNC_METHOD: {
Validator: EnumValidator("merge", "rebase"),
},
}

0 comments on commit 84a70f1

Please sign in to comment.