Skip to content

Commit

Permalink
show: Convert to using flagConfig()
Browse files Browse the repository at this point in the history
Add a general cobra command PersistentPreRun that can be called by any lab
command, and convert the 'mr show' and 'issue show' commands to use
flagConfig().

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 24, 2020
1 parent 47de8c5 commit 928ac90
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
14 changes: 6 additions & 8 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
)

var issueShowCmd = &cobra.Command{
Use: "show [remote] <id>",
Aliases: []string{"get"},
ArgAliases: []string{"s"},
Short: "Describe an issue",
Long: ``,
Use: "show [remote] <id>",
Aliases: []string{"get"},
ArgAliases: []string{"s"},
Short: "Describe an issue",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {

rn, issueNum, err := parseArgs(args)
Expand All @@ -44,9 +45,6 @@ var issueShowCmd = &cobra.Command{
printIssue(issue, rn, renderMarkdown)

showComments, _ := cmd.Flags().GetBool("comments")
if showComments == false {
showComments = getMainConfig().GetBool(CommandPrefix + "comments")
}
if showComments {
discussions, err := lab.IssueListDiscussions(rn, int(issueNum))
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ var (
)

var mrShowCmd = &cobra.Command{
Use: "show [remote] <id>",
Aliases: []string{"get"},
ArgAliases: []string{"s"},
Short: "Describe a merge request",
Long: ``,
Use: "show [remote] <id>",
Aliases: []string{"get"},
ArgAliases: []string{"s"},
Short: "Describe a merge request",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {

rn, mrNum, err := parseArgs(args)
Expand Down Expand Up @@ -68,9 +69,6 @@ var mrShowCmd = &cobra.Command{
}

showComments, _ := cmd.Flags().GetBool("comments")
if showComments == false {
showComments = getMainConfig().GetBool(CommandPrefix + "comments")
}
if showComments {
discussions, err := lab.MRListDiscussions(rn, int(mrNum))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/zaquestion/lab/internal/config"
Expand Down Expand Up @@ -69,3 +70,7 @@ func textToMarkdown(text string) string {
text = strings.Replace(text, "\n", " \n", -1)
return text
}

func LabPersistentPreRun(cmd *cobra.Command, args []string) {
flagConfig(cmd.Flags())
}

0 comments on commit 928ac90

Please sign in to comment.