From 0bddabad9fe8af4b7bdf5b62e4c604f9622b688c Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Mon, 29 Apr 2019 00:50:22 +0200 Subject: [PATCH] migrated pulls to Cobra Signed-off-by: Andreas Ulm --- cmd/config.go | 5 +++++ cmd/pulls.go | 46 +++++++++++++++++++++++----------------------- cmd/root.go | 23 +++++++++++++++++++++-- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index b8c7561..75c4aa1 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 diff --git a/cmd/pulls.go b/cmd/pulls.go index 43e6245..14f5108 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -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 } diff --git a/cmd/root.go b/cmd/root.go index 2b16a43..284f7c6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 +}