repos / pgit

static site generator for git
git clone https://github.com/picosh/pgit.git

commit
d86407d
parent
6d8b897
author
Eric Bower
date
2023-08-10 14:28:41 +0000 UTC
dockerfile
4 files changed,  +66, -12
A .github/workflows/static.yml
+0, -0
A Dockerfile
+21, -0
 1@@ -0,0 +1,21 @@
 2+FROM golang:1.20 AS builder
 3+
 4+WORKDIR /app
 5+
 6+COPY go.mod go.sum ./
 7+RUN go mod download && go mod verify
 8+
 9+COPY . /app
10+
11+RUN go build -v -o pgit main.go
12+
13+FROM debian:12
14+WORKDIR /app
15+
16+RUN apt-get update && apt-get install -y git
17+# ignore git warning "detected dubious ownership in repository"
18+RUN git config --global safe.directory '*'
19+
20+COPY --from=builder /app/pgit /usr/bin
21+
22+CMD ["pgit"]
M Makefile
+4, -0
 1@@ -6,6 +6,10 @@ build:
 2 	go build -o pgit ./main.go
 3 .PHONY: build
 4 
 5+img:
 6+	docker build -t neurosnap/pgit:latest .
 7+.PHONY: img
 8+
 9 static: build clean
10 	cp -R ./static ./public
11 	./pgit
M main.go
+41, -12
  1@@ -7,6 +7,7 @@ import (
  2 	"flag"
  3 	"fmt"
  4 	"html/template"
  5+	"io/ioutil"
  6 	"math"
  7 	"os"
  8 	"path/filepath"
  9@@ -24,6 +25,12 @@ import (
 10 	"go.uber.org/zap"
 11 )
 12 
 13+//go:embed static/main.css
 14+var mainCss []byte
 15+
 16+//go:embed static/syntax.css
 17+var syntaxCss []byte
 18+
 19 //go:embed html/*.tmpl
 20 var efs embed.FS
 21 
 22@@ -317,13 +324,19 @@ func (c *Config) writeHtml(writeData *WriteData) {
 23 	fp := filepath.Join(dir, writeData.Filename)
 24 	c.Logger.Infof("writing (%s)", fp)
 25 
 26-	w, err := os.OpenFile(fp, os.O_WRONLY|os.O_CREATE, 0755)
 27+	w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
 28 	bail(err)
 29 
 30 	err = ts.Execute(w, writeData.Data)
 31 	bail(err)
 32 }
 33 
 34+func (c *Config) copyStatic(dst string, data []byte) {
 35+	c.Logger.Infof("writing (%s)", dst)
 36+    err := ioutil.WriteFile(dst, data, 0755)
 37+    bail(err)
 38+}
 39+
 40 func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
 41 	c.writeHtml(&WriteData{
 42 		Filename: "index.html",
 43@@ -742,6 +755,8 @@ func main() {
 44 	var rpath = flag.String("repo", ".", "path to git repo")
 45 	var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. main,v1,c69f86f,HEAD")
 46 	var themeFlag = flag.String("theme", "dracula", "theme to use for site")
 47+	var labelFlag = flag.String("label", "", "pretty name for the subdir where we create the repo, default is last folder in --repo")
 48+	var assetFlag = flag.Bool("assets", false, "copy static assets to --out")
 49 
 50 	flag.Parse()
 51 
 52@@ -750,15 +765,6 @@ func main() {
 53 	repoPath, err := filepath.Abs(*rpath)
 54 	bail(err)
 55 
 56-	revs := strings.Split(*revsFlag, ",")
 57-	if len(revs) == 1 && revs[0] == "" {
 58-		revs = []string{}
 59-	}
 60-
 61-	if len(revs) == 0 {
 62-		bail(fmt.Errorf("you must provide --revs"))
 63-	}
 64-
 65 	theme := styles.Get(*themeFlag)
 66 
 67 	lg, err := zap.NewProduction()
 68@@ -768,10 +774,20 @@ func main() {
 69 
 70 	logger := lg.Sugar()
 71 
 72+	label := repoName(repoPath)
 73+	if *labelFlag != "" {
 74+		label = *labelFlag
 75+	}
 76+
 77+	revs := strings.Split(*revsFlag, ",")
 78+	if len(revs) == 1 && revs[0] == "" {
 79+		revs = []string{}
 80+	}
 81+
 82 	config := &Config{
 83 		Outdir:   out,
 84 		RepoPath: repoPath,
 85-		RepoName: repoName(repoPath),
 86+		RepoName: label,
 87 		Cache:    make(map[string]bool),
 88 		Revs:     revs,
 89 		Theme:    theme,
 90@@ -779,7 +795,20 @@ func main() {
 91 	}
 92 	config.Logger.Infof("%+v", config)
 93 
 94+	writeAssets := *assetFlag
 95+	if writeAssets {
 96+		config.copyStatic(filepath.Join(config.Outdir, "main.css"), mainCss)
 97+		config.copyStatic(filepath.Join(config.Outdir, "syntax.css"), syntaxCss)
 98+		return
 99+	}
100+
101+	if len(revs) == 0 {
102+		bail(fmt.Errorf("you must provide --revs"))
103+	}
104+
105 	config.writeRepo()
106-	url := filepath.Join("/", config.RepoName, "index.html")
107+
108+	prefixPath := filepath.Join("/", config.RepoName)
109+	url := filepath.Join(prefixPath, "index.html")
110 	config.Logger.Info(url)
111 }