diff --git a/cmd/cmd.go b/cmd/cmd.go index 0b77f0f..b0af64c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -125,6 +125,10 @@ func getOriginRepo() []string { return originRepo } +func fullRepoURL(ctx *cli.Context) string { + return ctx.String("url") + "/" + fullName(ctx) +} + func fullName(ctx *cli.Context) string { return ctx.String("owner") + "/" + ctx.String("repo") } diff --git a/cmd/open.go b/cmd/open.go new file mode 100644 index 0000000..01a4458 --- /dev/null +++ b/cmd/open.go @@ -0,0 +1,45 @@ +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") +} diff --git a/go.mod b/go.mod index 96b0f18..43da615 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/microcosm-cc/bluemonday v1.0.4 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/muesli/termenv v0.7.2 // indirect + github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/urfave/cli/v2 v2.2.0 github.com/yuin/goldmark v1.2.1 // indirect go.jolheiser.com/beaver v1.0.2 diff --git a/go.sum b/go.sum index 1340918..16080e3 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/main.go b/main.go index 4f37477..1ae8e4e 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func main() { &cmd.Issues, &cmd.Pulls, &cmd.Release, + &cmd.Open, } app.Flags = cmd.Flags app.EnableBashCompletion = true