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