-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
158 lines (137 loc) · 3.89 KB
/
gen.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
package main
import (
"context"
"flag"
"fmt"
"html/template"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"unicode/utf8"
git "github.com/go-git/go-git/v5"
github "github.com/google/go-github/v33/github"
)
var (
outputDir = flag.String("out", "./out", "Output directory")
domainName = flag.String("domain", "", "Domain name; overrides DOMAIN_NAME env variable")
githubUsername = flag.String("gh-user", "", "GitHub username; overrides GITHUB_ACTOR env variable")
indexTemplate = template.Must(template.ParseFiles("templates/index.html"))
redirectTemplate = template.Must(template.ParseFiles("templates/redirect.html"))
)
func main() {
start := time.Now()
defer fmt.Printf("Done in %v!\n", time.Since(start))
flag.Parse()
if len(*domainName) == 0 {
*domainName = os.Getenv("DOMAIN_NAME")
if len(*domainName) == 0 {
log.Fatal("Domain name must be specified in DOMAIN_NAME env variable")
}
}
if len(*githubUsername) == 0 {
*githubUsername = os.Getenv("GITHUB_ACTOR")
if len(*githubUsername) == 0 {
log.Fatal("GitHub username must be specified in GITHUB_ACTOR env variable")
}
}
fmt.Printf("Got configuration [domainName=%s, githubUsername=%s]\n", *domainName, *githubUsername)
fmt.Printf("Generating the files (at %s)...\n", *outputDir)
if err := os.MkdirAll(*outputDir, 0777); err != nil {
log.Fatal(err)
}
generateIndexFile(*githubUsername)
for _, p := range getRepositories(*githubUsername) {
if p.Language != nil && *p.Language == "Go" && p.Private != nil && !*p.Private {
fmt.Printf("> Found public Go repository \"%s\". Generating paths...\n", *p.Name)
for _, repoPath := range getRepositoryPaths(p) {
generateRedirectFile(*domainName, *p.Name, *githubUsername, repoPath)
}
}
}
}
func generateIndexFile(githubUsername string) {
filePath := path.Join(*outputDir, "index.html")
fmt.Printf(" + %s", "index.html\n")
file, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
err = indexTemplate.Execute(file, struct {
GitHubUsername string
}{
githubUsername,
})
if err != nil {
log.Fatal(err)
}
}
func generateRedirectFile(domainName, packageName, githubUsername, outputPath string) {
filePath := path.Join(*outputDir, outputPath+".html")
fmt.Printf(" + %s", outputPath+".html\n")
if err := os.MkdirAll(path.Dir(filePath), 0777); err != nil {
log.Fatal(err)
}
file, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
err = redirectTemplate.Execute(file, struct {
Domain, Package, GitHubUsername string
}{
domainName,
packageName,
githubUsername,
})
if err != nil {
log.Fatal(err)
}
}
func getRepositories(username string) []*github.Repository {
fmt.Printf("Getting the list of repositories for user %s from GitHub... ", username)
repos, _, err := github.NewClient(nil).Repositories.List(context.Background(), username, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found %v repositories.\n", len(repos))
return repos
}
func getRepositoryPaths(repo *github.Repository) []string {
tmpDir := path.Join(*outputDir, "tmp")
tmpRepoPath := path.Join(tmpDir, *repo.Name)
_, err := git.PlainClone(tmpRepoPath, false, &git.CloneOptions{
URL: *repo.CloneURL,
})
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpRepoPath)
return listDirs(tmpRepoPath, tmpDir)
}
// listDirs returns a list of paths with all sub-directories within a given directory.
func listDirs(curPath, tmpDir string) []string {
var dirs []string
err := filepath.Walk(curPath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if strings.Contains(p, ".git") {
return nil
}
resultPath := strings.TrimPrefix(p, tmpDir)
_, i := utf8.DecodeRuneInString(resultPath)
dirs = append(dirs, strings.TrimPrefix(resultPath, resultPath[i:]))
return nil
})
if err != nil {
log.Println(err)
}
return dirs
}