Skip to content

Commit

Permalink
feat: add --copy flag to copy PR URL to clipboard (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
wowu authored Aug 19, 2022
1 parent 98fb355 commit 60757aa
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 30 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ Use `-p | --print` flag to print the Pull Request URL instead of opening it in d
pro -p
```

Use `-c | --copy` flag to copy the Pull Request URL to clipboard instead of opening it in default browser:

```bash
pro -c
```

82 changes: 54 additions & 28 deletions command/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/wowu/pro/config"
"github.com/wowu/pro/provider/github"
"github.com/wowu/pro/provider/gitlab"
"golang.design/x/clipboard"

"github.com/fatih/color"
"github.com/go-git/go-git/v5"
giturls "github.com/whilp/git-urls"
)

func Open(repoPath string, print bool) {
func Open(repoPath string, print bool, copy bool) {
repository, err := findRepo(repoPath)
if err != nil {
fmt.Fprintln(os.Stderr, color.RedString("Unable to find git repository in given directory or any of parent directories."))
Expand Down Expand Up @@ -61,6 +62,9 @@ func Open(repoPath string, print bool) {

if print {
color.Blue(homeUrl)
} else if copy {
copyToClipboard(homeUrl)
fmt.Fprintln(os.Stderr, "Copied to clipboard: "+color.BlueString(homeUrl))
} else {
color.Blue(homeUrl)
openBrowser(homeUrl)
Expand All @@ -72,15 +76,43 @@ func Open(repoPath string, print bool) {
projectPath := strings.TrimPrefix(gitURL.Path, "/")
projectPath = strings.TrimSuffix(projectPath, ".git")

var url string
var exists bool
var requestType string
switch gitURL.Host {
case "gitlab.com":
openGitLab(branch, projectPath, print)
exists, url = getGitLabUrl(branch, projectPath, print)
requestType = "merge request"
case "github.com":
openGitHub(branch, projectPath, print)
exists, url = getGitHubUrl(branch, projectPath, print)
requestType = "pull request"
default:
fmt.Fprintln(os.Stderr, "Unknown remote type")
os.Exit(1)
}

if !exists {
fmt.Fprintf(os.Stderr, "No open %s found for current branch\n", requestType)
fmt.Fprintf(os.Stderr, "Create %s at ", requestType)
color.Blue(url)

if copy {
copyToClipboard(url)
fmt.Fprintln(os.Stderr, "Copied to clipboard.")
}

os.Exit(0)
}

if print {
color.Blue(url)
} else if copy {
copyToClipboard(url)
fmt.Fprintln(os.Stderr, "Copied to clipboard: "+color.BlueString(url))
} else {
fmt.Fprintln(os.Stderr, "Opening "+color.BlueString(url))
openBrowser(url)
}
}

// Find git repository in given directory or parent directories.
Expand Down Expand Up @@ -109,7 +141,8 @@ func findRepo(path string) (*git.Repository, error) {
return nil, err
}

func openGitLab(branch string, projectPath string, print bool) {
// Returns merge request URL if it exists for given branch, otherwise returns URL to create new one.
func getGitLabUrl(branch string, projectPath string, print bool) (exists bool, url string) {
gitlabToken := config.Get().GitLabToken

if gitlabToken == "" {
Expand All @@ -120,9 +153,7 @@ func openGitLab(branch string, projectPath string, print bool) {
mergeRequest, err := gitlab.FindMergeRequest(projectPath, gitlabToken, branch)
if err != nil {
if errors.Is(err, gitlab.ErrNotFound) {
fmt.Fprintln(os.Stderr, "No open merge request found for current branch")
fmt.Fprintln(os.Stderr, "Create pull request at", color.BlueString("https://gitlab.com/%s/merge_requests/new?merge_request%%5Bsource_branch%%5D=%s", projectPath, branch))
os.Exit(0)
return false, fmt.Sprintf("https://gitlab.com/%s/merge_requests/new?merge_request%%5Bsource_branch%%5D=%s", projectPath, branch)
} else if errors.Is(err, gitlab.ErrUnauthorized) || errors.Is(err, gitlab.ErrTokenExpired) {
fmt.Fprintln(os.Stderr, color.RedString("Unable to get merge requests: %s", err.Error()))
fmt.Fprintln(os.Stderr, "Connect GitLab again with `pro auth gitlab`.")
Expand All @@ -133,17 +164,11 @@ func openGitLab(branch string, projectPath string, print bool) {
}
}

url := mergeRequest.WebUrl

if print {
color.Blue(url)
} else {
fmt.Fprintln(os.Stderr, "Opening "+color.BlueString(url))
openBrowser(url)
}
return true, mergeRequest.WebUrl
}

func openGitHub(branch string, projectPath string, print bool) {
// Returns pull request URL if it exists for given branch, otherwise returns URL to create new one.
func getGitHubUrl(branch string, projectPath string, print bool) (exists bool, url string) {
githubToken := config.Get().GitHubToken

if githubToken == "" {
Expand All @@ -154,9 +179,7 @@ func openGitHub(branch string, projectPath string, print bool) {
pullRequest, err := github.FindPullRequest(projectPath, githubToken, branch)
if err != nil {
if errors.Is(err, github.ErrNotFound) {
fmt.Fprintln(os.Stderr, "No open pull request found for current branch")
fmt.Fprintln(os.Stderr, "Create pull request at", color.BlueString("https://github.com/%s/pull/new/%s", projectPath, branch))
os.Exit(0)
return false, fmt.Sprintf("https://github.com/%s/pull/new/%s", projectPath, branch)
} else if errors.Is(err, github.ErrUnauthorized) {
fmt.Fprintln(os.Stderr, color.RedString("Unable to get pull requests: %s", err.Error()))
fmt.Fprintln(os.Stderr, "Token may be expired or deleted. Run `pro auth github` to connect GitHub again.")
Expand All @@ -167,14 +190,7 @@ func openGitHub(branch string, projectPath string, print bool) {
}
}

url := pullRequest.HtmlURL

if print {
color.Blue(url)
} else {
fmt.Fprintln(os.Stderr, "Opening "+color.BlueString(url))
openBrowser(url)
}
return true, pullRequest.HtmlURL
}

func openBrowser(url string) {
Expand All @@ -192,7 +208,17 @@ func openBrowser(url string) {
}

if err != nil {
fmt.Printf("Unable to open browser: %s\n", err)
fmt.Fprintln(os.Stderr, color.RedString("Unable to open browser: %s", err.Error()))
os.Exit(1)
}
}

func copyToClipboard(url string) {
err := clipboard.Init()
if err != nil {
fmt.Fprintln(os.Stderr, color.RedString("Unable to copy to clipboard: %s", err.Error()))
os.Exit(1)
}

clipboard.Write(clipboard.FmtText, []byte(url))
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ require (
github.com/stretchr/testify v1.8.0 // indirect
github.com/xanzy/ssh-agent v0.3.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.design/x/clipboard v0.6.2 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
Expand Down Expand Up @@ -101,20 +102,45 @@ github.com/xanzy/ssh-agent v0.3.1 h1:AmzO1SSWxw73zxFZPRwaMN1MohDw8UyHnmuxyceTEGo
github.com/xanzy/ssh-agent v0.3.1/go.mod h1:QIE4lCeL7nkC25x+yA3LBIYfwCc1TFziCtG7cBAac6w=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.design/x/clipboard v0.6.2 h1:a3Np4qfKnLWwfFJQhUWU3IDeRfmVuqWl+QPtP4CSYGw=
golang.design/x/clipboard v0.6.2/go.mod h1:kqBSweBP0/im4SZGGjLrppH0D400Hnfo5WbFKSNK8N4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 h1:3In5TnfvnuXTF/uflgpYxSCEGP2NdYT37KsPh3VjZYU=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554/go.mod h1:jFTmtFYCV0MFtXBU+J5V/+5AUeVS0ON/0WkE/KSrl6E=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b h1:3ogNYyK4oIQdIKzTu68hQrr4iuVxF3AxKl9Aj/eDrw0=
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -125,8 +151,10 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -137,10 +165,17 @@ golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
9 changes: 7 additions & 2 deletions pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var openCommandFlags = []cli.Flag{
Aliases: []string{"p"},
Usage: "print URL instead of opening in browser",
},
&cli.BoolFlag{
Name: "copy",
Aliases: []string{"c"},
Usage: "copy URL to clipboard instead of opening in browser",
},
}

func main() {
Expand Down Expand Up @@ -54,13 +59,13 @@ func main() {
Usage: "Open PR page in browser (default action)",
Flags: openCommandFlags,
Action: func(c *cli.Context) error {
command.Open(".", c.Bool("print"))
command.Open(".", c.Bool("print"), c.Bool("copy"))
return nil
},
},
},
Action: func(c *cli.Context) error {
command.Open(".", c.Bool("print"))
command.Open(".", c.Bool("print"), c.Bool("copy"))

return nil
},
Expand Down

0 comments on commit 60757aa

Please sign in to comment.