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
Clean up of license/readme file lookup
author
Ben Vogt <[email protected]>
date
2023-04-04 21:38:32
stats
1 file(s) changed, 48 insertions(+), 40 deletions(-)
files
main.go
  1diff --git a/main.go b/main.go
  2index fd6bba8..cc4ed00 100644
  3--- a/main.go
  4+++ b/main.go
  5@@ -24,18 +24,20 @@ var htmlTemplates embed.FS
  6 var config Config
  7 
  8 type Config struct {
  9-	DebugOn        bool
 10-	Repo           string
 11-	OutputDir      string
 12-	CloneDir       string
 13-	BaseURL        string
 14-	RepoData       RepoData
 15-	TextExtensions map[string]bool
 16-	PlainFiles     map[string]bool
 17+	DebugOn             bool
 18+	Repo                string
 19+	OutputDir           string
 20+	CloneDir            string
 21+	BaseURL             string
 22+	RepoData            RepoData
 23+	AllowedLicenseFiles map[string]bool
 24+	AllowedReadMeFiles  map[string]bool
 25+	TextExtensions      map[string]bool
 26+	PlainFiles          map[string]bool
 27 }
 28 
 29 func DefaultConfig() Config {
 30-	return Config{
 31+	config := Config{
 32 		DebugOn:   true,
 33 		Repo:      "",
 34 		OutputDir: "",
 35@@ -89,13 +91,37 @@ func DefaultConfig() Config {
 36 			".yml":        true,
 37 		},
 38 		PlainFiles: map[string]bool{
 39-			"Dockerfile": true,
 40-			"LICENSE":    true,
 41-			"Makefile":   true,
 42-			"readme":     true,
 43+			"Dockerfile":  true,
 44+			"license-mit": true,
 45+			"LICENSE-MIT": true,
 46+			"license":     true,
 47+			"LICENSE":     true,
 48+			"Makefile":    true,
 49+			"readme":      true,
 50+			"Readme":      true,
 51+			"ReadMe":      true,
 52+			"README":      true,
 53+		},
 54+		AllowedLicenseFiles: map[string]bool{
 55+			"license-mit": true,
 56+			"LICENSE-MIT": true,
 57+			"license.md":  true,
 58+			"LICENSE.md":  true,
 59+			"license.txt": true,
 60+			"LICENSE.txt": true,
 61+			"LICENSE":     true,
 62+		},
 63+		AllowedReadMeFiles: map[string]bool{
 64+			"readme.md":  true,
 65+			"Readme.md":  true,
 66+			"ReadMe.md":  true,
 67+			"README.md":  true,
 68+			"readme.txt": true,
 69+			"README.txt": true,
 70 			"README":     true,
 71 		},
 72 	}
 73+	return config
 74 }
 75 
 76 type RepoData struct {
 77@@ -136,9 +162,9 @@ func main() {
 78 	debug("clone = %v", config.CloneDir)
 79 	debug("base-url = %v", config.BaseURL)
 80 	r := CloneAndInfo()
 81-	config.RepoData.ReadMePath = getReadmePath()
 82+	config.RepoData.ReadMePath = findFileInRoot(config.AllowedReadMeFiles)
 83 	config.RepoData.HasReadMe = config.RepoData.ReadMePath != ""
 84-	config.RepoData.LicenseFilePath = getLicenseFilePath()
 85+	config.RepoData.LicenseFilePath = findFileInRoot(config.AllowedLicenseFiles)
 86 	config.RepoData.HasLicenseFile = config.RepoData.LicenseFilePath != ""
 87 	RenderLogPage(r)
 88 	RenderAllCommitPages(r)
 89@@ -194,33 +220,13 @@ func highlight(pathOrExtension string, data *string) string {
 90 	return buf.String()
 91 }
 92 
 93-func getReadmePath() string {
 94-	for _, file := range []string{
 95-		"readme.md",
 96-		"README.md",
 97-		"readme.txt",
 98-		"README.txt",
 99-		"README",
100-	} {
101-		if stat, err := os.Stat(path.Join(config.CloneDir, file)); err == nil {
102-			return stat.Name()
103-		}
104-	}
105-	return ""
106-}
107-
108-func getLicenseFilePath() string {
109-	for _, file := range []string{
110-		"license-mit",
111-		"LICENSE-MIT",
112-		"license.md",
113-		"LICENSE.md",
114-		"license.txt",
115-		"LICENSE.txt",
116-		"LICENSE",
117-	} {
118-		if stat, err := os.Stat(path.Join(config.CloneDir, file)); err == nil {
119-			return stat.Name()
120+func findFileInRoot(oneOfThese map[string]bool) string {
121+	dir, err := os.ReadDir(config.CloneDir)
122+	checkErr(err)
123+	for _, e := range dir {
124+		name := e.Name()
125+		if _, ok := oneOfThese[name]; ok {
126+			return name
127 		}
128 	}
129 	return ""