Skip to content

Commit c00ce3a

Browse files
committed
Format code
1 parent 2c17f63 commit c00ce3a

File tree

13 files changed

+185
-113
lines changed

13 files changed

+185
-113
lines changed

cmd/export.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ func export(data []stat.Data, tp string) error {
5151
marshal, _ := yaml.Marshal(data)
5252
buffer.Write(marshal)
5353
case exportTPCSV:
54-
list, err := convert2ViperList(data)
54+
t, err := createTable(data, false, true)
5555
if err != nil {
5656
return err
5757
}
58-
t := createTable(list, false, true)
5958
// solve garbled characters
6059
buffer.WriteString("\xEF\xBB\xBF")
6160
buffer.WriteString(t.RenderCSV())

cmd/pretty.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,22 @@
2222

2323
package cmd
2424

25-
type style string
25+
type style = string
2626

2727
const (
28+
codeFailure = 1
29+
defaultEmptyString = ""
30+
flagFile = "file"
31+
flagFileShortHand = "f"
32+
flagToken = "token"
33+
flagTokenShortHand = "t"
34+
rootCMDDesc = "A cli tool to compare two github repositories"
35+
flagTokenDesc = "github access token"
36+
flagTermUIDesc = "print with term ui style(default)"
37+
flagJSONDesc = "print with json style"
38+
flagYAMLDesc = "print with yaml style"
39+
flagFileDesc = "output to a specified file"
40+
2841
styleJSON style = "json"
2942
styleYAML style = "yaml"
3043
styleTermUI style = "ui"

cmd/render.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ func render(printStyle style, list ...stat.Data) error {
4646
data, _ := yaml.Marshal(list)
4747
prettyText = string(data)
4848
default:
49-
data, err := convert2ViperList(list)
50-
if err != nil {
51-
return err
49+
if len(list) == 1 {
50+
return renderDetail(list[0])
5251
}
5352

54-
if len(data) == 1 {
55-
return renderDetail(list[0], data[0])
53+
t, err := createTable(list, true, false)
54+
if err != nil {
55+
return err
5656
}
5757

58-
t := createTable(data, true, false)
5958
t.SetStyle(table.StyleLight)
6059
prettyText = t.Render()
6160
}
@@ -72,10 +71,16 @@ func convert2ViperList(list []stat.Data) ([]*viper.Viper, error) {
7271
}
7372
data = append(data, v)
7473
}
74+
7575
return data, nil
7676
}
7777

78-
func createTable(data []*viper.Viper, emoji bool, exportCSV bool) table.Writer {
78+
func createTable(list []stat.Data, emoji bool, exportCSV bool) (table.Writer, error) {
79+
data, err := convert2ViperList(list)
80+
if err != nil {
81+
return nil, err
82+
}
83+
7984
t := table.NewWriter()
8085
t.AppendHeader(createRow("name", "fullName", false, data...))
8186
if exportCSV {
@@ -108,20 +113,24 @@ func createTable(data []*viper.Viper, emoji bool, exportCSV bool) table.Writer {
108113
createRow("lastCommit", "lastPushedAt", emoji, data...),
109114
createRow("lastUpdate", "lastUpdatedAt", emoji, data...),
110115
})
111-
return t
116+
117+
return t, nil
112118
}
113119

114120
func convert2Viper(e stat.Data) (*viper.Viper, error) {
115121
v := viper.New()
116122
v.SetConfigType("json")
123+
117124
d, err := json.Marshal(e)
118125
if err != nil {
119126
return nil, err
120127
}
128+
121129
err = v.ReadConfig(bytes.NewBuffer(d))
122130
if err != nil {
123131
return nil, err
124132
}
133+
125134
return v, nil
126135
}
127136

@@ -150,22 +159,21 @@ func createRow(title string, field string, emoji bool, data ...*viper.Viper) tab
150159
if emoji {
151160
title = emojiMap[field] + title
152161
}
162+
153163
ret := table.Row{title}
154164
for _, e := range data {
155165
ret = append(ret, e.Get(field))
156166
}
167+
157168
return ret
158169
}
159170

160-
func createCSVRow(title string, field string, data ...*viper.Viper) []string {
161-
ret := []string{title}
162-
for _, e := range data {
163-
ret = append(ret, fmt.Sprintf("%v", e.Get(field)))
171+
func renderDetail(st stat.Data) error {
172+
data, err := convert2Viper(st)
173+
if err != nil {
174+
return err
164175
}
165-
return ret
166-
}
167176

168-
func renderDetail(st stat.Data, data *viper.Viper) error {
169177
if err := ui.Init(); err != nil {
170178
return err
171179
}
@@ -256,7 +264,7 @@ func renderDetail(st stat.Data, data *viper.Viper) error {
256264
for {
257265
e := <-uiEvents
258266
switch e.ID {
259-
case "q", "<C-c>":
267+
case "q", "<C-c>", "<Escape>":
260268
ui.Clear()
261269
return nil
262270
case "<Resize>":
@@ -298,6 +306,7 @@ func createBarChart(data stat.Chart, title string, titleColor ui.Color,
298306
}
299307
return maxVal + 2
300308
}
309+
301310
bar := widgets.NewBarChart()
302311
bar.Title = title
303312
bar.Data = data.Data
@@ -314,6 +323,7 @@ func createBarChart(data stat.Chart, title string, titleColor ui.Color,
314323
return colorList
315324
}()
316325
}
326+
317327
bar.NumStyles = []ui.Style{ui.NewStyle(ui.ColorBlack)}
318328
return bar
319329
}

cmd/root.go

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
"github.com/spf13/cobra"
3434
)
3535

36-
const codeFailure = 1
37-
3836
var (
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+
7756
func 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+
}

cmd/validate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func validateGithubRepo(name ...string) error {
3636
if len(all) > 0 && all[0] == e {
3737
continue
3838
}
39+
3940
return fmt.Errorf("invalid github repo name: %s", e)
4041
}
42+
4143
return nil
4244
}

pkg/stat/commit.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
type CommitList []*github.RepositoryCommit
3333

3434
func (c CommitList) Chart() Chart {
35-
3635
var (
3736
labels []string
3837
data []float64
@@ -50,30 +49,36 @@ func (c CommitList) Chart() Chart {
5049
}
5150

5251
func (c CommitList) getSpecifiedDate(date time.Time) int {
53-
zero := timex.Truncate(date)
54-
var count int
52+
var (
53+
count int
54+
zero = timex.Truncate(date)
55+
)
56+
5557
for _, e := range c {
5658
commit := e.Commit
5759
if commit == nil {
5860
continue
5961
}
62+
6063
committer := commit.Author
6164
if committer == nil {
6265
continue
6366
}
67+
6468
if timex.Truncate(committer.GetDate()).Equal(zero) {
6569
count += 1
6670
}
6771
}
72+
6873
return count
6974
}
7075

7176
func (s Stat) latestWeekCommits() CommitList {
7277
var (
78+
page = 1
7379
list CommitList
7480
until = time.Now()
75-
since = time.Now().Add(-7 * 24 * time.Hour)
76-
page = 1
81+
since = time.Now().Add(-timeWeek)
7782
)
7883

7984
for {
@@ -94,6 +99,7 @@ func (s Stat) latestWeekCommits() CommitList {
9499
if page >= resp.LastPage {
95100
return list
96101
}
102+
97103
page = resp.NextPage
98104
}
99105
}

0 commit comments

Comments
 (0)