2019-10-23 23:58:18 +08:00
|
|
|
// 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"
|
|
|
|
|
2020-09-16 21:47:52 +08:00
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
2020-01-05 01:44:25 +08:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-10-23 23:58:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// create global variables for global Flags to simplify
|
|
|
|
// access to the options without requiring cli.Context
|
|
|
|
var (
|
|
|
|
loginValue string
|
|
|
|
repoValue string
|
|
|
|
outputValue string
|
2019-10-27 05:29:37 +08:00
|
|
|
remoteValue string
|
2019-10-23 23:58:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// LoginFlag provides flag to specify tea login profile
|
|
|
|
var LoginFlag = cli.StringFlag{
|
2020-03-06 11:43:28 +08:00
|
|
|
Name: "login",
|
|
|
|
Aliases: []string{"l"},
|
2019-11-04 04:34:41 +08:00
|
|
|
Usage: "Use a different Gitea login. Optional",
|
2019-10-23 23:58:18 +08:00
|
|
|
Destination: &loginValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoFlag provides flag to specify repository
|
|
|
|
var RepoFlag = cli.StringFlag{
|
2020-03-06 11:43:28 +08:00
|
|
|
Name: "repo",
|
|
|
|
Aliases: []string{"r"},
|
2019-11-04 04:34:41 +08:00
|
|
|
Usage: "Repository to interact with. Optional",
|
2019-10-23 23:58:18 +08:00
|
|
|
Destination: &repoValue,
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:29:37 +08:00
|
|
|
// RemoteFlag provides flag to specify remote repository
|
|
|
|
var RemoteFlag = cli.StringFlag{
|
2020-03-06 11:43:28 +08:00
|
|
|
Name: "remote",
|
|
|
|
Aliases: []string{"R"},
|
2019-11-04 04:34:41 +08:00
|
|
|
Usage: "Discover Gitea login from remote. Optional",
|
2019-10-27 05:29:37 +08:00
|
|
|
Destination: &remoteValue,
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:58:18 +08:00
|
|
|
// OutputFlag provides flag to specify output type
|
|
|
|
var OutputFlag = cli.StringFlag{
|
2020-03-06 11:43:28 +08:00
|
|
|
Name: "output",
|
|
|
|
Aliases: []string{"o"},
|
2019-11-04 04:34:41 +08:00
|
|
|
Usage: "Output format. (csv, simple, table, tsv, yaml)",
|
2019-10-23 23:58:18 +08:00
|
|
|
Destination: &outputValue,
|
|
|
|
}
|
|
|
|
|
2020-09-21 13:03:20 +08:00
|
|
|
// StateFlag provides flag to specify issue/pr state, defaulting to "open"
|
|
|
|
var StateFlag = cli.StringFlag{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "Filter by state (all|open|closed)",
|
|
|
|
DefaultText: "open",
|
|
|
|
}
|
|
|
|
|
2020-09-27 20:07:46 +08:00
|
|
|
// PaginationPageFlag provides flag for pagination options
|
|
|
|
var PaginationPageFlag = cli.StringFlag{
|
|
|
|
Name: "page",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "specify page, default is 1",
|
|
|
|
}
|
|
|
|
|
|
|
|
// PaginationLimitFlag provides flag for pagination options
|
|
|
|
var PaginationLimitFlag = cli.StringFlag{
|
|
|
|
Name: "limit",
|
|
|
|
Aliases: []string{"lm"},
|
|
|
|
Usage: "specify limit of items per page",
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:58:18 +08:00
|
|
|
// 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{
|
2020-01-05 01:44:25 +08:00
|
|
|
&LoginFlag,
|
|
|
|
&OutputFlag,
|
2019-10-23 23:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2020-01-05 01:44:25 +08:00
|
|
|
&LoginFlag,
|
|
|
|
&RepoFlag,
|
|
|
|
&RemoteFlag,
|
2019-10-23 23:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2020-01-05 01:44:25 +08:00
|
|
|
&RepoFlag,
|
|
|
|
&RemoteFlag,
|
2019-10-23 23:58:18 +08:00
|
|
|
}, LoginOutputFlags...)
|
|
|
|
|
2020-09-21 13:03:20 +08:00
|
|
|
// IssuePRFlags defines flags that should be available on issue & pr listing flags.
|
|
|
|
var IssuePRFlags = append([]cli.Flag{
|
|
|
|
&StateFlag,
|
2020-09-27 20:07:46 +08:00
|
|
|
&PaginationPageFlag,
|
|
|
|
&PaginationLimitFlag,
|
2020-09-21 13:03:20 +08:00
|
|
|
}, AllDefaultFlags...)
|
|
|
|
|
2019-10-23 23:58:18 +08:00
|
|
|
// initCommand returns repository and *Login based on flags
|
|
|
|
func initCommand() (*Login, string, string) {
|
2020-09-17 00:59:20 +08:00
|
|
|
var login *Login
|
|
|
|
|
2020-07-18 18:43:56 +08:00
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("load config file failed ", yamlConfigPath)
|
|
|
|
}
|
|
|
|
|
2020-09-23 22:23:27 +08:00
|
|
|
if login, err = getDefaultLogin(); err != nil {
|
2020-09-16 21:47:52 +08:00
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
exist, err := utils.PathExists(repoValue)
|
2020-07-18 00:36:48 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
2020-07-18 18:43:56 +08:00
|
|
|
}
|
2019-10-23 23:58:18 +08:00
|
|
|
|
2020-09-16 21:47:52 +08:00
|
|
|
if exist || len(repoValue) == 0 {
|
2020-09-17 00:59:20 +08:00
|
|
|
login, repoValue, err = curGitRepoPath(repoValue)
|
2020-09-16 21:47:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:43:56 +08:00
|
|
|
if loginValue != "" {
|
|
|
|
login = getLoginByName(loginValue)
|
|
|
|
if login == nil {
|
|
|
|
log.Fatal("Login name " + loginValue + " does not exist")
|
|
|
|
}
|
2019-10-23 23:58:18 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 00:59:20 +08:00
|
|
|
owner, repo := getOwnerAndRepo(repoValue, login.User)
|
2019-10-23 23:58:18 +08:00
|
|
|
return login, owner, repo
|
|
|
|
}
|
2019-11-08 09:33:46 +08:00
|
|
|
|
|
|
|
// 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
|
2020-04-19 11:09:03 +08:00
|
|
|
if loginValue == "" {
|
2020-09-23 22:23:27 +08:00
|
|
|
login, err = getDefaultLogin()
|
2020-04-19 11:09:03 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
login = getLoginByName(loginValue)
|
|
|
|
if login == nil {
|
|
|
|
log.Fatal("Login name " + loginValue + " does not exist")
|
|
|
|
}
|
2019-11-08 09:33:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return login
|
|
|
|
}
|