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" git_config "gopkg.in/src-d/go-git.v4/config"
"github.com/go-gitea/yaml" "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 // 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 { func getLoginByName(name string) *Login {
err := viper.Unmarshal(&config)
if err != nil {
return nil
}
for _, l := range config.Logins { for _, l := range config.Logins {
if l.Name == name { if l.Name == name {
return &l return &l

View File

@ -9,30 +9,33 @@ import (
"log" "log"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"github.com/urfave/cli"
) )
// CmdPulls represents to login a gitea server. var loginName string
var CmdPulls = cli.Command{ var repoPath string
Name: "pulls",
Usage: "Operate with pulls of the repository", func init() {
Description: `Operate with pulls of the repository`, rootCmd.AddCommand(pullCmd)
Action: runPulls, rootCmd.PersistentFlags().StringVarP(&loginName, "login", "l", "", "Indicate one login, optional when inside a gitea repository")
Flags: []cli.Flag{ rootCmd.PersistentFlags().StringVarP(&repoPath, "repo", "r", "", "Indicate one repository, optional when inside a gitea repository")
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",
},
},
} }
func runPulls(ctx *cli.Context) error { // pullCmd represents the pull command
login, owner, repo := initCommand(ctx) 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{ prs, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
Page: 0, Page: 0,
@ -45,7 +48,6 @@ func runPulls(ctx *cli.Context) error {
if len(prs) == 0 { if len(prs) == 0 {
fmt.Println("No pull requests left") fmt.Println("No pull requests left")
return nil
} }
for _, pr := range prs { 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) 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 ( import (
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
@ -15,6 +16,7 @@ import (
) )
var cfgFile string var cfgFile string
var debug bool
// Version holds the current Gitea version // Version holds the current Gitea version
var Version = "0.1.0-dev" var Version = "0.1.0-dev"
@ -46,7 +48,8 @@ func init() {
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // 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 { if len(Tags) > 0 {
Version += " built with: " + strings.Replace(Tags, " ", ", ", -1) Version += " built with: " + strings.Replace(Tags, " ", ", ", -1)
} }
@ -75,6 +78,22 @@ func initConfig() {
// If a config file is found, read it in. // If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {
if debug {
fmt.Println("Using config file:", viper.ConfigFileUsed()) 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
}