@@ -33,8 +33,6 @@ import (
3333 "github.com/spf13/cobra"
3434)
3535
36- const codeFailure = 1
37-
3836var (
3937 githubAccessToken string
4038 jsonStyle bool
@@ -43,37 +41,18 @@ var (
4341
4442 rootCmd = & cobra.Command {
4543 Use : "github-compare" ,
46- Short : "A cli tool to compare two github repositories" ,
44+ Short : rootCMDDesc ,
4745 Args : cobra .RangeArgs (1 , 4 ),
48- RunE : func (cmd * cobra.Command , args []string ) error {
49- if err := validateGithubRepo (args ... ); err != nil {
50- return err
51- }
52-
53- printStyle := styleTermUI
54- if jsonStyle {
55- printStyle = styleJSON
56- } else if yamlStyle {
57- printStyle = styleYAML
58- }
59-
60- // Only rendering color when print to terminal and there are more than 1 repositories
61- renderColor := printStyle == styleTermUI && len (outputFile ) == 0 && len (args ) > 1
62- data , err := getData (renderColor , args ... )
63- if err != nil {
64- return err
65- }
66-
67- if len (outputFile ) > 0 {
68- tp := getExportType (outputFile , printStyle )
69- return export (data , tp )
70- }
71-
72- return render (printStyle , data ... )
73- },
46+ RunE : run ,
7447 }
7548)
7649
50+ func Execute () {
51+ if err := rootCmd .Execute (); err != nil {
52+ os .Exit (codeFailure )
53+ }
54+ }
55+
7756func getExportType (outputFile string , printStyle style ) string {
7857 ext := strings .TrimPrefix (filepath .Ext (outputFile ), "." )
7958 switch strings .ToLower (ext ) {
@@ -91,19 +70,14 @@ func getExportType(outputFile string, printStyle style) string {
9170 }
9271}
9372
94- func init () {
95- rootCmd .PersistentFlags ().StringVarP (& githubAccessToken , "token" , "t" , "" ,
96- "github access token" )
97- rootCmd .PersistentFlags ().BoolVar (& termUIStyle , "ui" , true , "print with term ui style(default)" )
98- rootCmd .PersistentFlags ().BoolVar (& jsonStyle , "json" , false , "print with json style" )
99- rootCmd .PersistentFlags ().BoolVar (& yamlStyle , "yaml" , false , "print with yaml style" )
100- rootCmd .PersistentFlags ().StringVarP (& outputFile , "file" , "f" , "" , "output to a specified file" )
101- rootCmd .Version = version
102- }
103-
104- func Execute () {
105- if err := rootCmd .Execute (); err != nil {
106- os .Exit (codeFailure )
73+ func getPrintStyle () style {
74+ switch {
75+ case jsonStyle :
76+ return styleJSON
77+ case yamlStyle :
78+ return styleYAML
79+ default :
80+ return styleTermUI
10781 }
10882}
10983
@@ -115,3 +89,36 @@ func getData(renderColor bool, args ...string) ([]stat.Data, error) {
11589 s .Stop ()
11690 return data , nil
11791}
92+
93+ func init () {
94+ persistentFlags := rootCmd .PersistentFlags ()
95+ persistentFlags .StringVarP (& githubAccessToken , flagToken , flagTokenShortHand ,
96+ defaultEmptyString , flagTokenDesc )
97+ persistentFlags .BoolVar (& termUIStyle , styleTermUI , true , flagTermUIDesc )
98+ persistentFlags .BoolVar (& jsonStyle , styleJSON , false , flagJSONDesc )
99+ persistentFlags .BoolVar (& yamlStyle , styleYAML , false , flagYAMLDesc )
100+ persistentFlags .StringVarP (& outputFile , flagFile , flagFileShortHand , defaultEmptyString ,
101+ flagFileDesc )
102+ rootCmd .Version = version
103+ }
104+
105+ func run (_ * cobra.Command , args []string ) error {
106+ if err := validateGithubRepo (args ... ); err != nil {
107+ return err
108+ }
109+
110+ printStyle := getPrintStyle ()
111+ // Only rendering color when print to terminal and there are more than 1 repositories
112+ renderColor := printStyle == styleTermUI && len (outputFile ) == 0 && len (args ) > 1
113+ data , err := getData (renderColor , args ... )
114+ if err != nil {
115+ return err
116+ }
117+
118+ if len (outputFile ) > 0 {
119+ tp := getExportType (outputFile , printStyle )
120+ return export (data , tp )
121+ }
122+
123+ return render (printStyle , data ... )
124+ }
0 commit comments