package cmd import ( "errors" "fmt" "github.com/skratchdot/open-golang/open" "github.com/urfave/cli/v2" "strconv" "strings" ) var Open = cli.Command{ Name: "open", Aliases: []string{"o"}, Usage: "Open a repository or issue/pull request", Action: doOpen, } func doOpen(ctx *cli.Context) error { repo := fullRepoURL(ctx) if ctx.NArg() == 0 { return open.Run(repo) } arg := ctx.Args().First() // Check if issue or PR issue, err := strconv.ParseInt(arg, 10, 64) if err == nil { return open.Run(fmt.Sprintf("%s/issues/%d", repo, issue)) } // Check if overriding repository (jolheiser/sip) ownerRepoIssue := strings.Split(arg, "/") if len(ownerRepoIssue) == 2 { return open.Run(fmt.Sprintf("%s/%s", ctx.String("url"), arg)) } // Check if both? (jolheiser/sip/1234) if len(ownerRepoIssue) == 3 { return open.Run(fmt.Sprintf("%s/%s/%s/issues/%s", ctx.String("url"), ownerRepoIssue[0], ownerRepoIssue[1], ownerRepoIssue[2])) } return errors.New("unknown argument: leave blank to open current repo, pass issue/PR as #1234, or override repo as owner/repo") }