Skip to content

Commit

Permalink
Avoid letting the clone command panic if no gh command (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Feb 8, 2022
1 parent 853ad25 commit e65f991
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type cloneOption struct {
ws bool
ws bool
protocol string
}

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

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

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

var targetDir string
gitAddress := args[0]
Expand Down
14 changes: 13 additions & 1 deletion pkg/shortcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ import (

// ParseShortCode parses a short org/repo to a GitHub URL
func ParseShortCode(args []string, output func(string) string) []string {
return ParseShortCodeWithProtocol(args, "https", output)
}

// ParseShortCodeWithProtocol parses a short org/repo with protocol
func ParseShortCodeWithProtocol(args []string, protocol string, output func(string) string) []string {
if len(args) <= 0 {
return args
}
if output == nil {
output = defaultOutput
}

result := []string{fmt.Sprintf("https://github.com/%s", args[0])}
var result []string
switch protocol {
case "https":
result = []string{fmt.Sprintf("https://github.com/%s", args[0])}
case "ssh", "git":
result = []string{fmt.Sprintf("git@github.com:%s.git", args[0])}
}

if len(args) > 1 {
result = append(result, args[1:]...)
} else {
Expand Down

0 comments on commit e65f991

Please sign in to comment.