migrated pulls to Cobra

Signed-off-by: Andreas Ulm <andreas.ulm@root360.de>
This commit is contained in:
Andreas Ulm 2019-04-29 00:50:22 +02:00
parent 8a2b3a93d7
commit 0bddabad9f
3 changed files with 49 additions and 25 deletions

View File

@ -23,6 +23,7 @@ import (
git_config "gopkg.in/src-d/go-git.v4/config"
"github.com/go-gitea/yaml"
"github.com/spf13/viper"
)
// Login represents a login to a gitea server, you even could add multiple logins for one gitea server
@ -112,6 +113,10 @@ func getActiveLogin() (*Login, error) {
}
func getLoginByName(name string) *Login {
err := viper.Unmarshal(&config)
if err != nil {
return nil
}
for _, l := range config.Logins {
if l.Name == name {
return &l

View File

@ -9,30 +9,33 @@ import (
"log"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)
// 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`,
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",
},
},
var loginName string
var repoPath string
func init() {
rootCmd.AddCommand(pullCmd)
rootCmd.PersistentFlags().StringVarP(&loginName, "login", "l", "", "Indicate one login, optional when inside a gitea repository")
rootCmd.PersistentFlags().StringVarP(&repoPath, "repo", "r", "", "Indicate one repository, optional when inside a gitea repository")
}
func runPulls(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
// pullCmd represents the pull command
var pullCmd = &cobra.Command{
Use: "pulls",
Short: "Operate with pulls of the repository",
Long: `Operate with pulls of the repository`,
Run: runPulls,
}
func runPulls(cmd *cobra.Command, args []string) {
login := getLoginByName(loginName)
if login == nil {
Errorf("Login '%s' not found in config\n", loginName)
return
}
owner, repo := getOwnerRepo()
prs, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
Page: 0,
@ -45,7 +48,6 @@ func runPulls(ctx *cli.Context) error {
if len(prs) == 0 {
fmt.Println("No pull requests left")
return nil
}
for _, pr := range prs {
@ -58,6 +60,4 @@ func runPulls(ctx *cli.Context) error {
}
fmt.Printf("#%d\t%s\t%s\t%s\n", pr.Index, name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title)
}
return nil
}

View File

@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"
@ -15,6 +16,7 @@ import (
)
var cfgFile string
var debug bool
// Version holds the current Gitea version
var Version = "0.1.0-dev"
@ -46,7 +48,8 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tea/tea.yaml)")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.tea/tea.yaml)")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "config file (default is $HOME/.tea/tea.yaml)")
if len(Tags) > 0 {
Version += " built with: " + strings.Replace(Tags, " ", ", ", -1)
}
@ -75,6 +78,22 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if debug {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
}
func getOwnerRepo() (string, string) {
var err error
repoPath := rootCmd.PersistentFlags().Lookup("repo").Value.String()
if repoPath == "" {
_, repoPath, err = curGitRepoPath()
if err != nil {
log.Fatal(err.Error())
}
}
owner, repo := splitRepo(repoPath)
return owner, repo
}