-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathremote_resolver.go
More file actions
97 lines (84 loc) · 2.34 KB
/
Copy pathremote_resolver.go
File metadata and controls
97 lines (84 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package utils
import (
"fmt"
"strings"
)
type RemoteData struct {
Owner string
RepoName string
VCSProvider string
}
func ResolveRemote(repoArg string) (*RemoteData, error) {
var remote RemoteData
// If the user supplied a --repo flag with the repo URL
if repoArg != "" {
repoData, err := RepoArgumentResolver(repoArg)
if err != nil {
return nil, err
}
remote.VCSProvider = repoData[0]
remote.Owner = repoData[1]
remote.RepoName = repoData[2]
return &remote, nil
}
// If the user didn't pass --repo flag
// Figure out list of remotes by reading git config
remotesData, err := ListRemotes()
if err != nil {
if strings.Contains(err.Error(), "exit status 128") {
fmt.Println("This repository has not been initialized with git. Please initialize it with git using `git init`")
}
return nil, err
}
// If there is only one remote, use it
if len(remotesData) == 1 {
for _, value := range remotesData {
remote.Owner = value[0]
remote.RepoName = value[1]
remote.VCSProvider = value[2]
}
return &remote, nil
}
// If there are more than one remotes, give the option to user
// to select the one which they want
var promptOpts []string
// Preparing the options to show to the user
for _, value := range remotesData {
promptOpts = append(promptOpts, value[3])
}
selectedRemote, err := SelectFromOptions("Please select the repository:", "", promptOpts)
if err != nil {
return nil, err
}
// Matching the list of remotes with the one selected by user
for _, value := range remotesData {
if value[3] == selectedRemote {
remote.Owner = value[0]
remote.RepoName = value[1]
remote.VCSProvider = value[2]
}
}
return &remote, nil
}
// Utility to parse the --repo flag
func RepoArgumentResolver(arg string) ([]string, error) {
// github.com/deepsourcelabs/cli or gh/deepsourcelabs/cli
argComponents := strings.Split(arg, "/")
switch argComponents[0] {
case "gh", "github.com":
argComponents[0] = "GITHUB"
case "ghe":
argComponents[0] = "GITHUB_ENTERPRISE"
case "gl", "gitlab.com":
argComponents[0] = "GITLAB"
case "bb", "bitbucket.com":
argComponents[0] = "BITBUCKET"
case "bbdc":
argComponents[0] = "BITBUCKET_DATACENTER"
case "ads":
argComponents[0] = "ADS"
default:
return argComponents, fmt.Errorf("VCSProvider `%s` not supported", argComponents[0])
}
return argComponents, nil
}