Skip to content

Commit 4ef7e29

Browse files
committed
ensure dirs exist, improve logging
1 parent 1590bd5 commit 4ef7e29

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

authenticator/authenticator.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
// Authenticator represents a quiki server or site authentication service.
1111
type Authenticator struct {
1212
Users map[string]User `json:"users,omitempty"`
13-
14-
path string // path to JSON file
15-
mu *sync.Mutex // data lock
13+
IsNew bool `json:"-"`
14+
path string // path to JSON file
15+
mu *sync.Mutex // data lock
1616
}
1717

1818
// Open reads a user data file and returns an Authenticator for it.
@@ -44,6 +44,7 @@ func Open(path string) (*Authenticator, error) {
4444
}
4545

4646
// create a new one
47+
auth.IsNew = true
4748
return auth, auth.write()
4849
}
4950

wiki/log.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,35 @@ import (
99
// Log logs info for a wiki.
1010
func (w *Wiki) Log(i ...any) {
1111
w.logger().Println(i...)
12+
i = w.addLogPrefix(i)
1213
log.Println(i...)
1314
}
1415

1516
// Debug logs debug info for a wiki.
1617
func (w *Wiki) Debug(i ...any) {
18+
i = w.addLogPrefix(i)
1719
log.Println(i...)
1820
}
1921

2022
// Logf logs info for a wiki.
2123
func (w *Wiki) Logf(format string, i ...any) {
2224
w.logger().Printf(format+"\n", i...)
23-
log.Printf(format+"\n", i...)
25+
log.Printf("["+w.Opt.Name+"] "+format+"\n", i...)
2426
}
2527

2628
// Debugf logs debug info for a wiki.
2729
func (w *Wiki) Debugf(format string, i ...any) {
28-
log.Printf(format+"\n", i...)
30+
log.Printf("["+w.Opt.Name+"] "+format+"\n", i...)
31+
}
32+
33+
func (w *Wiki) addLogPrefix(i []any) []any {
34+
return append([]any{"[" + w.Opt.Name + "]"}, i...)
2935
}
3036

3137
func (w *Wiki) logger() *log.Logger {
38+
if w._logger != nil {
39+
return w._logger
40+
}
3241
// consider: if wiki is ever destoryed, need to close this
3342
f, err := os.OpenFile(w.Dir("cache", "wiki.log"),
3443
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

wiki/wiki.go

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func NewWikiConfig(confPath string) (*Wiki, error) {
5959
if err != nil {
6060
return nil, errors.Wrap(err, "init authenticator")
6161
}
62+
if w.Auth.IsNew {
63+
w.Log("created wiki authentication file")
64+
}
6265

6366
// no errors occurred
6467
return w, nil

wikifier/page-opt.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wikifier
22

33
import (
4+
"os"
45
"path/filepath"
56
"strconv"
67
"strings"
@@ -219,13 +220,30 @@ func InjectPageOpt(page *Page, opt *PageOpt) error {
219220
}
220221
}
221222

222-
// all of these deprecated options are now overridden
223+
// these dirs are derived from wiki dir
223224
opt.Dir.Page = filepath.Join(opt.Dir.Wiki, "pages")
224225
opt.Dir.Image = filepath.Join(opt.Dir.Wiki, "images")
225226
opt.Dir.Model = filepath.Join(opt.Dir.Wiki, "models")
226227
opt.Dir.Cache = filepath.Join(opt.Dir.Wiki, "cache")
227228
opt.Dir.Category = filepath.Join(opt.Dir.Wiki, "cache", "category")
228229

230+
// ensure they all exist
231+
for _, dir := range []string{
232+
opt.Dir.Wiki,
233+
opt.Dir.Page,
234+
opt.Dir.Image,
235+
opt.Dir.Model,
236+
opt.Dir.Cache,
237+
opt.Dir.Category,
238+
} {
239+
if _, err := os.Lstat(dir); err != nil && os.IsNotExist(err) {
240+
err = os.MkdirAll(dir, 0755)
241+
if err != nil {
242+
return errors.Wrap(err, "creating "+dir)
243+
}
244+
}
245+
}
246+
229247
// convert all HTTP roots to /
230248
opt.Root.Wiki = startWithSlash(filepath.ToSlash(opt.Root.Wiki))
231249
opt.Root.Image = startWithSlash(filepath.ToSlash(opt.Root.Image))

0 commit comments

Comments
 (0)