generated from GDGVIT/template
/
build.go
163 lines (136 loc) · 4.49 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright © 2020 DSCVIT
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/GDGVIT/katamari/internal/utils"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"github.com/google/go-github/v32/github"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build your katamari project",
Long: `Fetch all repos from the specified organization, clone the READMEs and generate static pages ready for hosting!`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
org := viper.GetString("site")
isUser := viper.GetBool("isuser")
if org == "" {
utils.Err("config", fmt.Sprintf("configuration not found! make sure you have a %s file in the "+
"project dir.", chalk.Green.Color(".katamari.toml")))
os.Exit(1)
}
utils.Info("sill", fmt.Sprintf("fetching repos for %s", chalk.Green.Color(fmt.Sprintf("'%s'", org))))
err := os.MkdirAll("./content/readmes/", 0755)
if err != nil {
utils.Err("enoent", err.Error())
}
ctx := context.Background()
token := os.Getenv("GITHUB_ACCESS_TOKEN")
var client *github.Client
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
} else {
utils.Warn("config", "Access Token Missing. Set an access token in a .katamari/config.json, else you might be rate limited by GitHub [refer README]")
client = github.NewClient(nil)
}
orgOpt := &github.RepositoryListByOrgOptions{
Type: "public",
ListOptions: github.ListOptions{PerPage: 50},
}
usrOpt := &github.RepositoryListOptions{
Type: "public",
ListOptions: github.ListOptions{PerPage: 50},
}
var allRepos []*github.Repository
if isUser {
for {
repos, resp, err := client.Repositories.List(ctx, org, usrOpt)
if err != nil {
utils.Err("enoent", err.Error())
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
usrOpt.Page = resp.NextPage
}
} else {
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, org, orgOpt)
if err != nil {
utils.Err("enoent", err.Error())
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
orgOpt.Page = resp.NextPage
}
}
if err != nil {
utils.Err("enoent", err.Error())
os.Exit(1)
}
var wg sync.WaitGroup
for _, repo := range allRepos {
wg.Add(1)
utils.Info("sill", fmt.Sprintf("Fetching readme for repo %s", *repo.Name))
go func(client *github.Client, repo *github.Repository, wg *sync.WaitGroup) {
defer wg.Done()
readme, _, err := client.Repositories.GetReadme(context.Background(), org, *repo.Name, nil)
if err != nil {
utils.Err("enoent", err.Error())
return
}
content, err := readme.GetContent()
if err != nil {
utils.Err("enoent", err.Error())
return
}
f, err := os.Create(fmt.Sprintf("./content/readmes/%s.md", *repo.Name))
if err != nil {
utils.Err("enoent", err.Error())
return
}
_, _ = f.WriteString("---\n")
_, _ = f.WriteString(fmt.Sprintf("title: %s\n", *repo.Name))
_, _ = f.WriteString(fmt.Sprintf("date: %s\n", time.Now().UTC().Format("2006-01-02T15:04:05-0700")))
_, _ = f.WriteString(fmt.Sprintf("draft: false\n"))
_, _ = f.WriteString("---\n")
_, _ = f.WriteString(content)
}(client, repo, &wg)
}
wg.Wait()
utils.Info("sill", fmt.Sprintf("Fetched %d repos", len(allRepos)))
utils.Info("sill", chalk.Green.Color("Successfully built your katamari project!"))
utils.Info("sill", fmt.Sprintf("Run %s %s", chalk.Green.Color("hugo server"), chalk.White.Color("to run the hugo server")))
utils.Info("sill", fmt.Sprintf("To change theme refer %s", chalk.Green.Color("hugo documentation")))
},
}
func init() {
rootCmd.AddCommand(buildCmd)
}