commit
message
Better logging
author
Ben Vogt <[email protected]>
date
2023-04-05 15:09:33
stats
7 file(s) changed,
31 insertions(+),
26 deletions(-)
files
README.md
commit.go
file.go
files.go
index.go
log.go
main.go
1diff --git a/README.md b/README.md
2index 9ba8a9b..8b260b9 100644
3--- a/README.md
4+++ b/README.md
5@@ -1,10 +1,3 @@
6 # gshr
7
8 Git static host repo.
9-
10----
11-
12-Tasks to do:
13-
14-* Better styles for index page.
15-* Name for main index page.
16diff --git a/commit.go b/commit.go
17index 365d6e3..ef087ad 100644
18--- a/commit.go
19+++ b/commit.go
20@@ -10,9 +10,6 @@ import (
21 "github.com/go-git/go-git/v5/plumbing/object"
22 )
23
24-type FileDiff struct {
25-}
26-
27 type CommitPage struct {
28 RepoData RepoData
29 Author string
30@@ -28,6 +25,7 @@ type CommitPage struct {
31 }
32
33 func (c *CommitPage) Render(t *template.Template) {
34+ debug("commit %v %v", c.RepoData.Name, c.Hash)
35 err := os.MkdirAll(path.Join(args.OutputDir, c.RepoData.Name, "commit", c.Hash), 0755)
36 checkErr(err)
37 output, err := os.Create(path.Join(args.OutputDir, c.RepoData.Name, "commit", c.Hash, "index.html"))
38diff --git a/file.go b/file.go
39index 539e367..ab3a815 100644
40--- a/file.go
41+++ b/file.go
42@@ -23,6 +23,7 @@ type FilePage struct {
43 }
44
45 func (f *FilePage) Render(t *template.Template) {
46+ debug("file %v%v", f.RepoData.Name, f.Name)
47 err := os.MkdirAll(f.DestinationDir, 0775)
48 checkErr(err)
49 if f.CanRender {
50@@ -55,9 +56,9 @@ func RenderSingleFilePages(data RepoData) {
51 _, canRenderByFullName := settings.PlainFiles[filepath.Base(filename)]
52 partialPath, _ := strings.CutPrefix(filename, path.Join(args.CloneDir, data.Name))
53 outputName := path.Join(args.OutputDir, data.Name, "files", partialPath, "index.html")
54- debug("reading %v %v", data.Name, partialPath)
55 (&FilePage{
56 RepoData: data,
57+ Name: partialPath,
58 Extension: ext,
59 CanRender: canRenderExtension || canRenderByFullName,
60 Origin: filename,
61diff --git a/files.go b/files.go
62index a5373dc..d832748 100644
63--- a/files.go
64+++ b/files.go
65@@ -23,6 +23,7 @@ type FilesPage struct {
66 }
67
68 func (f *FilesPage) Render(t *template.Template) {
69+ debug("file page for '%v'", f.RepoData.Name)
70 output, err := os.Create(path.Join(args.OutputDir, f.RepoData.Name, "files.html"))
71 checkErr(err)
72 err = t.Execute(output, f)
73diff --git a/index.go b/index.go
74index e4a15fc..2dae5a2 100644
75--- a/index.go
76+++ b/index.go
77@@ -13,6 +13,7 @@ type IndexPage struct {
78 }
79
80 func (l *IndexPage) Render(t *template.Template) {
81+ debug("index for '%v'", l.SiteName)
82 output, err := os.Create(path.Join(args.OutputDir, "index.html"))
83 checkErr(err)
84 err = t.Execute(output, l)
85diff --git a/log.go b/log.go
86index 4c7f849..359b140 100644
87--- a/log.go
88+++ b/log.go
89@@ -27,6 +27,7 @@ type LogPage struct {
90 }
91
92 func (l *LogPage) Render(t *template.Template) {
93+ debug("log page for '%v'", l.RepoData.Name)
94 output, err := os.Create(path.Join(args.OutputDir, l.RepoData.Name, "log.html"))
95 checkErr(err)
96 err = t.Execute(output, l)
97diff --git a/main.go b/main.go
98index f51c2ab..c14bdda 100644
99--- a/main.go
100+++ b/main.go
101@@ -6,6 +6,7 @@ import (
102 _ "embed"
103 "flag"
104 "fmt"
105+ "log"
106 "math/rand"
107 "os"
108 "path"
109@@ -41,10 +42,12 @@ func main() {
110 }
111
112 func Init() {
113+ log.SetFlags(0)
114+ log.SetOutput(new(LogWriter))
115 args = DefaultCmdArgs()
116 settings = DefaultSettings()
117 flag.StringVar(&args.ConfigFile, "config", "", "Config file.")
118- flag.BoolVar(&args.DebugOn, "debug", true, "Run in debug mode.")
119+ flag.BoolVar(&args.Silent, "silent", false, "Run in silent mode.")
120 flag.StringVar(&args.OutputDir, "output", "", "Dir of output.")
121 flag.StringVar(&args.CloneDir, "clone", "", "Dir to clone into. Default is /tmp/${rand}")
122 flag.Parse()
123@@ -53,13 +56,15 @@ func Init() {
124 args.CloneDir = fmt.Sprintf("/tmp/gshr-temp-clone-%v", rand.Uint32())
125 }
126
127- debug("config = %v", args.ConfigFile)
128- debug("output = %v", args.OutputDir)
129- debug("clone = %v", args.CloneDir)
130- configFileByes, err := os.ReadFile(args.ConfigFile)
131+ debug("config %v", args.ConfigFile)
132+ debug("output %v", args.OutputDir)
133+ debug("clone %v", args.CloneDir)
134+ configFileBytes, err := os.ReadFile(args.ConfigFile)
135+ configString := string(configFileBytes)
136 checkErr(err)
137- config = ParseConfiguration(string(configFileByes))
138- debug("base_url = %v", config.BaseURL)
139+ config = ParseConfiguration(configString)
140+ debug("base_url %v", config.BaseURL)
141+ debug("site_name %v", config.SiteName)
142 }
143
144 func CloneAndGetData(repo Repo, r *git.Repository) RepoData {
145@@ -83,17 +88,22 @@ func CloneAndGetData(repo Repo, r *git.Repository) RepoData {
146 return data
147 }
148
149+type LogWriter struct{}
150+
151+func (writer LogWriter) Write(bytes []byte) (int, error) {
152+ return fmt.Print(string(bytes))
153+}
154+
155 func checkErr(err error) {
156 if err != nil {
157- fmt.Printf("ERROR: %v\n", err)
158+ log.Printf("ERROR: %v", err)
159 os.Exit(1)
160 }
161 }
162
163 func debug(format string, a ...any) {
164- if args.DebugOn {
165- fmt.Printf(format, a...)
166- fmt.Print("\n")
167+ if !args.Silent {
168+ log.Printf("DEBUG: "+format, a...)
169 }
170 }
171
172@@ -131,7 +141,7 @@ func findFileInRoot(name string, oneOfThese map[string]bool) string {
173 }
174
175 type CmdArgs struct {
176- DebugOn bool
177+ Silent bool
178 ConfigFile string
179 OutputDir string
180 CloneDir string
181@@ -139,7 +149,7 @@ type CmdArgs struct {
182
183 func DefaultCmdArgs() CmdArgs {
184 return CmdArgs{
185- DebugOn: true,
186+ Silent: true,
187 ConfigFile: "",
188 OutputDir: "",
189 CloneDir: "",