diff --git a/cmd/open.go b/cmd/open.go new file mode 100644 index 0000000..a528c04 --- /dev/null +++ b/cmd/open.go @@ -0,0 +1,77 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "log" + "path" + "strings" + + local_git "code.gitea.io/tea/modules/git" + + "github.com/skratchdot/open-golang/open" + "github.com/urfave/cli/v2" +) + +// CmdOpen represents a sub command of issues to open issue on the web browser +var CmdOpen = cli.Command{ + Name: "open", + Usage: "Open something of the repository on web browser", + Description: `Open something of the repository on web browser`, + Action: runOpen, + Flags: append([]cli.Flag{}, LoginRepoFlags...), +} + +func runOpen(ctx *cli.Context) error { + login, owner, repo := initCommand() + + var suffix string + number := ctx.Args().Get(0) + switch { + case strings.EqualFold(number, "issues"): + suffix = "issues" + case strings.EqualFold(number, "pulls"): + suffix = "pulls" + case strings.EqualFold(number, "releases"): + suffix = "releases" + case strings.EqualFold(number, "commits"): + b, err := local_git.GetRepoReference("./") + if err != nil { + log.Fatal(err) + return nil + } + name := b.Name() + switch { + case name.IsBranch(): + suffix = "commits/branch/" + name.Short() + case name.IsTag(): + suffix = "commits/tag/" + name.Short() + } + case strings.EqualFold(number, "branches"): + suffix = "branches" + case strings.EqualFold(number, "wiki"): + suffix = "wiki" + case strings.EqualFold(number, "activity"): + suffix = "activity" + case strings.EqualFold(number, "settings"): + suffix = "settings" + case strings.EqualFold(number, "labels"): + suffix = "labels" + case strings.EqualFold(number, "milestones"): + suffix = "milestones" + case number != "": + suffix = "issues/" + number + default: + suffix = number + } + + u := path.Join(login.URL, owner, repo, suffix) + err := open.Run(u) + if err != nil { + log.Fatal(err) + } + + return nil +} diff --git a/go.mod b/go.mod index edd1744..3b336b8 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-gitea/yaml v0.0.0-20170812160011-eb3733d160e7 github.com/mattn/go-runewidth v0.0.4 // indirect github.com/olekukonko/tablewriter v0.0.1 + github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/stretchr/testify v1.4.0 github.com/urfave/cli/v2 v2.1.1 gopkg.in/src-d/go-git.v4 v4.13.1 diff --git a/go.sum b/go.sum index e18acf8..97d698a 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,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/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4= github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/main.go b/main.go index 9b4062e..8292ace 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ func main() { &cmd.CmdRepos, &cmd.CmdLabels, &cmd.CmdTrackedTimes, + &cmd.CmdOpen, } app.EnableBashCompletion = true err := app.Run(os.Args) diff --git a/modules/git/ref.go b/modules/git/ref.go new file mode 100644 index 0000000..89aa8fd --- /dev/null +++ b/modules/git/ref.go @@ -0,0 +1,20 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + go_git "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing" +) + +// GetRepoReference returns the current repository's current branch or tag +func GetRepoReference(p string) (*plumbing.Reference, error) { + gitPath, err := go_git.PlainOpenWithOptions(p, &go_git.PlainOpenOptions{DetectDotGit: true}) + if err != nil { + return nil, err + } + + return gitPath.Head() +} diff --git a/modules/git/url.go b/modules/git/url.go index 87bf00e..7124aa2 100644 --- a/modules/git/url.go +++ b/modules/git/url.go @@ -1,3 +1,7 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package git import ( diff --git a/vendor/github.com/skratchdot/open-golang/LICENSE b/vendor/github.com/skratchdot/open-golang/LICENSE new file mode 100644 index 0000000..afd04c8 --- /dev/null +++ b/vendor/github.com/skratchdot/open-golang/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2013 skratchdot + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/skratchdot/open-golang/open/exec.go b/vendor/github.com/skratchdot/open-golang/open/exec.go new file mode 100644 index 0000000..1b0e713 --- /dev/null +++ b/vendor/github.com/skratchdot/open-golang/open/exec.go @@ -0,0 +1,18 @@ +// +build !windows,!darwin + +package open + +import ( + "os/exec" +) + +// http://sources.debian.net/src/xdg-utils/1.1.0~rc1%2Bgit20111210-7.1/scripts/xdg-open/ +// http://sources.debian.net/src/xdg-utils/1.1.0~rc1%2Bgit20111210-7.1/scripts/xdg-mime/ + +func open(input string) *exec.Cmd { + return exec.Command("xdg-open", input) +} + +func openWith(input string, appName string) *exec.Cmd { + return exec.Command(appName, input) +} diff --git a/vendor/github.com/skratchdot/open-golang/open/exec_darwin.go b/vendor/github.com/skratchdot/open-golang/open/exec_darwin.go new file mode 100644 index 0000000..16160e6 --- /dev/null +++ b/vendor/github.com/skratchdot/open-golang/open/exec_darwin.go @@ -0,0 +1,15 @@ +// +build darwin + +package open + +import ( + "os/exec" +) + +func open(input string) *exec.Cmd { + return exec.Command("open", input) +} + +func openWith(input string, appName string) *exec.Cmd { + return exec.Command("open", "-a", appName, input) +} diff --git a/vendor/github.com/skratchdot/open-golang/open/exec_windows.go b/vendor/github.com/skratchdot/open-golang/open/exec_windows.go new file mode 100644 index 0000000..6e46c00 --- /dev/null +++ b/vendor/github.com/skratchdot/open-golang/open/exec_windows.go @@ -0,0 +1,33 @@ +// +build windows + +package open + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + // "syscall" +) + +var ( + cmd = "url.dll,FileProtocolHandler" + runDll32 = filepath.Join(os.Getenv("SYSTEMROOT"), "System32", "rundll32.exe") +) + +func cleaninput(input string) string { + r := strings.NewReplacer("&", "^&") + return r.Replace(input) +} + +func open(input string) *exec.Cmd { + cmd := exec.Command(runDll32, cmd, input) + //cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return cmd +} + +func openWith(input string, appName string) *exec.Cmd { + cmd := exec.Command("cmd", "/C", "start", "", appName, cleaninput(input)) + //cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return cmd +} diff --git a/vendor/github.com/skratchdot/open-golang/open/open.go b/vendor/github.com/skratchdot/open-golang/open/open.go new file mode 100644 index 0000000..b1f648f --- /dev/null +++ b/vendor/github.com/skratchdot/open-golang/open/open.go @@ -0,0 +1,50 @@ +/* + + Open a file, directory, or URI using the OS's default + application for that object type. Optionally, you can + specify an application to use. + + This is a proxy for the following commands: + + OSX: "open" + Windows: "start" + Linux/Other: "xdg-open" + + This is a golang port of the node.js module: https://github.com/pwnall/node-open + +*/ +package open + +/* + Open a file, directory, or URI using the OS's default + application for that object type. Wait for the open + command to complete. +*/ +func Run(input string) error { + return open(input).Run() +} + +/* + Open a file, directory, or URI using the OS's default + application for that object type. Don't wait for the + open command to complete. +*/ +func Start(input string) error { + return open(input).Start() +} + +/* + Open a file, directory, or URI using the specified application. + Wait for the open command to complete. +*/ +func RunWith(input string, appName string) error { + return openWith(input, appName).Run() +} + +/* + Open a file, directory, or URI using the specified application. + Don't wait for the open command to complete. +*/ +func StartWith(input string, appName string) error { + return openWith(input, appName).Start() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 5b6f962..ee0d191 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -35,6 +35,8 @@ github.com/russross/blackfriday/v2 github.com/sergi/go-diff/diffmatchpatch # github.com/shurcooL/sanitized_anchor_name v1.0.0 github.com/shurcooL/sanitized_anchor_name +# github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 +github.com/skratchdot/open-golang/open # github.com/src-d/gcfg v1.4.0 github.com/src-d/gcfg github.com/src-d/gcfg/scanner