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 name string
24 target 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 target == "" {
37 target, err = promptTarget()
38 if err != nil {
39 return err
40 }
41 }
42
43 if name == "" {
44 name, err = promptName()
45 if err != nil {
46 return err
47 }
48 }
49
50 b, err := bridge.NewBridge(backend, target, name)
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", name)
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: `# For Github
114git bug bridge configure \
115 --name=default \
116 --target=github \
117 --owner=$(OWNER) \
118 --project=$(PROJECT) \
119 --token=$(TOKEN)
120
121# For Launchpad
122git bug bridge configure \
123 --name=default \
124 --target=launchpad-preview \
125 --url=https://bugs.launchpad.net/ubuntu/`,
126 PreRunE: loadRepo,
127 RunE: runBridgeConfigure,
128}
129
130func init() {
131 bridgeCmd.AddCommand(bridgeConfigureCmd)
132 bridgeConfigureCmd.Flags().StringVarP(&name, "name", "n", "", "A distinctive name to identify the bridge")
133 bridgeConfigureCmd.Flags().StringVarP(&target, "target", "t", "",
134 fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
135 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
136 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
137 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
138 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
139 bridgeConfigureCmd.Flags().SortFlags = false
140}