gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← Commit log
commit
message
Flags, expanding line endings
author
Ben Vogt <[email protected]>
date
2023-04-03 20:56:16
stats
2 file(s) changed, 65 insertions(+), 31 deletions(-)
files
Makefile
main.go
  1diff --git a/Makefile b/Makefile
  2index 79de3ac..966f437 100644
  3--- a/Makefile
  4+++ b/Makefile
  5@@ -23,9 +23,9 @@ target/gshr-${OS}-${ARCH}-${ENVIRONMENT}.bin:
  6 	go build -o target/gshr-${OS}-${ARCH}-${ENVIRONMENT}.bin main.go
  7 
  8 dev: target output cloning target/gshr-${OS}-${ARCH}-${ENVIRONMENT}.bin
  9-	OUTPUT_DIR=$(PWD)/target/output \
 10-    CLONING_DIR=$(PWD)/target/cloning \
 11-    ./target/gshr-${OS}-${ARCH}-${ENVIRONMENT}.bin && \
 12+	./target/gshr-${OS}-${ARCH}-${ENVIRONMENT}.bin \
 13+    --output=$(PWD)/target/output \
 14+    --clone=$(PWD)/target/cloning && \
 15     cp styles.css $(PWD)/target/output/ && \
 16     cd $(PWD)/target/output && \
 17     python3 -m http.server 8000
 18\ No newline at end of file
 19diff --git a/main.go b/main.go
 20index 1b5cb2b..7115954 100644
 21--- a/main.go
 22+++ b/main.go
 23@@ -2,9 +2,11 @@ package main
 24 
 25 import (
 26 	"bytes"
 27+	"flag"
 28 	"fmt"
 29 	"html/template"
 30 	"io/fs"
 31+	"math/rand"
 32 	"os"
 33 	"path"
 34 	"path/filepath"
 35@@ -17,22 +19,62 @@ import (
 36 	"github.com/go-git/go-git/v5/plumbing/object"
 37 )
 38 
 39-var allowedExtensions = map[string]bool{
 40-	".md":   true,
 41-	".txt":  true,
 42-	".html": true,
 43-	".css":  true,
 44-	".js":   true,
 45-	".toml": true,
 46-	".json": true,
 47-	".lock": true,
 48-}
 49-
 50 var (
 51-	outputDir  = ""
 52-	cloningDir = ""
 53+	debugOn        = true
 54+	outputDir      = ""
 55+	cloningDir     = ""
 56+	textExtensions = map[string]bool{
 57+		".conf":       true,
 58+		".config":     true,
 59+		".css":        true,
 60+		".gitignore":  true,
 61+		".gitmodules": true,
 62+		".go":         true,
 63+		".htm":        true,
 64+		".html":       true,
 65+		".iml":        true,
 66+		".js":         true,
 67+		".json":       true,
 68+		".jsx":        true,
 69+		".less":       true,
 70+		".lock":       true,
 71+		".log":        true,
 72+		".Makefile":   true,
 73+		".md":         true,
 74+		".mod":        true,
 75+		".php":        true,
 76+		".py":         true,
 77+		".rb":         true,
 78+		".rs":         true,
 79+		".scss":       true,
 80+		".sum":        true,
 81+		".toml":       true,
 82+		".ts":         true,
 83+		".tsx":        true,
 84+		".txt":        true,
 85+		".xml":        true,
 86+		"Makefile":    true,
 87+	}
 88 )
 89 
 90+func main() {
 91+	flag.BoolVar(&debugOn, "debug", true, "debug mode")
 92+	flag.StringVar(&outputDir, "output", "", "clone directory")
 93+	flag.StringVar(&cloningDir, "clone", "", "clone directory")
 94+	flag.Parse()
 95+
 96+	if cloningDir == "" {
 97+		cloningDir = fmt.Sprintf("/tmp/gshr-temp-clone-%v", rand.Uint32())
 98+	}
 99+
100+	debug("output = %v", outputDir)
101+	debug("clone = %v", cloningDir)
102+	r := CloneAndInfo()
103+	BuildLogPage(r)
104+	BuildFilesPages()
105+	BuildSingleFilePages()
106+}
107+
108 type TrackedFileMetaData struct {
109 	Mode   string
110 	Name   string
111@@ -112,17 +154,6 @@ func (fi *FilesIndex) SaveTemplate(t *template.Template) {
112 	checkErr(err)
113 }
114 
115-func main() {
116-	outputDir = os.Getenv("OUTPUT_DIR")
117-	cloningDir = os.Getenv("CLONING_DIR")
118-	line("OUTPUT_DIR = %v", outputDir)
119-	line("CLONING_DIR = %v", cloningDir)
120-	r := CloneAndInfo()
121-	BuildLogPage(r)
122-	BuildFilesPages()
123-	BuildSingleFilePages()
124-}
125-
126 func CloneAndInfo() *git.Repository {
127 	r, err := git.PlainClone(cloningDir, false, &git.CloneOptions{
128 		URL: "/Users/bvogt/dev/src/ben/www",
129@@ -140,7 +171,6 @@ func BuildLogPage(r *git.Repository) {
130 	checkErr(err)
131 
132 	err = cIter.ForEach(func(c *object.Commit) error {
133-		fmt.Println(c)
134 		commits = append(commits, GshrCommit{
135 			Author:  c.Author.Email,
136 			Message: c.Message,
137@@ -167,7 +197,7 @@ func BuildFilesPages() {
138 
139 		if !info.IsDir() {
140 			ext := filepath.Ext(filename)
141-			if _, ok := allowedExtensions[ext]; ok {
142+			if _, ok := textExtensions[ext]; ok {
143 				info, err := os.Stat(filename)
144 				checkErr(err)
145 				Name, _ := strings.CutPrefix(filename, cloningDir)
146@@ -201,11 +231,10 @@ func BuildSingleFilePages() {
147 
148 		if !info.IsDir() {
149 			ext := filepath.Ext(filename)
150-			if _, ok := allowedExtensions[ext]; ok {
151+			if _, ok := textExtensions[ext]; ok {
152 				partialPath, _ := strings.CutPrefix(filename, cloningDir)
153 				outputName := path.Join(outputDir, "files", partialPath, "index.html")
154-				line("READING: %v", filename)
155-				line("WRITING: %v", outputName)
156+				debug("reading = %v", partialPath)
157 				tf := TrackedFile{
158 					Origin:         filename,
159 					Destination:    outputName,
160@@ -226,7 +255,9 @@ func checkErr(err error) {
161 	}
162 }
163 
164-func line(format string, a ...any) {
165-	fmt.Printf(format, a...)
166-	fmt.Print("\n")
167+func debug(format string, a ...any) {
168+	if debugOn {
169+		fmt.Printf(format, a...)
170+		fmt.Print("\n")
171+	}
172 }