1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-26 07:33:48 +01:00
sip/cmd/open.go
John Olheiser b4d7ff5775 Add open functionality (#21)
Allow overriding repo and choosing issue/PR

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Add open command for repo/issue/PR

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/21
2020-09-13 04:34:11 +00:00

46 lines
1.1 KiB
Go

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