added global appendable Flags (#12) (#39)

This commit is contained in:
root360-AndreasUlm 2019-10-23 15:58:18 +00:00 committed by techknowlogick
parent bbcc689b93
commit 7df0354d6c
5 changed files with 116 additions and 98 deletions

98
cmd/flags.go Normal file
View File

@ -0,0 +1,98 @@
// 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"
"github.com/urfave/cli"
)
// create global variables for global Flags to simplify
// access to the options without requiring cli.Context
var (
loginValue string
repoValue string
outputValue string
)
// 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",
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",
Destination: &repoValue,
}
// OutputFlag provides flag to specify output type
var OutputFlag = cli.StringFlag{
Name: "output, o",
Usage: "Specify output format. (csv, simple, table, tsv, yaml)",
Destination: &outputValue,
}
// LoginOutputFlags defines login and output flags that should
// added to all subcommands and appended to the flags of the
// subcommand to work around issue and provide --login and --output:
// https://github.com/urfave/cli/issues/585
var LoginOutputFlags = []cli.Flag{
LoginFlag,
OutputFlag,
}
// LoginRepoFlags defines login and repo flags that should
// be used for all subcommands and appended to the flags of
// the subcommand to work around issue and provide --login and --repo:
// https://github.com/urfave/cli/issues/585
var LoginRepoFlags = []cli.Flag{
LoginFlag,
RepoFlag,
}
// AllDefaultFlags defines flags that should be available
// for all subcommands working with dedicated repositories
// to work around issue and provide --login, --repo and --output:
// https://github.com/urfave/cli/issues/585
var AllDefaultFlags = append([]cli.Flag{
RepoFlag,
}, LoginOutputFlags...)
// initCommand returns repository and *Login based on flags
func initCommand() (*Login, string, string) {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed ", yamlConfigPath)
}
var login *Login
if loginValue == "" {
login, err = getActiveLogin()
if err != nil {
log.Fatal(err)
}
} else {
login = getLoginByName(loginValue)
if login == nil {
log.Fatal("indicated login name ", loginValue, " does not exist")
}
}
repoPath := repoValue
if repoPath == "" {
login, repoPath, err = curGitRepoPath()
if err != nil {
log.Fatal(err.Error())
}
}
owner, repo := splitRepo(repoPath)
return login, owner, repo
}

View File

@ -26,21 +26,7 @@ var CmdIssues = cli.Command{
CmdIssuesList,
CmdIssuesCreate,
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
Flags: AllDefaultFlags,
}
// CmdIssuesList represents a sub command of issues to list issues
@ -49,6 +35,7 @@ var CmdIssuesList = cli.Command{
Usage: "List issues of the repository",
Description: `List issues of the repository`,
Action: runIssuesList,
Flags: AllDefaultFlags,
}
func runIssues(ctx *cli.Context) error {
@ -59,7 +46,7 @@ func runIssues(ctx *cli.Context) error {
}
func runIssueDetail(ctx *cli.Context, index string) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
if strings.HasPrefix(index, "#") {
index = index[1:]
@ -85,7 +72,7 @@ func runIssueDetail(ctx *cli.Context, index string) error {
}
func runIssuesList(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
issues, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
Page: 0,
@ -136,7 +123,7 @@ var CmdIssuesCreate = cli.Command{
Usage: "Create an issue on repository",
Description: `Create an issue on repository`,
Action: runIssuesCreate,
Flags: []cli.Flag{
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "title, t",
Usage: "issue title to create",
@ -145,50 +132,11 @@ var CmdIssuesCreate = cli.Command{
Name: "body, b",
Usage: "issue body to create",
},
},
}
func initCommand(ctx *cli.Context) (*Login, string, string) {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}
var login *Login
if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
login, err = getActiveLogin()
if err != nil {
log.Fatal(err)
}
} else {
login = getLoginByName(loginFlag)
if login == nil {
log.Fatal("indicated login name", loginFlag, "does not exist")
}
}
repoPath := getGlobalFlag(ctx, "repo")
if repoPath == "" {
login, repoPath, err = curGitRepoPath()
if err != nil {
log.Fatal(err.Error())
}
}
owner, repo := splitRepo(repoPath)
return login, owner, repo
}
func getGlobalFlag(ctx *cli.Context, flag string) string {
var val = ctx.String(flag)
if val == "" {
return ctx.GlobalString(flag)
}
return val
}, LoginRepoFlags...),
}
func runIssuesCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
_, err := login.Client().CreateIssue(owner, repo, gitea.CreateIssueOption{
Title: ctx.String("title"),

View File

@ -44,7 +44,7 @@ var CmdLabels = cli.Command{
}
func runLabels(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
labels, err := login.Client().ListRepoLabels(owner, repo)
if err != nil {
@ -124,7 +124,7 @@ func splitLabelLine(line string) (string, string, string) {
}
func runLabelCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
labelFile := ctx.String("file")
var err error
@ -195,7 +195,7 @@ var CmdLabelUpdate = cli.Command{
}
func runLabelUpdate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
id := ctx.Int64("id")
var pName, pColor, pDescription *string
@ -243,7 +243,7 @@ var CmdLabelDelete = cli.Command{
}
func runLabelDelete(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
err := login.Client().DeleteLabel(owner, repo, ctx.Int64("id"))
if err != nil {

View File

@ -21,25 +21,11 @@ var CmdPulls = cli.Command{
Usage: "Operate with pulls of the repository",
Description: `Operate with pulls of the repository`,
Action: runPulls,
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
Flags: AllDefaultFlags,
}
func runPulls(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
prs, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
Page: 0,

View File

@ -23,25 +23,11 @@ var CmdReleases = cli.Command{
Subcommands: []cli.Command{
CmdReleaseCreate,
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
Flags: AllDefaultFlags,
}
func runReleases(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
releases, err := login.Client().ListReleases(owner, repo)
if err != nil {
@ -84,7 +70,7 @@ var CmdReleaseCreate = cli.Command{
Usage: "Create a release in repository",
Description: `Create a release in repository`,
Action: runReleaseCreate,
Flags: []cli.Flag{
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "tag",
Usage: "release tag name",
@ -113,11 +99,11 @@ var CmdReleaseCreate = cli.Command{
Name: "asset, a",
Usage: "a list of files to attach to the release",
},
},
}, LoginRepoFlags...),
}
func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
login, owner, repo := initCommand()
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
TagName: ctx.String("tag"),