From 4a61afe5586d49b22a4ddb8f3de88a250ea10107 Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Thu, 2 May 2019 07:44:57 +0200 Subject: [PATCH] added global appendable Flags (#12) Signed-off-by: Andreas Ulm --- cmd/flags.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/issues.go | 60 ++++------------------------------- cmd/pulls.go | 13 ++------ cmd/releases.go | 19 +++-------- 4 files changed, 97 insertions(+), 79 deletions(-) create mode 100644 cmd/flags.go diff --git a/cmd/flags.go b/cmd/flags.go new file mode 100644 index 0000000..6f41a03 --- /dev/null +++ b/cmd/flags.go @@ -0,0 +1,84 @@ +// 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 ( + "github.com/urfave/cli" + "log" +) + +// 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: "Indicate one repository, optional when inside a gitea repository", + Destination: &outputValue, +} + +// DefaultFlags defines flags that should be available +// for all subcommands +var DefaultFlags = []cli.Flag{ + LoginFlag, + OutputFlag, +} + +// RepoDefaultFlags defines flags that should be available +// for all subcommands working with dedicated repositories +var RepoDefaultFlags = append([]cli.Flag{ + RepoFlag, +}, DefaultFlags...) + +// 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 +} diff --git a/cmd/issues.go b/cmd/issues.go index 3f343e6..3cceb39 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -26,16 +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", - }, - }, + Flags: append([]cli.Flag{}, RepoDefaultFlags...), } // CmdIssuesList represents a sub command of issues to list issues @@ -54,7 +45,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:] @@ -80,7 +71,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, @@ -113,7 +104,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", @@ -122,50 +113,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 + }, RepoDefaultFlags...), } 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"), diff --git a/cmd/pulls.go b/cmd/pulls.go index 43e6245..4a5195d 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -19,20 +19,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", - }, - }, + Flags: append([]cli.Flag{}, RepoDefaultFlags...), } 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, diff --git a/cmd/releases.go b/cmd/releases.go index 61cfe21..d6ee10e 100644 --- a/cmd/releases.go +++ b/cmd/releases.go @@ -24,20 +24,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", - }, - }, + Flags: append([]cli.Flag{}, RepoDefaultFlags...), } 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 { @@ -65,7 +56,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", @@ -94,11 +85,11 @@ var CmdReleaseCreate = cli.Command{ Name: "asset, a", Usage: "a list of files to attach to the release", }, - }, + }, RepoDefaultFlags...), } 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"),