Skip to content

Commit e65f991

Browse files
authored
Avoid letting the clone command panic if no gh command (#46)
1 parent 853ad25 commit e65f991

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cmd/clone.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
type cloneOption struct {
14-
ws bool
14+
ws bool
15+
protocol string
1516
}
1617

1718
// NewCloneCommand returns the clone command
@@ -26,6 +27,7 @@ func NewCloneCommand() (cmd *cobra.Command) {
2627

2728
flags := cmd.Flags()
2829
flags.BoolVarP(&opt.ws, "ws", "", false, "Clone the code into ~/ws/github/org/repo if it is true")
30+
flags.StringVarP(&opt.protocol, "protocol", "p", "https", "The protocol, support: https, ssh, git")
2931
return
3032
}
3133

@@ -39,7 +41,7 @@ func (o *cloneOption) runE(_ *cobra.Command, args []string) (err error) {
3941
if !o.ws {
4042
output = nil
4143
}
42-
args = pkg.ParseShortCode(args, output)
44+
args = pkg.ParseShortCodeWithProtocol(args, o.protocol, output)
4345

4446
var targetDir string
4547
gitAddress := args[0]

pkg/shortcode.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ import (
77

88
// ParseShortCode parses a short org/repo to a GitHub URL
99
func ParseShortCode(args []string, output func(string) string) []string {
10+
return ParseShortCodeWithProtocol(args, "https", output)
11+
}
12+
13+
// ParseShortCodeWithProtocol parses a short org/repo with protocol
14+
func ParseShortCodeWithProtocol(args []string, protocol string, output func(string) string) []string {
1015
if len(args) <= 0 {
1116
return args
1217
}
1318
if output == nil {
1419
output = defaultOutput
1520
}
1621

17-
result := []string{fmt.Sprintf("https://github.com/%s", args[0])}
22+
var result []string
23+
switch protocol {
24+
case "https":
25+
result = []string{fmt.Sprintf("https://github.com/%s", args[0])}
26+
case "ssh", "git":
27+
result = []string{fmt.Sprintf("git@github.com:%s.git", args[0])}
28+
}
29+
1830
if len(args) > 1 {
1931
result = append(result, args[1:]...)
2032
} else {

0 commit comments

Comments
 (0)