aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bridge_configure.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-28 18:26:29 +0200
committerMichael Muré <batolettre@gmail.com>2020-06-28 18:26:29 +0200
commit26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch)
treef1fe939311c75bd615071e96f3d37822cccd77a7 /commands/bridge_configure.go
parentc0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff)
downloadgit-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz
commands: refactor to avoid globals
Diffstat (limited to 'commands/bridge_configure.go')
-rw-r--r--commands/bridge_configure.go216
1 files changed, 112 insertions, 104 deletions
diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go
index 89553633..83555b0c 100644
--- a/commands/bridge_configure.go
+++ b/commands/bridge_configure.go
@@ -17,79 +17,161 @@ import (
"github.com/MichaelMure/git-bug/util/interrupt"
)
-const (
- defaultName = "default"
-)
+type bridgeConfigureOptions struct {
+ name string
+ target string
+ params core.BridgeParams
+ token string
+ tokenStdin bool
+}
-var (
- bridgeConfigureName string
- bridgeConfigureTarget string
- bridgeConfigureParams core.BridgeParams
- bridgeConfigureToken string
- bridgeConfigureTokenStdin bool
-)
+func newBridgeConfigureCommand() *cobra.Command {
+ env := newEnv()
+ options := bridgeConfigureOptions{}
+
+ cmd := &cobra.Command{
+ Use: "configure",
+ Short: "Configure a new bridge.",
+ Long: ` Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge.`,
+ Example: `# Interactive example
+[1]: github
+[2]: gitlab
+[3]: jira
+[4]: launchpad-preview
+
+target: 1
+name [default]: default
+
+Detected projects:
+[1]: github.com/a-hilaly/git-bug
+[2]: github.com/MichaelMure/git-bug
+
+[0]: Another project
+
+Select option: 1
+
+[1]: user provided token
+[2]: interactive token creation
+Select option: 1
+
+You can generate a new token by visiting https://github.com/settings/tokens.
+Choose 'Generate new token' and set the necessary access scope for your repository.
+
+The access scope depend on the type of repository.
+Public:
+ - 'public_repo': to be able to read public repositories
+Private:
+ - 'repo' : to be able to read private repositories
+
+Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
+Successfully configured bridge: default
+
+# For GitHub
+git bug bridge configure \
+ --name=default \
+ --target=github \
+ --owner=$(OWNER) \
+ --project=$(PROJECT) \
+ --token=$(TOKEN)
+
+# For Launchpad
+git bug bridge configure \
+ --name=default \
+ --target=launchpad-preview \
+ --url=https://bugs.launchpad.net/ubuntu/
-func runBridgeConfigure(cmd *cobra.Command, args []string) error {
- backend, err := cache.NewRepoCache(repo)
+# For Gitlab
+git bug bridge configure \
+ --name=default \
+ --target=github \
+ --url=https://github.com/michaelmure/git-bug \
+ --token=$(TOKEN)`,
+ PreRunE: loadRepo(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runBridgeConfigure(env, options)
+ },
+ }
+
+ flags := cmd.Flags()
+ flags.SortFlags = false
+
+ flags.StringVarP(&options.name, "name", "n", "", "A distinctive name to identify the bridge")
+ flags.StringVarP(&options.target, "target", "t", "",
+ fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
+ flags.StringVarP(&options.params.URL, "url", "u", "", "The URL of the remote repository")
+ flags.StringVarP(&options.params.BaseURL, "base-url", "b", "", "The base URL of your remote issue tracker")
+ flags.StringVarP(&options.params.Login, "login", "l", "", "The login on your remote issue tracker")
+ flags.StringVarP(&options.params.CredPrefix, "credential", "c", "", "The identifier or prefix of an already known credential for your remote issue tracker (see \"git-bug bridge auth\")")
+ flags.StringVar(&options.token, "token", "", "A raw authentication token for the remote issue tracker")
+ flags.BoolVar(&options.tokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token")
+ flags.StringVarP(&options.params.Owner, "owner", "o", "", "The owner of the remote repository")
+ flags.StringVarP(&options.params.Project, "project", "p", "", "The name of the remote repository")
+
+ return cmd
+}
+
+func runBridgeConfigure(env *Env, opts bridgeConfigureOptions) error {
+ backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- if (bridgeConfigureTokenStdin || bridgeConfigureToken != "" || bridgeConfigureParams.CredPrefix != "") &&
- (bridgeConfigureName == "" || bridgeConfigureTarget == "") {
+ if (opts.tokenStdin || opts.token != "" || opts.params.CredPrefix != "") &&
+ (opts.name == "" || opts.target == "") {
return fmt.Errorf("you must provide a bridge name and target to configure a bridge with a credential")
}
// early fail
- if bridgeConfigureParams.CredPrefix != "" {
- if _, err := auth.LoadWithPrefix(repo, bridgeConfigureParams.CredPrefix); err != nil {
+ if opts.params.CredPrefix != "" {
+ if _, err := auth.LoadWithPrefix(env.repo, opts.params.CredPrefix); err != nil {
return err
}
}
switch {
- case bridgeConfigureTokenStdin:
+ case opts.tokenStdin:
reader := bufio.NewReader(os.Stdin)
token, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("reading from stdin: %v", err)
}
- bridgeConfigureParams.TokenRaw = strings.TrimSpace(token)
- case bridgeConfigureToken != "":
- bridgeConfigureParams.TokenRaw = bridgeConfigureToken
+ opts.params.TokenRaw = strings.TrimSpace(token)
+ case opts.token != "":
+ opts.params.TokenRaw = opts.token
}
- if bridgeConfigureTarget == "" {
- bridgeConfigureTarget, err = promptTarget()
+ if opts.target == "" {
+ opts.target, err = promptTarget()
if err != nil {
return err
}
}
- if bridgeConfigureName == "" {
- bridgeConfigureName, err = promptName(repo)
+ if opts.name == "" {
+ opts.name, err = promptName(env.repo)
if err != nil {
return err
}
}
- b, err := bridge.NewBridge(backend, bridgeConfigureTarget, bridgeConfigureName)
+ b, err := bridge.NewBridge(backend, opts.target, opts.name)
if err != nil {
return err
}
- err = b.Configure(bridgeConfigureParams)
+ err = b.Configure(opts.params)
if err != nil {
return err
}
- fmt.Printf("Successfully configured bridge: %s\n", bridgeConfigureName)
+ env.out.Printf("Successfully configured bridge: %s\n", opts.name)
return nil
}
func promptTarget() (string, error) {
+ // TODO: use the reusable prompt from the input package
targets := bridge.Targets()
for {
@@ -117,6 +199,9 @@ func promptTarget() (string, error) {
}
func promptName(repo repository.RepoConfig) (string, error) {
+ // TODO: use the reusable prompt from the input package
+ const defaultName = "default"
+
defaultExist := core.BridgeExist(repo, defaultName)
for {
@@ -149,80 +234,3 @@ func promptName(repo repository.RepoConfig) (string, error) {
fmt.Println("a bridge with the same name already exist")
}
}
-
-var bridgeConfigureCmd = &cobra.Command{
- Use: "configure",
- Short: "Configure a new bridge.",
- Long: ` Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge.`,
- Example: `# Interactive example
-[1]: github
-[2]: gitlab
-[3]: jira
-[4]: launchpad-preview
-
-target: 1
-name [default]: default
-
-Detected projects:
-[1]: github.com/a-hilaly/git-bug
-[2]: github.com/MichaelMure/git-bug
-
-[0]: Another project
-
-Select option: 1
-
-[1]: user provided token
-[2]: interactive token creation
-Select option: 1
-
-You can generate a new token by visiting https://github.com/settings/tokens.
-Choose 'Generate new token' and set the necessary access scope for your repository.
-
-The access scope depend on the type of repository.
-Public:
- - 'public_repo': to be able to read public repositories
-Private:
- - 'repo' : to be able to read private repositories
-
-Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
-Successfully configured bridge: default
-
-# For GitHub
-git bug bridge configure \
- --name=default \
- --target=github \
- --owner=$(OWNER) \
- --project=$(PROJECT) \
- --token=$(TOKEN)
-
-# For Launchpad
-git bug bridge configure \
- --name=default \
- --target=launchpad-preview \
- --url=https://bugs.launchpad.net/ubuntu/
-
-# For Gitlab
-git bug bridge configure \
- --name=default \
- --target=github \
- --url=https://github.com/michaelmure/git-bug \
- --token=$(TOKEN)`,
- PreRunE: loadRepo,
- RunE: runBridgeConfigure,
-}
-
-func init() {
- bridgeCmd.AddCommand(bridgeConfigureCmd)
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
- fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.URL, "url", "u", "", "The URL of the remote repository")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.BaseURL, "base-url", "b", "", "The base URL of your remote issue tracker")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.Login, "login", "l", "", "The login on your remote issue tracker")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.CredPrefix, "credential", "c", "", "The identifier or prefix of an already known credential for your remote issue tracker (see \"git-bug bridge auth\")")
- bridgeConfigureCmd.Flags().StringVar(&bridgeConfigureToken, "token", "", "A raw authentication token for the remote issue tracker")
- bridgeConfigureCmd.Flags().BoolVar(&bridgeConfigureTokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.Owner, "owner", "o", "", "The owner of the remote repository")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.Project, "project", "p", "", "The name of the remote repository")
- bridgeConfigureCmd.Flags().SortFlags = false
-}