2019-10-23 11:58:18 -04: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"
|
|
|
|
|
|
|
|
"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
|
2019-10-26 17:29:37 -04:00
|
|
|
remoteValue string
|
2019-10-23 11:58:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// LoginFlag provides flag to specify tea login profile
|
|
|
|
var LoginFlag = cli.StringFlag{
|
|
|
|
Name: "login, l",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Use a different Gitea login. Optional",
|
2019-10-23 11:58:18 -04:00
|
|
|
Destination: &loginValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoFlag provides flag to specify repository
|
|
|
|
var RepoFlag = cli.StringFlag{
|
|
|
|
Name: "repo, r",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Repository to interact with. Optional",
|
2019-10-23 11:58:18 -04:00
|
|
|
Destination: &repoValue,
|
|
|
|
}
|
|
|
|
|
2019-10-26 17:29:37 -04:00
|
|
|
// RemoteFlag provides flag to specify remote repository
|
|
|
|
var RemoteFlag = cli.StringFlag{
|
|
|
|
Name: "remote, R",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Discover Gitea login from remote. Optional",
|
2019-10-26 17:29:37 -04:00
|
|
|
Destination: &remoteValue,
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:58:18 -04:00
|
|
|
// OutputFlag provides flag to specify output type
|
|
|
|
var OutputFlag = cli.StringFlag{
|
|
|
|
Name: "output, o",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Output format. (csv, simple, table, tsv, yaml)",
|
2019-10-23 11:58:18 -04:00
|
|
|
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,
|
2019-12-18 06:21:29 -05:00
|
|
|
RemoteFlag,
|
2019-10-23 11:58:18 -04: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{
|
|
|
|
RepoFlag,
|
2019-10-26 17:29:37 -04:00
|
|
|
RemoteFlag,
|
2019-10-23 11:58:18 -04:00
|
|
|
}, LoginOutputFlags...)
|
|
|
|
|
|
|
|
// initCommand returns repository and *Login based on flags
|
|
|
|
func initCommand() (*Login, string, string) {
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
2019-11-03 15:34:41 -05:00
|
|
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
2019-10-23 11:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var login *Login
|
|
|
|
if loginValue == "" {
|
|
|
|
login, err = getActiveLogin()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
login = getLoginByName(loginValue)
|
|
|
|
if login == nil {
|
2019-11-03 15:34:41 -05:00
|
|
|
log.Fatal("Login name " + loginValue + " does not exist")
|
2019-10-23 11:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := repoValue
|
|
|
|
if repoPath == "" {
|
|
|
|
login, repoPath, err = curGitRepoPath()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, repo := splitRepo(repoPath)
|
|
|
|
return login, owner, repo
|
|
|
|
}
|
2019-11-07 20:33:46 -05: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
|
|
|
|
|
|
|
|
login = getLoginByName(loginValue)
|
|
|
|
if login == nil {
|
|
|
|
log.Fatal("indicated login name ", loginValue, " does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
return login
|
|
|
|
}
|