Skip to content

Commit 3972625

Browse files
authored
Merge pull request #5 from anqiansong/feat/data-export
feat: Support saving data to file
2 parents 9e61aef + e3c512e commit 3972625

25 files changed

Lines changed: 783 additions & 500 deletions

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
![banner](./resource/banner.png)
22

33
# github-compare
4+
45
A Command-line tool to statistics the GitHub repositories
56

67
## Install
@@ -10,14 +11,80 @@ $ go install github.com/anqiansong/github-compare@latest
1011
```
1112

1213
## Example
14+
15+
### TableView
16+
1317
```bash
14-
$ github-compare zeromicro/go-zero go-kratos/kratos asim/go-micro go-kit/kit
18+
$ github-compare spf13/cobra urfave/cli junegunn/fzf antonmedv/fx
1519
```
1620
![preview](./resource/compare-preview.png)
1721

22+
### JSONView
23+
24+
```bash
25+
$ github-compare spf13/cobra --json
26+
[
27+
{
28+
"age": "3187 days",
29+
"avgReleasePeriod": "199 days",
30+
"contributorCount": "246",
31+
"forkCount": "2327(0/d)",
32+
"fullName": "spf13/cobra",
33+
"homepage": "https://cobra.dev",
34+
"issue": "107/892",
35+
"language": "Go",
36+
"lastPushedAt": "8 hour(s) ago",
37+
"latestReleaseAt": "2 month(s) ago",
38+
"lastUpdatedAt": "1 hour(s) ago",
39+
"latestDayStarCount": "14 ⇊",
40+
"latestMonthStarCount": "477",
41+
"latestWeekStarCount": "110 ⇈",
42+
"license": "Apache License 2.0",
43+
"pull": "55/808",
44+
"releaseCount": "16",
45+
"starCount": "26774(8/d)",
46+
"watcherCount": "350"
47+
}
48+
]
49+
```
50+
51+
### YAMLView
52+
53+
```bash
54+
$ github-compare spf13/cobra --yaml
55+
- age: 3187 days
56+
avgreleaseperiod: 199 days
57+
contributorcount: "246"
58+
forkcount: 2327(0/d)
59+
fullname: spf13/cobra
60+
homepage: https://cobra.dev
61+
issue: 107/892
62+
language: Go
63+
lastpushedat: 8 hour(s) ago
64+
latestreleaseat: 2 month(s) ago
65+
lastupdatedat: 1 hour(s) ago
66+
latestdaystarcount: 14 ⇊
67+
latestmonthstarcount: "477"
68+
latestweekstarcount: 110 ⇈
69+
license: Apache License 2.0
70+
pull: 55/808
71+
releasecount: "16"
72+
starcount: 26774(8/d)
73+
watchercount: "350"
74+
```
75+
76+
### Export as a csv file
77+
78+
```bash
79+
$ github-compare spf13/cobra urfave/cli junegunn/fzf antonmedv/fx -f data.csv
80+
```
81+
![csv](./resource/compare-csv.png)
82+
83+
1884
## Usage
1985

2086
### Preparation
87+
2188
1. [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)
2289
2. Set access token token
2390
- Copied the access token and export to environment
@@ -34,6 +101,24 @@ $ export GITHUB_ACCESS_TOKEN=${GITHUB_ACCESS_TOKEN}
34101
$ github-compare zeromicro/go-zero
35102
```
36103

104+
### Commands
105+
106+
```bash
107+
$ github-compare -h
108+
A cli tool to compare two github repositories
109+
110+
Usage:
111+
github-compare [flags]
112+
113+
Flags:
114+
-f, --file string output to a specified file
115+
-h, --help help for github-compare
116+
--json print with json style
117+
--table print with table style(default) (default true)
118+
-t, --token string github access token
119+
--yaml print with yaml style
120+
```
121+
37122
## Note
38123

39124
1. A GitHub personal access token is required.
@@ -42,9 +127,11 @@ $ github-compare zeromicro/go-zero
42127
environment key `GITHUB_ACCESS_TOKEN`
43128

44129
## Last
130+
45131
If this repository can help you, give a star please!
46132

47133
Thanks all!
48134

49135
## License
136+
50137
[MIT License](License)

cmd/export.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 anqiansong
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package cmd
24+
25+
import (
26+
"bytes"
27+
"encoding/json"
28+
"fmt"
29+
"io/ioutil"
30+
"path/filepath"
31+
32+
"github.com/anqiansong/github-compare/pkg/stat"
33+
"gopkg.in/yaml.v3"
34+
)
35+
36+
const (
37+
exportTPJSON = "json"
38+
exportTPYAML = "yaml"
39+
exportTPCSV = "csv"
40+
)
41+
42+
var outputFile string
43+
44+
func export(data []stat.Data, tp string) error {
45+
var buffer bytes.Buffer
46+
switch tp {
47+
case exportTPJSON:
48+
marshal, _ := json.MarshalIndent(data, "", " ")
49+
buffer.Write(marshal)
50+
case exportTPYAML:
51+
marshal, _ := yaml.Marshal(data)
52+
buffer.Write(marshal)
53+
case exportTPCSV:
54+
list, err := convert2ViperList(data)
55+
if err != nil {
56+
return err
57+
}
58+
t := createTable(list, false)
59+
buffer.WriteString(t.RenderCSV())
60+
default:
61+
return fmt.Errorf("invalid type %q", tp)
62+
}
63+
64+
return outputOrPrint(outputFile, buffer)
65+
}
66+
67+
func outputOrPrint(file string, buffer bytes.Buffer) error {
68+
if len(file) == 0 {
69+
fmt.Println(buffer.String())
70+
return nil
71+
}
72+
73+
abs, err := filepath.Abs(file)
74+
if err != nil {
75+
return err
76+
}
77+
78+
return ioutil.WriteFile(abs, buffer.Bytes(), 0666)
79+
}

cmd/pretty.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 anqiansong
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package cmd
24+
25+
type style string
26+
27+
const (
28+
styleJSON style = "json"
29+
styleYAML style = "yaml"
30+
styleTable style = "table"
31+
)

0 commit comments

Comments
 (0)