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
Cleaning up by using config
author
Ben Vogt <[email protected]>
date
2023-04-03 21:11:12
stats
2 file(s) changed, 81 insertions(+), 66 deletions(-)
files
README.md
main.go
  1diff --git a/README.md b/README.md
  2index 33b39e3..8b260b9 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -1,9 +1,3 @@
  6 # gshr
  7 
  8 Git static host repo.
  9-
 10----
 11-
 12-Some useful links:
 13-
 14-* Syntax highlighting: https://github.com/alecthomas/chroma
 15\ No newline at end of file
 16diff --git a/main.go b/main.go
 17index a90d47a..7ee0128 100644
 18--- a/main.go
 19+++ b/main.go
 20@@ -21,61 +21,79 @@ import (
 21 )
 22 
 23 var (
 24-	debugOn        = true
 25-	repo           = ""
 26-	outputDir      = ""
 27-	cloningDir     = ""
 28-	textExtensions = map[string]bool{
 29-		".conf":       true,
 30-		".config":     true,
 31-		".css":        true,
 32-		".gitignore":  true,
 33-		".gitmodules": true,
 34-		".go":         true,
 35-		".htm":        true,
 36-		".html":       true,
 37-		".iml":        true,
 38-		".js":         true,
 39-		".json":       true,
 40-		".jsx":        true,
 41-		".less":       true,
 42-		".lock":       true,
 43-		".log":        true,
 44-		".Makefile":   true,
 45-		".md":         true,
 46-		".mod":        true,
 47-		".php":        true,
 48-		".py":         true,
 49-		".rb":         true,
 50-		".rs":         true,
 51-		".scss":       true,
 52-		".sum":        true,
 53-		".toml":       true,
 54-		".ts":         true,
 55-		".tsx":        true,
 56-		".txt":        true,
 57-		".xml":        true,
 58-		"Makefile":    true,
 59-	}
 60+	config Config
 61 )
 62 
 63+type Config struct {
 64+	DebugOn        bool
 65+	Repo           string
 66+	OutputDir      string
 67+	CloneDir       string
 68+	TextExtensions map[string]bool
 69+}
 70+
 71+func DefaultConfig() Config {
 72+	return Config{
 73+		DebugOn:   true,
 74+		Repo:      "",
 75+		OutputDir: "",
 76+		CloneDir:  fmt.Sprintf("/tmp/gshr-temp-clone-%v", rand.Uint32()),
 77+		TextExtensions: map[string]bool{
 78+			".c":          true,
 79+			".cc":         true,
 80+			".conf":       true,
 81+			".config":     true,
 82+			".cpp":        true,
 83+			".css":        true,
 84+			".gitignore":  true,
 85+			".gitmodules": true,
 86+			".go":         true,
 87+			".h":          true,
 88+			".htm":        true,
 89+			".html":       true,
 90+			".iml":        true,
 91+			".js":         true,
 92+			".json":       true,
 93+			".jsx":        true,
 94+			".less":       true,
 95+			".lock":       true,
 96+			".log":        true,
 97+			".Makefile":   true,
 98+			".md":         true,
 99+			".mod":        true,
100+			".php":        true,
101+			".py":         true,
102+			".rb":         true,
103+			".rs":         true,
104+			".scss":       true,
105+			".sql":        true,
106+			".sum":        true,
107+			".toml":       true,
108+			".ts":         true,
109+			".tsx":        true,
110+			".txt":        true,
111+			".xml":        true,
112+			".yaml":       true,
113+			".yml":        true,
114+			"Makefile":    true,
115+		},
116+	}
117+}
118+
119 func main() {
120-	flag.StringVar(&repo, "repo", "", "Repo to use.")
121-	flag.BoolVar(&debugOn, "debug", true, "Run in debug mode.")
122-	flag.StringVar(&outputDir, "output", "", "Directory of output.")
123-	flag.StringVar(&cloningDir, "clone", "", "Directory to clone into. Random directory in /tmp if omitted.")
124+	config = DefaultConfig()
125+	flag.StringVar(&config.Repo, "repo", "", "Repo to use.")
126+	flag.BoolVar(&config.DebugOn, "debug", true, "Run in debug mode.")
127+	flag.StringVar(&config.OutputDir, "output", "", "Directory of output.")
128+	flag.StringVar(&config.CloneDir, "clone", "", "Directory to clone into. Random directory in /tmp if omitted.")
129 	flag.Parse()
130 
131-	if repo == "" {
132+	if config.Repo == "" {
133 		checkErr(errors.New("--repo flag is required"))
134 	}
135 
136-	if cloningDir == "" {
137-		cloningDir = fmt.Sprintf("/tmp/gshr-temp-clone-%v", rand.Uint32())
138-	}
139-
140-	debug("output = %v", outputDir)
141-	debug("clone = %v", cloningDir)
142+	debug("output = %v", config.OutputDir)
143+	debug("clone = %v", config.CloneDir)
144 	r := CloneAndInfo()
145 	BuildLogPage(r)
146 	BuildFilesPages()
147@@ -144,7 +162,7 @@ type LogPage struct {
148 }
149 
150 func (mi *LogPage) SaveTemplate(t *template.Template) {
151-	output, err := os.Create(path.Join(outputDir, "log.html"))
152+	output, err := os.Create(path.Join(config.OutputDir, "log.html"))
153 	checkErr(err)
154 	err = t.Execute(output, mi)
155 	checkErr(err)
156@@ -155,15 +173,15 @@ type FilesIndex struct {
157 }
158 
159 func (fi *FilesIndex) SaveTemplate(t *template.Template) {
160-	output, err := os.Create(path.Join(outputDir, "files.html"))
161+	output, err := os.Create(path.Join(config.OutputDir, "files.html"))
162 	checkErr(err)
163 	err = t.Execute(output, fi)
164 	checkErr(err)
165 }
166 
167 func CloneAndInfo() *git.Repository {
168-	r, err := git.PlainClone(cloningDir, false, &git.CloneOptions{
169-		URL: repo,
170+	r, err := git.PlainClone(config.CloneDir, false, &git.CloneOptions{
171+		URL: config.Repo,
172 	})
173 	checkErr(err)
174 	return r
175@@ -197,17 +215,17 @@ func BuildLogPage(r *git.Repository) {
176 func BuildFilesPages() {
177 	t, err := template.ParseFiles("files.template.html")
178 	trackedFiles := make([]TrackedFileMetaData, 0)
179-	err = filepath.Walk(cloningDir, func(filename string, info fs.FileInfo, err error) error {
180+	err = filepath.Walk(config.CloneDir, func(filename string, info fs.FileInfo, err error) error {
181 		if info.IsDir() && info.Name() == ".git" {
182 			return filepath.SkipDir
183 		}
184 
185 		if !info.IsDir() {
186 			ext := filepath.Ext(filename)
187-			if _, ok := textExtensions[ext]; ok {
188+			if _, ok := config.TextExtensions[ext]; ok {
189 				info, err := os.Stat(filename)
190 				checkErr(err)
191-				Name, _ := strings.CutPrefix(filename, cloningDir)
192+				Name, _ := strings.CutPrefix(filename, config.CloneDir)
193 				Name, _ = strings.CutPrefix(Name, "/")
194 				tf := TrackedFileMetaData{
195 					Origin: filename,
196@@ -231,21 +249,21 @@ func BuildSingleFilePages() {
197 	t, err := template.ParseFiles("file.template.html")
198 	checkErr(err)
199 
200-	err = filepath.Walk(cloningDir, func(filename string, info fs.FileInfo, err error) error {
201+	err = filepath.Walk(config.CloneDir, func(filename string, info fs.FileInfo, err error) error {
202 		if info.IsDir() && info.Name() == ".git" {
203 			return filepath.SkipDir
204 		}
205 
206 		if !info.IsDir() {
207 			ext := filepath.Ext(filename)
208-			if _, ok := textExtensions[ext]; ok {
209-				partialPath, _ := strings.CutPrefix(filename, cloningDir)
210-				outputName := path.Join(outputDir, "files", partialPath, "index.html")
211+			if _, ok := config.TextExtensions[ext]; ok {
212+				partialPath, _ := strings.CutPrefix(filename, config.CloneDir)
213+				outputName := path.Join(config.OutputDir, "files", partialPath, "index.html")
214 				debug("reading = %v", partialPath)
215 				tf := TrackedFile{
216 					Origin:         filename,
217 					Destination:    outputName,
218-					DestinationDir: path.Join(outputDir, "files", partialPath),
219+					DestinationDir: path.Join(config.OutputDir, "files", partialPath),
220 				}
221 				tf.SaveTemplate(t)
222 			}
223@@ -263,7 +281,7 @@ func checkErr(err error) {
224 }
225 
226 func debug(format string, a ...any) {
227-	if debugOn {
228+	if config.DebugOn {
229 		fmt.Printf(format, a...)
230 		fmt.Print("\n")
231 	}