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 termState, err := terminal.GetState(int(syscall.Stdin))
37 if err != nil {
38 return err
39 }
40 interrupt.RegisterCleaner(func() error {
41 return terminal.Restore(int(syscall.Stdin), termState)
42 })
43 if bridgeConfigureTarget == "" {
44 bridgeConfigureTarget, err = promptTarget()
45 if err != nil {
46 return err
47 }
48 }
49
50 if bridgeConfigureName == "" {
51 bridgeConfigureName, err = promptName()
52 if err != nil {
53 return err
54 }
55 }
56
57 b, err := bridge.NewBridge(backend, bridgeConfigureTarget, bridgeConfigureName)
58 if err != nil {
59 return err
60 }
61
62 err = b.Configure(bridgeParams)
63 if err != nil {
64 return err
65 }
66
67 fmt.Printf("Successfully configured bridge: %s\n", bridgeConfigureName)
68 return nil
69}
70
71func promptTarget() (string, error) {
72 targets := bridge.Targets()
73
74 for {
75 for i, target := range targets {
76 fmt.Printf("[%d]: %s\n", i+1, target)
77 }
78 fmt.Printf("target: ")
79
80 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
81 if err != nil {
82 return "", err
83 }
84
85 line = strings.TrimRight(line, "\n")
86
87 index, err := strconv.Atoi(line)
88 if err != nil || index <= 0 || index > len(targets) {
89 fmt.Println("invalid input")
90 continue
91 }
92
93 return targets[index-1], nil
94 }
95}
96
97func promptName() (string, error) {
98 fmt.Printf("name [%s]: ", defaultName)
99
100 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
101 if err != nil {
102 return "", err
103 }
104
105 line = strings.TrimRight(line, "\n")
106
107 if line == "" {
108 return defaultName, nil
109 }
110
111 return line, nil
112}
113
114var bridgeConfigureCmd = &cobra.Command{
115 Use: "configure",
116 Short: "Configure a new bridge.",
117 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.
118 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.
119 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.`,
120 Example: `# Interactive example
121[1]: github
122[2]: launchpad-preview
123target: 1
124name [default]: default
125
126Detected projects:
127[1]: github.com/a-hilaly/git-bug
128[2]: github.com/MichaelMure/git-bug
129
130[0]: Another project
131
132Select option: 1
133
134[0]: user provided token
135[1]: interactive token creation
136Select option: 0
137
138You can generate a new token by visiting https://github.com/settings/tokens.
139Choose 'Generate new token' and set the necessary access scope for your repository.
140
141The access scope depend on the type of repository.
142Public:
143 - 'public_repo': to be able to read public repositories
144Private:
145 - 'repo' : to be able to read private repositories
146
147Enter token:
148Successfully configured bridge: default
149
150# For Github
151git bug bridge configure \
152 --name=default \
153 --target=github \
154 --owner=$(OWNER) \
155 --project=$(PROJECT) \
156 --token=$(TOKEN)
157
158# For Launchpad
159git bug bridge configure \
160 --name=default \
161 --target=launchpad-preview \
162 --url=https://bugs.launchpad.net/ubuntu/`,
163 PreRunE: loadRepo,
164 RunE: runBridgeConfigure,
165}
166
167func init() {
168 bridgeCmd.AddCommand(bridgeConfigureCmd)
169 bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
170 bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
171 fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
172 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
173 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
174 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
175 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
176 bridgeConfigureCmd.Flags().SortFlags = false
177}