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
Using better clone dir, prepping for hosted repos
author
Ben Vogt <[email protected]>
date
2023-04-05 17:08:27
stats
6 file(s) changed, 44 insertions(+), 44 deletions(-)
files
Makefile
config.go
examples/ghsr-simple.toml
file.go
files.go
main.go
  1diff --git a/Makefile b/Makefile
  2index e041e91..085fefb 100644
  3--- a/Makefile
  4+++ b/Makefile
  5@@ -10,19 +10,16 @@ target:
  6 target/output: target
  7 	mkdir -p target/output
  8 
  9-target/cloning: target
 10-	mkdir -p target/cloning
 11-
 12 clean:
 13 	rm -rf target/*
 14 
 15 target/gshr.bin: Makefile target $(wildcard *.go)
 16 	go build -o target/gshr.bin $(wildcard *.go)
 17 
 18-build: Makefile target target/output target/cloning target/gshr.bin
 19+build: Makefile target target/output  target/gshr.bin
 20 	@# intentionally blank, proxy for prerequisite.
 21 
 22-dev: Makefile target target/output target/cloning target/gshr.bin
 23+dev: Makefile target target/output  target/gshr.bin
 24 	./target/gshr.bin \
 25     --config=$(PWD)/gshr.toml \
 26     --output=$(PWD)/target/output \
 27@@ -30,7 +27,7 @@ dev: Makefile target target/output target/cloning target/gshr.bin
 28     cd $(PWD)/target/output && \
 29     python3 -m http.server 8000
 30 
 31-dev-example-go-git: Makefile target target/output target/cloning target/gshr.bin
 32+dev-example-go-git: Makefile target target/output  target/gshr.bin
 33 	./target/gshr.bin \
 34     --config=$(PWD)/examples/go-git.toml \
 35     --output=$(PWD)/target/output \
 36@@ -38,7 +35,7 @@ dev-example-go-git: Makefile target target/output target/cloning target/gshr.bin
 37     cd $(PWD)/target/output && \
 38     python3 -m http.server 8000
 39 
 40-dev-example-gshr: Makefile target target/output target/cloning target/gshr.bin
 41+dev-example-gshr: Makefile target target/output  target/gshr.bin
 42 	./target/gshr.bin \
 43     --config=$(PWD)/examples/ghsr-simple.toml \
 44     --output=$(PWD)/target/output \
 45diff --git a/config.go b/config.go
 46index 9500b47..9804115 100644
 47--- a/config.go
 48+++ b/config.go
 49@@ -1,22 +1,44 @@
 50 package main
 51 
 52 import (
 53+	"os"
 54+	"path"
 55+
 56 	"github.com/BurntSushi/toml"
 57 )
 58 
 59 type Config struct {
 60-	SiteName string `toml:"site_name"`
 61-	Repos    []Repo
 62 	BaseURL  string `toml:"base_url"`
 63+	SiteName string `toml:"site_name"`
 64+	Repos    []Repo `toml:"repos"`
 65 }
 66 
 67 type Repo struct {
 68-	Name            string
 69-	Description     string
 70-	URL             string
 71+	Name            string `toml:"name"`
 72+	Description     string `toml:"description"`
 73+	URL             string `toml:"url"`
 74+	HostGit         bool   `toml:"host_git"`
 75 	PublishedGitURL string `toml:"published_git_url"`
 76 }
 77 
 78+// / CloneDir gets the directory that this repo was cloned into using the output directory
 79+// / from the program arguments, and this repo's name.
 80+func (r *Repo) CloneDir() string {
 81+	return path.Join(args.OutputDir, "git", r.Name)
 82+}
 83+
 84+func (r *Repo) FindFileInRoot(oneOfThese map[string]bool) string {
 85+	dir, err := os.ReadDir(r.CloneDir())
 86+	checkErr(err)
 87+	for _, e := range dir {
 88+		name := e.Name()
 89+		if _, ok := oneOfThese[name]; ok {
 90+			return name
 91+		}
 92+	}
 93+	return ""
 94+}
 95+
 96 func ParseConfiguration(data string) Config {
 97 	conf := Config{}
 98 	_, err := toml.Decode(data, &conf)
 99@@ -25,9 +47,8 @@ func ParseConfiguration(data string) Config {
100 }
101 
102 type RepoData struct {
103-	Name            string
104+	Repo
105 	PublishedGitURL string
106-	Description     string
107 	BaseURL         string
108 	ReadMePath      string
109 	LicenseFilePath string
110diff --git a/examples/ghsr-simple.toml b/examples/ghsr-simple.toml
111index f307799..af46f49 100644
112--- a/examples/ghsr-simple.toml
113+++ b/examples/ghsr-simple.toml
114@@ -6,3 +6,4 @@ name = "gshr"
115 description = "git static host repo -- generates static html for repos"
116 url = "https://github.com/vogtb/gshr"
117 published_git_url = "https://github.com/vogtb/gshr"
118+host_git = true
119diff --git a/file.go b/file.go
120index bde7265..33676da 100644
121--- a/file.go
122+++ b/file.go
123@@ -45,7 +45,7 @@ func (f *FilePage) RenderPage(t *template.Template) {
124 func RenderSingleFilePages(data RepoData) {
125 	t, err := template.ParseFS(htmlTemplates, "templates/file.html", "templates/partials.html")
126 	checkErr(err)
127-	err = filepath.Walk(path.Join(args.CloneDir, data.Name), func(filename string, info fs.FileInfo, err error) error {
128+	err = filepath.Walk(data.CloneDir(), func(filename string, info fs.FileInfo, err error) error {
129 		if info.IsDir() && info.Name() == ".git" {
130 			return filepath.SkipDir
131 		}
132@@ -54,7 +54,7 @@ func RenderSingleFilePages(data RepoData) {
133 			ext := filepath.Ext(filename)
134 			_, canRenderExtension := settings.TextExtensions[ext]
135 			_, canRenderByFullName := settings.PlainFiles[filepath.Base(filename)]
136-			partialPath, _ := strings.CutPrefix(filename, path.Join(args.CloneDir, data.Name))
137+			partialPath, _ := strings.CutPrefix(filename, data.CloneDir())
138 			outputName := path.Join(args.OutputDir, data.Name, "files", partialPath, "index.html")
139 			(&FilePage{
140 				RepoData:       data,
141diff --git a/files.go b/files.go
142index b63208b..28c2319 100644
143--- a/files.go
144+++ b/files.go
145@@ -34,7 +34,7 @@ func RenderAllFilesPage(data RepoData) {
146 	t, err := template.ParseFS(htmlTemplates, "templates/files.html", "templates/partials.html")
147 	checkErr(err)
148 	files := make([]FileOverview, 0)
149-	err = filepath.Walk(path.Join(args.CloneDir, data.Name), func(filename string, info fs.FileInfo, err error) error {
150+	err = filepath.Walk(data.CloneDir(), func(filename string, info fs.FileInfo, err error) error {
151 		if info.IsDir() && info.Name() == ".git" {
152 			return filepath.SkipDir
153 		}
154@@ -42,7 +42,7 @@ func RenderAllFilesPage(data RepoData) {
155 		if !info.IsDir() {
156 			info, err := os.Stat(filename)
157 			checkErr(err)
158-			Name, _ := strings.CutPrefix(filename, path.Join(args.CloneDir, data.Name))
159+			Name, _ := strings.CutPrefix(filename, data.CloneDir())
160 			Name, _ = strings.CutPrefix(Name, "/")
161 			tf := FileOverview{
162 				Origin: filename,
163diff --git a/main.go b/main.go
164index c49f127..f776345 100644
165--- a/main.go
166+++ b/main.go
167@@ -7,7 +7,6 @@ import (
168 	"flag"
169 	"fmt"
170 	"log"
171-	"math/rand"
172 	"os"
173 	"path"
174 
175@@ -55,17 +54,11 @@ func Init() {
176 	settings = DefaultSettings()
177 	flag.StringVar(&args.ConfigFile, "config", "", "Config file.")
178 	flag.StringVar(&args.OutputDir, "output", "", "Dir of output.")
179-	flag.StringVar(&args.CloneDir, "clone", "", "Dir to clone into. Default is /tmp/${rand}")
180 	flag.BoolVar(&args.Silent, "silent", false, "Run in silent mode.")
181 	flag.Parse()
182 
183-	if args.CloneDir == "" {
184-		args.CloneDir = fmt.Sprintf("/tmp/gshr-temp-clone-%v", rand.Uint32())
185-	}
186-
187 	debug("config '%v'", args.ConfigFile)
188 	debug("output '%v'", args.OutputDir)
189-	debug("clone '%v'", args.CloneDir)
190 	configFileBytes, err := os.ReadFile(args.ConfigFile)
191 	configString := string(configFileBytes)
192 	checkErr(err)
193@@ -75,22 +68,21 @@ func Init() {
194 }
195 
196 func CloneAndGetData(repo Repo, r *git.Repository) RepoData {
197-	err := os.MkdirAll(path.Join(args.CloneDir, repo.Name), 0755)
198+	err := os.MkdirAll(repo.CloneDir(), 0755)
199 	checkErr(err)
200 	err = os.MkdirAll(path.Join(args.OutputDir, repo.Name), 0755)
201 	checkErr(err)
202 	debug("cloning '%v'", repo.Name)
203-	repoRef, err := git.PlainClone(path.Join(args.CloneDir, repo.Name), false, &git.CloneOptions{
204+	repoRef, err := git.PlainClone(repo.CloneDir(), false, &git.CloneOptions{
205 		URL: repo.URL,
206 	})
207 	checkErr(err)
208 	data := RepoData{
209-		Name:            repo.Name,
210+		Repo:            repo,
211 		PublishedGitURL: repo.PublishedGitURL,
212-		Description:     repo.Description,
213 		BaseURL:         config.BaseURL,
214-		ReadMePath:      findFileInRoot(repo.Name, settings.AllowedReadMeFiles),
215-		LicenseFilePath: findFileInRoot(repo.Name, settings.AllowedLicenseFiles),
216+		ReadMePath:      repo.FindFileInRoot(settings.AllowedReadMeFiles),
217+		LicenseFilePath: repo.FindFileInRoot(settings.AllowedLicenseFiles),
218 	}
219 	*r = *repoRef
220 	return data
221@@ -143,23 +135,10 @@ func highlight(pathOrExtension string, data *string) string {
222 	return buf.String()
223 }
224 
225-func findFileInRoot(name string, oneOfThese map[string]bool) string {
226-	dir, err := os.ReadDir(path.Join(args.CloneDir, name))
227-	checkErr(err)
228-	for _, e := range dir {
229-		name := e.Name()
230-		if _, ok := oneOfThese[name]; ok {
231-			return name
232-		}
233-	}
234-	return ""
235-}
236-
237 type CmdArgs struct {
238 	Silent     bool
239 	ConfigFile string
240 	OutputDir  string
241-	CloneDir   string
242 }
243 
244 func DefaultCmdArgs() CmdArgs {
245@@ -167,7 +146,6 @@ func DefaultCmdArgs() CmdArgs {
246 		Silent:     true,
247 		ConfigFile: "",
248 		OutputDir:  "",
249-		CloneDir:   "",
250 	}
251 }
252