Add tea open (#101)

Fix open default to home page

Improve path join and open with no arg

add labels and milestones

Add tea open

Reviewed-on: https://gitea.com/gitea/tea/pulls/101
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>
This commit is contained in:
Lunny Xiao 2020-04-01 03:22:24 +00:00 committed by John Olheiser
parent b37673c954
commit 7a10ea10df
12 changed files with 245 additions and 0 deletions

77
cmd/open.go Normal file
View File

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

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/go-gitea/yaml v0.0.0-20170812160011-eb3733d160e7 github.com/go-gitea/yaml v0.0.0-20170812160011-eb3733d160e7
github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1 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/stretchr/testify v1.4.0
github.com/urfave/cli/v2 v2.1.1 github.com/urfave/cli/v2 v2.1.1
gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/src-d/go-git.v4 v4.13.1

2
go.sum
View File

@ -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/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 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 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 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -42,6 +42,7 @@ func main() {
&cmd.CmdRepos, &cmd.CmdRepos,
&cmd.CmdLabels, &cmd.CmdLabels,
&cmd.CmdTrackedTimes, &cmd.CmdTrackedTimes,
&cmd.CmdOpen,
} }
app.EnableBashCompletion = true app.EnableBashCompletion = true
err := app.Run(os.Args) err := app.Run(os.Args)

20
modules/git/ref.go Normal file
View File

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

View File

@ -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 package git
import ( import (

22
vendor/github.com/skratchdot/open-golang/LICENSE generated vendored Normal file
View File

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

18
vendor/github.com/skratchdot/open-golang/open/exec.go generated vendored Normal file
View File

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

View File

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

View File

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

50
vendor/github.com/skratchdot/open-golang/open/open.go generated vendored Normal file
View File

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

2
vendor/modules.txt vendored
View File

@ -35,6 +35,8 @@ github.com/russross/blackfriday/v2
github.com/sergi/go-diff/diffmatchpatch github.com/sergi/go-diff/diffmatchpatch
# github.com/shurcooL/sanitized_anchor_name v1.0.0 # github.com/shurcooL/sanitized_anchor_name v1.0.0
github.com/shurcooL/sanitized_anchor_name 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 v1.4.0
github.com/src-d/gcfg github.com/src-d/gcfg
github.com/src-d/gcfg/scanner github.com/src-d/gcfg/scanner