1package commands
2
3import (
4 "bufio"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9
10 "github.com/spf13/cobra"
11
12 "github.com/MichaelMure/git-bug/bridge"
13 "github.com/MichaelMure/git-bug/bridge/core"
14 "github.com/MichaelMure/git-bug/bridge/core/auth"
15 "github.com/MichaelMure/git-bug/repository"
16)
17
18type bridgeConfigureOptions struct {
19 name string
20 target string
21 params core.BridgeParams
22 token string
23 tokenStdin bool
24 nonInteractive bool
25}
26
27func newBridgeConfigureCommand() *cobra.Command {
28 env := newEnv()
29 options := bridgeConfigureOptions{}
30
31 cmd := &cobra.Command{
32 Use: "configure",
33 Short: "Configure a new bridge.",
34 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.`,
35 Example: `# Interactive example
36[1]: github
37[2]: gitlab
38[3]: jira
39[4]: launchpad-preview
40
41target: 1
42name [default]: default
43
44Detected projects:
45[1]: github.com/a-hilaly/git-bug
46[2]: github.com/MichaelMure/git-bug
47
48[0]: Another project
49
50Select option: 1
51
52[1]: user provided token
53[2]: interactive token creation
54Select option: 1
55
56You can generate a new token by visiting https://github.com/settings/tokens.
57Choose 'Generate new token' and set the necessary access scope for your repository.
58
59The access scope depend on the type of repository.
60Public:
61 - 'public_repo': to be able to read public repositories
62Private:
63 - 'repo' : to be able to read private repositories
64
65Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
66Successfully configured bridge: default
67
68# For GitHub
69git bug bridge configure \
70 --name=default \
71 --target=github \
72 --owner=$(OWNER) \
73 --project=$(PROJECT) \
74 --token=$(TOKEN)
75
76# For Launchpad
77git bug bridge configure \
78 --name=default \
79 --target=launchpad-preview \
80 --url=https://bugs.launchpad.net/ubuntu/
81
82# For Gitlab
83git bug bridge configure \
84 --name=default \
85 --target=github \
86 --url=https://github.com/michaelmure/git-bug \
87 --token=$(TOKEN)`,
88 PreRunE: loadBackend(env),
89 RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
90 return runBridgeConfigure(env, options)
91 }),
92 }
93
94 flags := cmd.Flags()
95 flags.SortFlags = false
96
97 flags.StringVarP(&options.name, "name", "n", "", "A distinctive name to identify the bridge")
98 flags.StringVarP(&options.target, "target", "t", "",
99 fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
100 flags.StringVarP(&options.params.URL, "url", "u", "", "The URL of the remote repository")
101 flags.StringVarP(&options.params.BaseURL, "base-url", "b", "", "The base URL of your remote issue tracker")
102 flags.StringVarP(&options.params.Login, "login", "l", "", "The login on your remote issue tracker")
103 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\")")
104 flags.StringVar(&options.token, "token", "", "A raw authentication token for the remote issue tracker")
105 flags.BoolVar(&options.tokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token")
106 flags.StringVarP(&options.params.Owner, "owner", "o", "", "The owner of the remote repository")
107 flags.StringVarP(&options.params.Project, "project", "p", "", "The name of the remote repository")
108 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
109
110 return cmd
111}
112
113func runBridgeConfigure(env *Env, opts bridgeConfigureOptions) error {
114 var err error
115
116 if (opts.tokenStdin || opts.token != "" || opts.params.CredPrefix != "") &&
117 (opts.name == "" || opts.target == "") {
118 return fmt.Errorf("you must provide a bridge name and target to configure a bridge with a credential")
119 }
120
121 // early fail
122 if opts.params.CredPrefix != "" {
123 if _, err := auth.LoadWithPrefix(env.repo, opts.params.CredPrefix); err != nil {
124 return err
125 }
126 }
127
128 switch {
129 case opts.tokenStdin:
130 reader := bufio.NewReader(os.Stdin)
131 token, err := reader.ReadString('\n')
132 if err != nil {
133 return fmt.Errorf("reading from stdin: %v", err)
134 }
135 opts.params.TokenRaw = strings.TrimSpace(token)
136 case opts.token != "":
137 opts.params.TokenRaw = opts.token
138 }
139
140 if !opts.nonInteractive && opts.target == "" {
141 opts.target, err = promptTarget()
142 if err != nil {
143 return err
144 }
145 }
146
147 if !opts.nonInteractive && opts.name == "" {
148 opts.name, err = promptName(env.repo)
149 if err != nil {
150 return err
151 }
152 }
153
154 b, err := bridge.NewBridge(env.backend, opts.target, opts.name)
155 if err != nil {
156 return err
157 }
158
159 err = b.Configure(opts.params, !opts.nonInteractive)
160 if err != nil {
161 return err
162 }
163
164 env.out.Printf("Successfully configured bridge: %s\n", opts.name)
165 return nil
166}
167
168func promptTarget() (string, error) {
169 // TODO: use the reusable prompt from the input package
170 targets := bridge.Targets()
171
172 for {
173 for i, target := range targets {
174 fmt.Printf("[%d]: %s\n", i+1, target)
175 }
176 fmt.Printf("target: ")
177
178 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
179
180 if err != nil {
181 return "", err
182 }
183
184 line = strings.TrimSpace(line)
185
186 index, err := strconv.Atoi(line)
187 if err != nil || index <= 0 || index > len(targets) {
188 fmt.Println("invalid input")
189 continue
190 }
191
192 return targets[index-1], nil
193 }
194}
195
196func promptName(repo repository.RepoConfig) (string, error) {
197 // TODO: use the reusable prompt from the input package
198 const defaultName = "default"
199
200 defaultExist := core.BridgeExist(repo, defaultName)
201
202 for {
203 if defaultExist {
204 fmt.Printf("name: ")
205 } else {
206 fmt.Printf("name [%s]: ", defaultName)
207 }
208
209 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
210 if err != nil {
211 return "", err
212 }
213
214 line = strings.TrimSpace(line)
215
216 name := line
217 if defaultExist && name == "" {
218 continue
219 }
220
221 if name == "" {
222 name = defaultName
223 }
224
225 if !core.BridgeExist(repo, name) {
226 return name, nil
227 }
228
229 fmt.Println("a bridge with the same name already exist")
230 }
231}