1package commands
2
3import (
4 "bufio"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9
10 "github.com/MichaelMure/git-bug/bridge/core"
11
12 "github.com/MichaelMure/git-bug/bridge"
13 "github.com/MichaelMure/git-bug/cache"
14 "github.com/MichaelMure/git-bug/util/interrupt"
15 "github.com/spf13/cobra"
16)
17
18const (
19 defaultName = "default"
20)
21
22var (
23 bridgeConfigureName string
24 bridgeConfigureTarget string
25 bridgeParams core.BridgeParams
26)
27
28func runBridgeConfigure(cmd *cobra.Command, args []string) error {
29 backend, err := cache.NewRepoCache(repo)
30 if err != nil {
31 return err
32 }
33 defer backend.Close()
34 interrupt.RegisterCleaner(backend.Close)
35
36 if bridgeConfigureTarget == "" {
37 bridgeConfigureTarget, err = promptTarget()
38 if err != nil {
39 return err
40 }
41 }
42
43 if bridgeConfigureName == "" {
44 bridgeConfigureName, err = promptName()
45 if err != nil {
46 return err
47 }
48 }
49
50 b, err := bridge.NewBridge(backend, bridgeConfigureTarget, bridgeConfigureName)
51 if err != nil {
52 return err
53 }
54
55 err = b.Configure(bridgeParams)
56 if err != nil {
57 return err
58 }
59
60 fmt.Printf("Successfully configured bridge: %s\n", bridgeConfigureName)
61 return nil
62}
63
64func promptTarget() (string, error) {
65 targets := bridge.Targets()
66
67 for {
68 for i, target := range targets {
69 fmt.Printf("[%d]: %s\n", i+1, target)
70 }
71 fmt.Printf("target: ")
72
73 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
74 if err != nil {
75 return "", err
76 }
77
78 line = strings.TrimRight(line, "\n")
79
80 index, err := strconv.Atoi(line)
81 if err != nil || index <= 0 || index > len(targets) {
82 fmt.Println("invalid input")
83 continue
84 }
85
86 return targets[index-1], nil
87 }
88}
89
90func promptName() (string, error) {
91 fmt.Printf("name [%s]: ", defaultName)
92
93 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
94 if err != nil {
95 return "", err
96 }
97
98 line = strings.TrimRight(line, "\n")
99
100 if line == "" {
101 return defaultName, nil
102 }
103
104 return line, nil
105}
106
107var bridgeConfigureCmd = &cobra.Command{
108 Use: "configure",
109 Short: "Configure a new bridge.",
110 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.
111 Repository configuration can be made by passing either the --url flag or the --project and --owner flags. If the three flags are provided git-bug will use --project and --owner flags.
112 Token configuration can be directly passed with the --token flag or in the terminal prompt. If you don't already have one you can use the interactive procedure to generate one.`,
113 Example: `# Interactive example
114[1]: github
115[2]: launchpad-preview
116target: 1
117name [default]: default
118
119Detected projects:
120[1]: github.com/a-hilaly/git-bug
121[2]: github.com/MichaelMure/git-bug
122
123[0]: Another project
124
125Select option: 1
126
127[0]: user provided token
128[1]: interactive token creation
129Select option: 0
130
131You can generate a new token by visiting https://github.com/settings/tokens.
132Choose 'Generate new token' and set the necessary access scope for your repository.
133
134The access scope depend on the type of repository.
135Public:
136 - 'public_repo': to be able to read public repositories
137Private:
138 - 'repo' : to be able to read private repositories
139
140Enter token:
141Successfully configured bridge: default
142
143# For Github
144git bug bridge configure \
145 --name=default \
146 --target=github \
147 --owner=$(OWNER) \
148 --project=$(PROJECT) \
149 --token=$(TOKEN)
150
151# For Launchpad
152git bug bridge configure \
153 --name=default \
154 --target=launchpad-preview \
155 --url=https://bugs.launchpad.net/ubuntu/`,
156 PreRunE: loadRepo,
157 RunE: runBridgeConfigure,
158}
159
160func init() {
161 bridgeCmd.AddCommand(bridgeConfigureCmd)
162 bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
163 bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
164 fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
165 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
166 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
167 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
168 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
169 bridgeConfigureCmd.Flags().SortFlags = false
170}