Add open functionality #21

Merged
jolheiser merged 2 commits from open into master 2020-09-13 04:34:13 +00:00
5 changed files with 53 additions and 0 deletions

View File

@ -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")
}

45
cmd/open.go Normal file
View File

@ -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")
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -26,6 +26,7 @@ func main() {
&cmd.Issues,
&cmd.Pulls,
&cmd.Release,
&cmd.Open,
}
app.Flags = cmd.Flags
app.EnableBashCompletion = true