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 or the --url flag or the --project and/or --owner flags. If the three flags are provided git-bug will use --project and --owner flags.
112 Token configuration can be made by passing it in the --token flag or in the terminal prompt. If you don't already have one you can use terminal prompt to login and generate it directly.
113 For Github and Gitlab bridges, git-bug need a token to export and import issues, comments and editions for public and private repositories.
114 For Launchpad bridges, git-bug for now supports only public repositories and you only need --project or --url flag to configure it.`,
115 Example: `# For Github
116git bug bridge configure \
117 --name=default \
118 --target=github \
119 --owner=$(OWNER) \
120 --project=$(PROJECT) \
121 --token=$(TOKEN)
122
123# For Launchpad
124git bug bridge configure \
125 --name=default \
126 --target=launchpad-preview \
127 --url=https://bugs.launchpad.net/ubuntu/`,
128 PreRunE: loadRepo,
129 RunE: runBridgeConfigure,
130}
131
132func init() {
133 bridgeCmd.AddCommand(bridgeConfigureCmd)
134 bridgeConfigureCmd.Flags().StringVarP(&name, "name", "n", "", "A distinctive name to identify the bridge")
135 bridgeConfigureCmd.Flags().StringVarP(&target, "target", "t", "",
136 fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
137 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
138 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
139 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
140 bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
141 bridgeConfigureCmd.Flags().SortFlags = false
142}