Commit a41a46b
Eric Bower
·
2023-06-25 14:51:17 -0400 EDT
parent 38c6478
refactor: change directory structure And other tweaks
3 files changed,
+101,
-57
+0,
-13
| ... | ... | @@ -1,13 +0,0 @@ | |
| 1 | - | FROM golang:1.20 as builder | |
| 2 | - | COPY . /app | |
| 3 | - | WORKDIR /app | |
| 4 | - | RUN go build -o pgit ./main.go | |
| 5 | - | ||
| 6 | - | RUN git clone https://github.com/picosh/pico.git | |
| 7 | - | RUN git clone https://github.com/picosh/ops.git | |
| 8 | - | ||
| 9 | - | RUN ./pgit | |
| 10 | - | ||
| 11 | - | FROM nginx:latest | |
| 12 | - | COPY --from=builder /app/public /usr/share/nginx/html | |
| 13 | - | EXPOSE 80 |
+7,
-2
| ... | ... | @@ -1,3 +1,8 @@ | |
| 1 | + | url: git.erock.io | |
| 1 | 2 | repos: | |
| 2 | - | - /app/pico | |
| 3 | - | - /app/ops | |
| 3 | + | - path: /home/erock/dev/pico/pico | |
| 4 | + | refs: | |
| 5 | + | - main | |
| 6 | + | - path: /home/erock/dev/pico/pico-ops | |
| 7 | + | refs: | |
| 8 | + | - main |
M
main.go
+94,
-42
| ... | ... | @@ -4,7 +4,7 @@ import ( | |
| 4 | 4 | "fmt" | |
| 5 | 5 | html "html/template" | |
| 6 | 6 | "os" | |
| 7 | - | "path" | |
| 7 | + | "path/filepath" | |
| 8 | 8 | "strings" | |
| 9 | 9 | ||
| 10 | 10 | git "github.com/gogs/git-module" |
| ... | ... | @@ -83,11 +83,16 @@ func CommitURL(repo string, commitID string) string { | |
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | 85 | func repoName(root string) string { | |
| 86 | - | _, file := path.Split(root) | |
| 86 | + | _, file := filepath.Split(root) | |
| 87 | 87 | return file | |
| 88 | 88 | } | |
| 89 | - | func findDefaultBranch(refs []*git.Reference) *git.Reference { | |
| 90 | - | for _, def := range defaultBranches { | |
| 89 | + | func findDefaultBranch(config *RepoConfig, refs []*git.Reference) *git.Reference { | |
| 90 | + | branches := config.Refs | |
| 91 | + | if len(branches) == 0 { | |
| 92 | + | branches = defaultBranches | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | for _, def := range branches { | |
| 91 | 96 | for _, ref := range refs { | |
| 92 | 97 | if "refs/heads/"+def == ref.Refspec { | |
| 93 | 98 | return ref |
| ... | ... | @@ -113,7 +118,7 @@ func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeIt | |
| 113 | 118 | aggregate = append(aggregate, &TreeItem{ | |
| 114 | 119 | Path: fname, | |
| 115 | 120 | Entry: entry, | |
| 116 | - | URL: path.Join("/tree", branch, fname), | |
| 121 | + | URL: filepath.Join("/", "tree", branch, "item", fname), | |
| 117 | 122 | }) | |
| 118 | 123 | } | |
| 119 | 124 | } |
| ... | ... | @@ -133,13 +138,13 @@ func writeHtml(data *WriteData) { | |
| 133 | 138 | bail(err) | |
| 134 | 139 | ||
| 135 | 140 | outdir := viper.GetString("outdir") | |
| 136 | - | dir := path.Join(outdir, data.RepoName, data.Subdir) | |
| 141 | + | dir := filepath.Join(outdir, data.RepoName, data.Subdir) | |
| 137 | 142 | fmt.Println(dir) | |
| 138 | 143 | fmt.Println(data.Name) | |
| 139 | 144 | err = os.MkdirAll(dir, os.ModePerm) | |
| 140 | 145 | bail(err) | |
| 141 | 146 | ||
| 142 | - | w, err := os.OpenFile(path.Join(dir, data.Name), os.O_WRONLY|os.O_CREATE, 0755) | |
| 147 | + | w, err := os.OpenFile(filepath.Join(dir, data.Name), os.O_WRONLY|os.O_CREATE, 0755) | |
| 143 | 148 | bail(err) | |
| 144 | 149 | ||
| 145 | 150 | err = ts.Execute(w, data) |
| ... | ... | @@ -158,20 +163,31 @@ func writeIndex(data *IndexPage) { | |
| 158 | 163 | bail(err) | |
| 159 | 164 | ||
| 160 | 165 | outdir := viper.GetString("outdir") | |
| 161 | - | dir := path.Join(outdir) | |
| 166 | + | dir := filepath.Join(outdir) | |
| 162 | 167 | fmt.Println(dir) | |
| 163 | 168 | err = os.MkdirAll(dir, os.ModePerm) | |
| 164 | 169 | bail(err) | |
| 165 | 170 | ||
| 166 | - | w, err := os.OpenFile(path.Join(dir, "index.html"), os.O_WRONLY|os.O_CREATE, 0755) | |
| 171 | + | w, err := os.OpenFile(filepath.Join(dir, "index.html"), os.O_WRONLY|os.O_CREATE, 0755) | |
| 167 | 172 | bail(err) | |
| 168 | 173 | ||
| 169 | 174 | err = ts.Execute(w, data) | |
| 170 | 175 | bail(err) | |
| 171 | 176 | } | |
| 177 | + | ||
| 178 | + | func writeRootSummary(data *PageData) { | |
| 179 | + | writeHtml(&WriteData{ | |
| 180 | + | Name: "index.html", | |
| 181 | + | Template: "./html/summary.page.tmpl", | |
| 182 | + | Data: data, | |
| 183 | + | RepoName: data.Repo.Name, | |
| 184 | + | Repo: data.Repo, | |
| 185 | + | }) | |
| 186 | + | } | |
| 172 | 187 | func writeSummary(data *PageData) { | |
| 173 | 188 | writeHtml(&WriteData{ | |
| 174 | 189 | Name: "index.html", | |
| 190 | + | Subdir: filepath.Join("tree", data.RevName), | |
| 175 | 191 | Template: "./html/summary.page.tmpl", | |
| 176 | 192 | Data: data, | |
| 177 | 193 | RepoName: data.Repo.Name, |
| ... | ... | @@ -180,7 +196,8 @@ func writeSummary(data *PageData) { | |
| 180 | 196 | } | |
| 181 | 197 | func writeTree(data *PageData) { | |
| 182 | 198 | writeHtml(&WriteData{ | |
| 183 | - | Name: "tree.html", | |
| 199 | + | Name: "index.html", | |
| 200 | + | Subdir: filepath.Join("tree", data.RevName), | |
| 184 | 201 | Template: "./html/tree.page.tmpl", | |
| 185 | 202 | Data: data, | |
| 186 | 203 | RepoName: data.Repo.Name, |
| ... | ... | @@ -189,7 +206,8 @@ func writeTree(data *PageData) { | |
| 189 | 206 | } | |
| 190 | 207 | func writeLog(data *PageData) { | |
| 191 | 208 | writeHtml(&WriteData{ | |
| 192 | - | Name: "log.html", | |
| 209 | + | Name: "index.html", | |
| 210 | + | Subdir: filepath.Join("logs", data.RevName), | |
| 193 | 211 | Template: "./html/log.page.tmpl", | |
| 194 | 212 | Data: data, | |
| 195 | 213 | RepoName: data.Repo.Name, |
| ... | ... | @@ -216,13 +234,13 @@ func writeHTMLTreeFiles(data *PageData) { | |
| 216 | 234 | bail(err) | |
| 217 | 235 | file.NumLines = len(strings.Split(string(b), "\n")) | |
| 218 | 236 | ||
| 219 | - | d := path.Dir(file.Path) | |
| 237 | + | d := filepath.Dir(file.Path) | |
| 220 | 238 | writeHtml(&WriteData{ | |
| 221 | 239 | Name: fmt.Sprintf("%s.html", file.Entry.Name()), | |
| 222 | 240 | Template: "./html/file.page.tmpl", | |
| 223 | 241 | Data: &FileData{Contents: string(b)}, | |
| 224 | 242 | RepoName: data.Repo.Name, | |
| 225 | - | Subdir: path.Join("tree", data.RevName, d), | |
| 243 | + | Subdir: filepath.Join("tree", data.RevName, "item", d), | |
| 226 | 244 | Repo: data.Repo, | |
| 227 | 245 | }) | |
| 228 | 246 | } |
| ... | ... | @@ -285,40 +303,62 @@ func writeLogDiffs(project string, repo *git.Repository, data *PageData, cache m | |
| 285 | 303 | } | |
| 286 | 304 | } | |
| 287 | 305 | ||
| 288 | - | func writeRepo(root string) { | |
| 289 | - | repo, err := git.Open(root) | |
| 306 | + | func writeRepo(config *RepoConfig) { | |
| 307 | + | repo, err := git.Open(config.Path) | |
| 290 | 308 | bail(err) | |
| 291 | 309 | ||
| 292 | - | name := repoName(root) | |
| 310 | + | name := repoName(config.Path) | |
| 311 | + | ||
| 312 | + | heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false}) | |
| 313 | + | bail(err) | |
| 314 | + | ||
| 315 | + | rev := findDefaultBranch(config, heads) | |
| 316 | + | if rev == nil { | |
| 317 | + | bail(fmt.Errorf("no default branch found")) | |
| 318 | + | } | |
| 319 | + | _, revName := filepath.Split(rev.Refspec) | |
| 320 | + | ||
| 293 | 321 | repoData := &RepoData{ | |
| 294 | 322 | Name: name, | |
| 295 | 323 | SummaryURL: fmt.Sprintf("/%s/index.html", name), | |
| 296 | - | TreeURL: fmt.Sprintf("/%s/tree.html", name), | |
| 297 | - | LogURL: fmt.Sprintf("/%s/log.html", name), | |
| 324 | + | TreeURL: fmt.Sprintf("/%s/tree/%s/index.html", name, revName), | |
| 325 | + | LogURL: fmt.Sprintf("/%s/logs/%s/index.html", name, revName), | |
| 298 | 326 | RefsURL: fmt.Sprintf("/%s/refs.html", name), | |
| 299 | 327 | CloneURL: fmt.Sprintf("/%s.git", name), | |
| 300 | 328 | } | |
| 301 | 329 | ||
| 302 | - | heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false}) | |
| 303 | - | bail(err) | |
| 304 | - | ||
| 305 | 330 | tags, _ := repo.ShowRef(git.ShowRefOptions{Heads: false, Tags: true}) | |
| 306 | 331 | ||
| 307 | 332 | cache := make(map[string]bool) | |
| 308 | - | rev := findDefaultBranch(heads) | |
| 309 | - | if rev != nil { | |
| 310 | - | // for _, rev := range heads { | |
| 311 | - | _, revName := path.Split(rev.Refspec) | |
| 312 | - | data := &PageData{ | |
| 313 | - | Branches: heads, | |
| 314 | - | Tags: tags, | |
| 315 | - | Rev: rev, | |
| 316 | - | RevName: revName, | |
| 317 | - | Repo: repoData, | |
| 318 | - | Readme: "", | |
| 319 | - | } | |
| 320 | 333 | ||
| 321 | - | writeBranch(repo, data, cache) | |
| 334 | + | data := &PageData{ | |
| 335 | + | Branches: heads, | |
| 336 | + | Tags: tags, | |
| 337 | + | Rev: rev, | |
| 338 | + | RevName: revName, | |
| 339 | + | Repo: repoData, | |
| 340 | + | Readme: "", | |
| 341 | + | } | |
| 342 | + | writeRootSummary(data) | |
| 343 | + | writeRefs(data) | |
| 344 | + | ||
| 345 | + | for _, revn := range config.Refs { | |
| 346 | + | for _, head := range heads { | |
| 347 | + | _, headName := filepath.Split(head.Refspec) | |
| 348 | + | if revn != headName { | |
| 349 | + | continue | |
| 350 | + | } | |
| 351 | + | data := &PageData{ | |
| 352 | + | Branches: heads, | |
| 353 | + | Tags: tags, | |
| 354 | + | Rev: head, | |
| 355 | + | RevName: headName, | |
| 356 | + | Repo: repoData, | |
| 357 | + | Readme: "", | |
| 358 | + | } | |
| 359 | + | ||
| 360 | + | writeBranch(repo, data, cache) | |
| 361 | + | } | |
| 322 | 362 | } | |
| 323 | 363 | } | |
| 324 | 364 |
| ... | ... | @@ -340,11 +380,12 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool | |
| 340 | 380 | treeEntries := walkTree(tree, pageData.RevName, "", entries) | |
| 341 | 381 | for _, entry := range treeEntries { | |
| 342 | 382 | entry.Path = strings.TrimPrefix(entry.Path, "/") | |
| 343 | - | entry.URL = path.Join( | |
| 383 | + | entry.URL = filepath.Join( | |
| 344 | 384 | "/", | |
| 345 | 385 | pageData.Repo.Name, | |
| 346 | 386 | "tree", | |
| 347 | 387 | pageData.RevName, | |
| 388 | + | "item", | |
| 348 | 389 | fmt.Sprintf("%s.html", entry.Path), | |
| 349 | 390 | ) | |
| 350 | 391 | } |
| ... | ... | @@ -352,9 +393,7 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool | |
| 352 | 393 | pageData.Log = logs | |
| 353 | 394 | pageData.Tree = treeEntries | |
| 354 | 395 | ||
| 355 | - | writeSummary(pageData) | |
| 356 | 396 | writeLog(pageData) | |
| 357 | - | writeRefs(pageData) | |
| 358 | 397 | writeHTMLTreeFiles(pageData) | |
| 359 | 398 | writeLogDiffs(pageData.Repo.Name, repo, pageData, cache) | |
| 360 | 399 |
| ... | ... | @@ -365,6 +404,15 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool | |
| 365 | 404 | } | |
| 366 | 405 | } | |
| 367 | 406 | ||
| 407 | + | type RepoConfig struct { | |
| 408 | + | Path string `mapstructure:"path"` | |
| 409 | + | Refs []string `mapstructure:"refs"` | |
| 410 | + | } | |
| 411 | + | type Config struct { | |
| 412 | + | Repos []*RepoConfig `mapstructure:"repos"` | |
| 413 | + | URL string `mapstructure:"url"` | |
| 414 | + | } | |
| 415 | + | ||
| 368 | 416 | func main() { | |
| 369 | 417 | viper.SetDefault("outdir", "./public") | |
| 370 | 418 | viper.SetConfigName("config") |
| ... | ... | @@ -373,11 +421,15 @@ func main() { | |
| 373 | 421 | err := viper.ReadInConfig() | |
| 374 | 422 | bail(err) | |
| 375 | 423 | ||
| 376 | - | repos := viper.GetStringSlice("repos") | |
| 424 | + | var config Config | |
| 425 | + | if err := viper.Unmarshal(&config); err != nil { | |
| 426 | + | fmt.Println(err) | |
| 427 | + | return | |
| 428 | + | } | |
| 377 | 429 | repoList := []*RepoItemData{} | |
| 378 | - | for _, r := range repos { | |
| 379 | - | name := repoName(r) | |
| 380 | - | url := path.Join("/", name, "index.html") | |
| 430 | + | for _, r := range config.Repos { | |
| 431 | + | name := repoName(r.Path) | |
| 432 | + | url := filepath.Join("/", name, "index.html") | |
| 381 | 433 | repoList = append(repoList, &RepoItemData{ | |
| 382 | 434 | URL: url, | |
| 383 | 435 | Name: name, |