4 Commits

14 changed files with 208 additions and 63 deletions

View File

@ -163,15 +163,15 @@ steps:
event:
- push
- name: github
- name: gitea
pull: always
image: plugins/gitea-releases:1
settings:
files:
- "dist/release/*"
environment:
GITHUB_TOKEN:
from_secret: github_token
base_url: https://gitea.com
api_key:
from_secret: gitea_token
when:
event:
- tag

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
tea
.idea/
.history/
dist/

View File

@ -157,7 +157,7 @@ release-linux:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u src.techknowlogick.com/xgo; \
fi
xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/*' -out tea-$(VERSION) .
xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out tea-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif

View File

@ -84,7 +84,7 @@ func init() {
dir := filepath.Join(homeDir, ".tea")
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
log.Fatal("Init tea config dir", dir, "failed")
log.Fatal("Init tea config dir " + dir + " failed")
}
yamlConfigPath = filepath.Join(dir, "tea.yml")
@ -126,7 +126,7 @@ func addLogin(login Login) error {
if l.URL == login.URL && l.Token == login.Token {
return nil
}
return errors.New("login name has already been used")
return errors.New("Login name has already been used")
}
if l.URL == login.URL && l.Token == login.Token {
return errors.New("URL has been added")
@ -155,7 +155,7 @@ func isFileExist(fileName string) (bool, error) {
return false, err
}
if f.IsDir() {
return false, errors.New("the same name directory exist")
return false, errors.New("A directory with the same name exists")
}
return true, nil
}
@ -198,7 +198,7 @@ func curGitRepoPath() (*Login, string, error) {
// if no remote
if len(gitConfig.Remotes) == 0 {
return nil, "", errors.New("No remote repository set on this git repository")
return nil, "", errors.New("No remote(s) found in this Git repository")
}
// if only one remote exists
@ -219,7 +219,7 @@ func curGitRepoPath() (*Login, string, error) {
remoteConfig, ok := gitConfig.Remotes[remoteValue]
if !ok || remoteConfig == nil {
return nil, "", errors.New("No remote " + remoteValue + " found on this git repository")
return nil, "", errors.New("Remote " + remoteValue + " not found in this Git repository")
}
for _, l := range config.Logins {
@ -242,5 +242,5 @@ func curGitRepoPath() (*Login, string, error) {
}
}
return nil, "", errors.New("No Gitea login found")
return nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository")
}

View File

@ -22,28 +22,28 @@ var (
// LoginFlag provides flag to specify tea login profile
var LoginFlag = cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
Usage: "Use a different Gitea login. Optional",
Destination: &loginValue,
}
// RepoFlag provides flag to specify repository
var RepoFlag = cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
Usage: "Repository to interact with. Optional",
Destination: &repoValue,
}
// RemoteFlag provides flag to specify remote repository
var RemoteFlag = cli.StringFlag{
Name: "remote, R",
Usage: "Set a specific remote repository, is optional if not set use git default one",
Usage: "Discover Gitea login from remote. Optional",
Destination: &remoteValue,
}
// OutputFlag provides flag to specify output type
var OutputFlag = cli.StringFlag{
Name: "output, o",
Usage: "Specify output format. (csv, simple, table, tsv, yaml)",
Usage: "Output format. (csv, simple, table, tsv, yaml)",
Destination: &outputValue,
}
@ -78,7 +78,7 @@ var AllDefaultFlags = append([]cli.Flag{
func initCommand() (*Login, string, string) {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed ", yamlConfigPath)
log.Fatal("Unable to load config file " + yamlConfigPath)
}
var login *Login
@ -90,7 +90,7 @@ func initCommand() (*Login, string, string) {
} else {
login = getLoginByName(loginValue)
if login == nil {
log.Fatal("indicated login name ", loginValue, " does not exist")
log.Fatal("Login name " + loginValue + " does not exist")
}
}
@ -105,3 +105,20 @@ func initCommand() (*Login, string, string) {
owner, repo := splitRepo(repoPath)
return login, owner, repo
}
// initCommandLoginOnly return *Login based on flags
func initCommandLoginOnly() *Login {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed ", yamlConfigPath)
}
var login *Login
login = getLoginByName(loginValue)
if login == nil {
log.Fatal("indicated login name ", loginValue, " does not exist")
}
return login
}

View File

@ -19,8 +19,8 @@ import (
// CmdIssues represents to login a gitea server.
var CmdIssues = cli.Command{
Name: "issues",
Usage: "Operate with issues of the repository",
Description: `Operate with issues of the repository`,
Usage: "List and create issues",
Description: `List and create issues`,
Action: runIssues,
Subcommands: []cli.Command{
CmdIssuesList,

View File

@ -20,8 +20,8 @@ import (
// CmdLabels represents to operate repositories' labels.
var CmdLabels = cli.Command{
Name: "labels",
Usage: "Operate with labels of the repository",
Description: `Operate with labels of the repository`,
Usage: "Manage issue labels",
Description: `Manage issue labels`,
Action: runLabels,
Subcommands: []cli.Command{
CmdLabelCreate,
@ -90,8 +90,8 @@ func runLabels(ctx *cli.Context) error {
// CmdLabelCreate represents a sub command of labels to create label.
var CmdLabelCreate = cli.Command{
Name: "create",
Usage: "Create a label in repository",
Description: `Create a label in repository`,
Usage: "Create a label",
Description: `Create a label`,
Action: runLabelCreate,
Flags: []cli.Flag{
cli.StringFlag{
@ -182,8 +182,8 @@ func runLabelCreate(ctx *cli.Context) error {
// CmdLabelUpdate represents a sub command of labels to update label.
var CmdLabelUpdate = cli.Command{
Name: "update",
Usage: "Update a label in repository",
Description: `Update a label in repository`,
Usage: "Update a label",
Description: `Update a label`,
Action: runLabelUpdate,
Flags: []cli.Flag{
cli.IntFlag{
@ -242,8 +242,8 @@ func runLabelUpdate(ctx *cli.Context) error {
// CmdLabelDelete represents a sub command of labels to delete label.
var CmdLabelDelete = cli.Command{
Name: "delete",
Usage: "Delete a label in repository",
Description: `Delete a label in repository`,
Usage: "Delete a label",
Description: `Delete a label`,
Action: runLabelCreate,
Flags: []cli.Flag{
cli.IntFlag{

View File

@ -17,8 +17,6 @@ var (
showLog bool
)
const outputUsage = "Specify output format - table (default), csv, simple, tsv or yaml."
// Println println content according the flag
func Println(a ...interface{}) {
if showLog {

View File

@ -19,9 +19,8 @@ import (
// CmdLogin represents to login a gitea server.
var CmdLogin = cli.Command{
Name: "login",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runLoginList,
Usage: "Log in to a Gitea server",
Description: `Log in to a Gitea server`,
Subcommands: []cli.Command{
cmdLoginList,
cmdLoginAdd,
@ -31,28 +30,28 @@ var CmdLogin = cli.Command{
// CmdLogin represents to login a gitea server.
var cmdLoginAdd = cli.Command{
Name: "add",
Usage: "Add a Login of a Gitea server",
Description: `Add a Login of a Gitea server`,
Usage: "Add a Gitea login",
Description: `Add a Gitea login`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "Name for the gitea login",
Usage: "Login name",
},
cli.StringFlag{
Name: "url, u",
Value: "https://try.gitea.io",
EnvVar: "GITEA_SERVER_URL",
Usage: "Gitea server URL",
Usage: "Server URL",
},
cli.StringFlag{
Name: "token, t",
Value: "",
EnvVar: "GITEA_SERVER_TOKEN",
Usage: "token for operating the Gitea login",
Usage: "Access token. Can be obtained from Settings > Applications",
},
cli.BoolFlag{
Name: "insecure, i",
Usage: "insecure visit gitea server",
Usage: "Disable TLS verification",
},
},
Action: runLoginAdd,
@ -62,18 +61,16 @@ func runLoginAdd(ctx *cli.Context) error {
if !ctx.IsSet("url") {
log.Fatal("You have to input Gitea server URL")
}
if !ctx.IsSet("token") {
log.Fatal("No token found")
}
if !ctx.IsSet("name") {
log.Fatal("You have to set a name for the login")
}
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
log.Fatal("Unable to load config file " + yamlConfigPath)
}
client := gitea.NewClient(ctx.String("url"), ctx.String("token"))
@ -92,7 +89,7 @@ func runLoginAdd(ctx *cli.Context) error {
log.Fatal(err)
}
fmt.Println("Login successful! Login name", u.UserName)
fmt.Println("Login successful! Login name " + u.UserName)
err = addLogin(Login{
Name: ctx.String("name"),
@ -115,15 +112,15 @@ func runLoginAdd(ctx *cli.Context) error {
// CmdLogin represents to login a gitea server.
var cmdLoginList = cli.Command{
Name: "ls",
Usage: "List all Logins of Gitea servers",
Description: `List all Logins of Gitea servers`,
Usage: "List Gitea logins",
Description: `List Gitea logins`,
Action: runLoginList,
}
func runLoginList(ctx *cli.Context) error {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
log.Fatal("Unable to load config file " + yamlConfigPath)
}
fmt.Printf("Name\tURL\tSSHHost\n")

View File

@ -21,7 +21,7 @@ var CmdLogout = cli.Command{
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "name wants to log out",
Usage: "Login name to remove",
},
},
}
@ -33,12 +33,12 @@ func runLogout(ctx *cli.Context) error {
} else if ctx.IsSet("name") {
name = ctx.String("name")
} else {
return errors.New("need log out server name")
return errors.New("Please specify a login name")
}
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
log.Fatal("Unable to load config file " + yamlConfigPath)
}
var idx = -1
@ -52,7 +52,7 @@ func runLogout(ctx *cli.Context) error {
config.Logins = append(config.Logins[:idx], config.Logins[idx+1:]...)
err = saveConfig(yamlConfigPath)
if err != nil {
log.Fatal("save config file failed", yamlConfigPath)
log.Fatal("Unable to save config file " + yamlConfigPath)
}
}

View File

@ -16,8 +16,8 @@ import (
// CmdPulls represents to login a gitea server.
var CmdPulls = cli.Command{
Name: "pulls",
Usage: "Operate with pulls of the repository",
Description: `Operate with pulls of the repository`,
Usage: "List open pull requests",
Description: `List open pull requests`,
Action: runPulls,
Flags: AllDefaultFlags,
}

View File

@ -17,8 +17,8 @@ import (
// CmdReleases represents to login a gitea server.
var CmdReleases = cli.Command{
Name: "releases",
Usage: "Operate with releases of the repository",
Description: `Operate with releases of the repository`,
Usage: "Create releases",
Description: `Create releases`,
Action: runReleases,
Subcommands: []cli.Command{
CmdReleaseCreate,
@ -67,37 +67,37 @@ func runReleases(ctx *cli.Context) error {
// CmdReleaseCreate represents a sub command of Release to create release.
var CmdReleaseCreate = cli.Command{
Name: "create",
Usage: "Create a release in repository",
Description: `Create a release in repository`,
Usage: "Create a release",
Description: `Create a release`,
Action: runReleaseCreate,
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "tag",
Usage: "release tag name",
Usage: "Tag name",
},
cli.StringFlag{
Name: "target",
Usage: "release target refs, branch name or commit id",
Usage: "Target refs, branch name or commit id",
},
cli.StringFlag{
Name: "title, t",
Usage: "release title to create",
Usage: "Release title",
},
cli.StringFlag{
Name: "note, n",
Usage: "release note to create",
Usage: "Release notes",
},
cli.BoolFlag{
Name: "draft, d",
Usage: "the release is a draft",
Usage: "Is a draft",
},
cli.BoolFlag{
Name: "prerelease, p",
Usage: "the release is a prerelease",
Usage: "Is a pre-release",
},
cli.StringSliceFlag{
Name: "asset, a",
Usage: "a list of files to attach to the release",
Usage: "List of files to attach",
},
}, LoginRepoFlags...),
}

132
cmd/repos.go Normal file
View File

@ -0,0 +1,132 @@
// 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 cmd
import (
"log"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
)
// CmdRepos represents to login a gitea server.
var CmdRepos = cli.Command{
Name: "repos",
Usage: "Operate with repositories",
Description: `Operate with repositories`,
Action: runReposList,
Subcommands: []cli.Command{
CmdReposList,
},
Flags: LoginOutputFlags,
}
// CmdReposList represents a sub command of issues to list issues
var CmdReposList = cli.Command{
Name: "ls",
Usage: "List available repositories",
Description: `List available repositories`,
Action: runReposList,
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "mode",
Usage: "Filter listed repositories based on mode, optional - fork, mirror, source",
},
cli.StringFlag{
Name: "org",
Usage: "Filter listed repositories based on organization, optional",
},
cli.StringFlag{
Name: "user",
Usage: "Filter listed repositories absed on user, optional",
},
}, LoginOutputFlags...),
}
// runReposList list repositories
func runReposList(ctx *cli.Context) error {
login := initCommandLoginOnly()
mode := ctx.String("mode")
org := ctx.String("org")
user := ctx.String("user")
var rps []*gitea.Repository
var err error
if org != "" {
rps, err = login.Client().ListOrgRepos(org)
} else if user != "" {
rps, err = login.Client().ListUserRepos(user)
} else {
rps, err = login.Client().ListMyRepos()
}
if err != nil {
log.Fatal(err)
}
var repos []*gitea.Repository
if mode == "" {
repos = rps
} else if mode == "fork" {
for _, rp := range rps {
if rp.Fork == true {
repos = append(repos, rp)
}
}
} else if mode == "mirror" {
for _, rp := range rps {
if rp.Mirror == true {
repos = append(repos, rp)
}
}
} else if mode == "source" {
for _, rp := range rps {
if rp.Mirror != true && rp.Fork != true {
repos = append(repos, rp)
}
}
} else {
log.Fatal("Unknown mode: ", mode, "\nUse one of the following:\n- fork\n- mirror\n- source\n")
return nil
}
if len(rps) == 0 {
log.Fatal("No repositories found", rps)
return nil
}
headers := []string{
"Name",
"Type",
"SSH",
"Owner",
}
var values [][]string
for _, rp := range repos {
var mode = "source"
if rp.Fork {
mode = "fork"
}
if rp.Mirror {
mode = "mirror"
}
values = append(
values,
[]string{
rp.FullName,
mode,
rp.SSHURL,
rp.Owner.UserName,
},
)
}
Output(outputValue, headers, values)
return nil
}

View File

@ -39,6 +39,7 @@ func main() {
cmd.CmdIssues,
cmd.CmdPulls,
cmd.CmdReleases,
cmd.CmdRepos,
cmd.CmdLabels,
}
app.EnableBashCompletion = true