diff options
| author | Shulhan <ms@kilabit.info> | 2022-08-01 20:31:25 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-08-01 20:39:38 +0700 |
| commit | f0be5f35c325da2aebea6e0d98f035ad23dc3019 (patch) | |
| tree | 270513ab0d9872a9b30b799ad084a181378fbad1 | |
| parent | 0a9e6c793dc1d91d71db6838e2c45adecc6aae92 (diff) | |
| download | ciigo-f0be5f35c325da2aebea6e0d98f035ad23dc3019.tar.xz | |
all: clean up codes
Replace any usage of ":=" with explicit variable declaration for better
types clarity.
| -rw-r--r-- | ciigo.go | 37 | ||||
| -rw-r--r-- | ciigo_test.go | 31 | ||||
| -rw-r--r-- | cmd/ciigo-example/main.go | 22 | ||||
| -rw-r--r-- | cmd/ciigo/main.go | 26 | ||||
| -rw-r--r-- | embedded_css.go | 2 | ||||
| -rw-r--r-- | internal/cmd/goembed/main.go | 34 | ||||
| -rw-r--r-- | server.go | 36 | ||||
| -rw-r--r-- | watcher.go | 6 | ||||
| -rw-r--r-- | watcher_test.go | 24 |
9 files changed, 134 insertions, 84 deletions
@@ -85,6 +85,7 @@ func GoEmbed(opts *EmbedOptions) (err error) { converter *Converter fileMarkups map[string]*fileMarkup mfs *memfs.MemFS + mfsOpts *memfs.Options convertForce bool ) @@ -112,13 +113,13 @@ func GoEmbed(opts *EmbedOptions) (err error) { converter.convertFileMarkups(fileMarkups, convertForce) - memfsOpts := &memfs.Options{ + mfsOpts = &memfs.Options{ Root: opts.Root, Excludes: defExcludes, Embed: opts.EmbedOptions, } - mfs, err = memfs.New(memfsOpts) + mfs, err = memfs.New(mfsOpts) if err != nil { return fmt.Errorf("%s: %w", logp, err) } @@ -254,8 +255,16 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) ( ) { var ( logp = "listFileMarkups" - d *os.File - fis []os.FileInfo + + d *os.File + fi os.FileInfo + fmarkup *fileMarkup + name string + filePath string + k string + ext string + fis []os.FileInfo + fmarkups map[string]*fileMarkup ) d, err = os.Open(dir) @@ -270,9 +279,9 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) ( fileMarkups = make(map[string]*fileMarkup) - for _, fi := range fis { - name := fi.Name() - filePath := filepath.Join(dir, name) + for _, fi = range fis { + name = fi.Name() + filePath = filepath.Join(dir, name) if isExcluded(filePath, excRE) { continue @@ -283,24 +292,24 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) ( // Skip any directory start with '.'. continue } - fmarkups, err := listFileMarkups(filePath, excRE) + fmarkups, err = listFileMarkups(filePath, excRE) if err != nil { return nil, fmt.Errorf("%s: %s: %w", logp, filePath, err) } - for k, v := range fmarkups { - fileMarkups[k] = v + for k, fmarkup = range fmarkups { + fileMarkups[k] = fmarkup } continue } - ext := strings.ToLower(filepath.Ext(name)) + ext = strings.ToLower(filepath.Ext(name)) if !isExtensionMarkup(ext) { continue } if fi.Size() == 0 { continue } - fmarkup, err := newFileMarkup(filePath, fi) + fmarkup, err = newFileMarkup(filePath, fi) if err != nil { return nil, fmt.Errorf("%s: %s: %w", logp, filePath, err) } @@ -314,7 +323,9 @@ func isExcluded(path string, excs []*regexp.Regexp) bool { if len(excs) == 0 { return false } - for _, re := range excs { + + var re *regexp.Regexp + for _, re = range excs { if re.MatchString(path) { return true } diff --git a/ciigo_test.go b/ciigo_test.go index 6754135..f2bee3d 100644 --- a/ciigo_test.go +++ b/ciigo_test.go @@ -12,14 +12,12 @@ import ( ) func TestListFileMarkups(t *testing.T) { - var ( - dir = "testdata" - ) - - cases := []struct { + type testCase struct { excRegex string exp []string - }{{ + } + + var cases = []testCase{{ excRegex: `(ex)/.*`, exp: []string{ "testdata/in/clu/de/file.adoc", @@ -36,16 +34,27 @@ func TestListFileMarkups(t *testing.T) { }, }} - for _, c := range cases { - excre := regexp.MustCompile(c.excRegex) + var ( + dir = "testdata" + + c testCase + excre *regexp.Regexp + list map[string]*fileMarkup + got []string + k string + err error + ) + + for _, c = range cases { + excre = regexp.MustCompile(c.excRegex) - list, err := listFileMarkups(dir, []*regexp.Regexp{excre}) + list, err = listFileMarkups(dir, []*regexp.Regexp{excre}) if err != nil { t.Fatal(err) } - got := make([]string, 0, len(list)) - for k := range list { + got = make([]string, 0, len(list)) + for k = range list { got = append(got, k) } diff --git a/cmd/ciigo-example/main.go b/cmd/ciigo-example/main.go index 9329dfe..0eeb52c 100644 --- a/cmd/ciigo-example/main.go +++ b/cmd/ciigo-example/main.go @@ -16,16 +16,20 @@ import ( var ciigoFS *memfs.MemFS func main() { - opts := &ciigo.ServeOptions{ - ConvertOptions: ciigo.ConvertOptions{ - Root: "_example", - HtmlTemplate: "_example/html.tmpl", - }, - Mfs: ciigoFS, - Address: ":8080", - } + var ( + opts = &ciigo.ServeOptions{ + ConvertOptions: ciigo.ConvertOptions{ + Root: "_example", + HtmlTemplate: "_example/html.tmpl", + }, + Mfs: ciigoFS, + Address: ":8080", + } + + err error + ) - err := ciigo.Serve(opts) + err = ciigo.Serve(opts) if err != nil { log.Fatal(err) } diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go index ae15f59..fd97e99 100644 --- a/cmd/ciigo/main.go +++ b/cmd/ciigo/main.go @@ -36,7 +36,6 @@ import ( "strings" "git.sr.ht/~shulhan/ciigo" - "github.com/shuLhan/share/lib/memfs" ) const ( @@ -77,7 +76,9 @@ func main() { Exclude: *exclude, } - err error + embedOpts ciigo.EmbedOptions + serveOpts ciigo.ServeOptions + err error ) if len(command) == 0 { @@ -93,24 +94,19 @@ func main() { err = ciigo.Convert(&convertOpts) case cmdEmbed: - genOpts := ciigo.EmbedOptions{ - ConvertOptions: convertOpts, - EmbedOptions: memfs.EmbedOptions{ - GoFileName: *outputFile, - }, - } - err = ciigo.GoEmbed(&genOpts) + embedOpts.ConvertOptions = convertOpts + embedOpts.EmbedOptions.GoFileName = *outputFile + + err = ciigo.GoEmbed(&embedOpts) case cmdHelp: usage() case cmdServe: - opts := ciigo.ServeOptions{ - ConvertOptions: convertOpts, - Address: *address, - IsDevelopment: true, - } - err = ciigo.Serve(&opts) + serveOpts.ConvertOptions = convertOpts + serveOpts.Address = *address + serveOpts.IsDevelopment = true + err = ciigo.Serve(&serveOpts) case cmdVersion: fmt.Println(version) diff --git a/embedded_css.go b/embedded_css.go index f4a675d..3ffddf9 100644 --- a/embedded_css.go +++ b/embedded_css.go @@ -12,7 +12,7 @@ func embeddedCSS() *template.CSS { return _embeddedCSS } - css := template.CSS(` + var css template.CSS = template.CSS(` body { margin: 0; font-family: Arial, sans-serif; diff --git a/internal/cmd/goembed/main.go b/internal/cmd/goembed/main.go index 3b0b4a2..7d35377 100644 --- a/internal/cmd/goembed/main.go +++ b/internal/cmd/goembed/main.go @@ -9,26 +9,32 @@ package main import ( "log" - "git.sr.ht/~shulhan/ciigo" "github.com/shuLhan/share/lib/memfs" + + "git.sr.ht/~shulhan/ciigo" ) func main() { - opts := ciigo.EmbedOptions{ - ConvertOptions: ciigo.ConvertOptions{ - Root: "_example", - HtmlTemplate: "_example/html.tmpl", - }, - EmbedOptions: memfs.EmbedOptions{ - CommentHeader: `// SPDX-FileCopyrightText: 2019 Shulhan <ms@kilabit.info> + var ( + opts = ciigo.EmbedOptions{ + ConvertOptions: ciigo.ConvertOptions{ + Root: "_example", + HtmlTemplate: "_example/html.tmpl", + }, + EmbedOptions: memfs.EmbedOptions{ + CommentHeader: `// SPDX-FileCopyrightText: 2019 Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-or-later `, - PackageName: "main", - VarName: "ciigoFS", - GoFileName: "cmd/ciigo-example/static.go", - }, - } - err := ciigo.GoEmbed(&opts) + PackageName: "main", + VarName: "ciigoFS", + GoFileName: "cmd/ciigo-example/static.go", + }, + } + + err error + ) + + err = ciigo.GoEmbed(&opts) if err != nil { log.Fatal(err) } @@ -30,7 +30,9 @@ func newServer(opts *ServeOptions) (srv *server, err error) { var ( logp = "newServer" - tmplNode *memfs.Node + tmplNode *memfs.Node + httpdOpts *libhttp.ServerOptions + epInSearch *libhttp.Endpoint ) if opts.Mfs == nil { @@ -49,7 +51,7 @@ func newServer(opts *ServeOptions) (srv *server, err error) { opts: *opts, } - httpdOpts := &libhttp.ServerOptions{ + httpdOpts = &libhttp.ServerOptions{ Memfs: opts.Mfs, Address: opts.Address, } @@ -59,7 +61,7 @@ func newServer(opts *ServeOptions) (srv *server, err error) { return nil, fmt.Errorf("%s: %w", logp, err) } - epInSearch := &libhttp.Endpoint{ + epInSearch = &libhttp.Endpoint{ Method: libhttp.RequestMethodGet, Path: "/_internal/search", RequestType: libhttp.RequestTypeQuery, @@ -100,10 +102,12 @@ func newServer(opts *ServeOptions) (srv *server, err error) { // start the web server. func (srv *server) start() (err error) { - logp := "start" + var ( + logp = "start" + ) if srv.opts.IsDevelopment { - err := srv.watcher.start() + err = srv.watcher.start() if err != nil { return fmt.Errorf("%s: %w", logp, err) } @@ -121,21 +125,29 @@ func (srv *server) start() (err error) { } func (srv *server) onSearch(epr *libhttp.EndpointRequest) (resBody []byte, err error) { - var bufSearch, buf bytes.Buffer - logp := "onSearch" + var ( + logp = "onSearch" - q := epr.HttpRequest.Form.Get("q") - results := srv.http.Options.Memfs.Search(strings.Fields(q), 0) + fhtml *fileHtml + buf bytes.Buffer + q string + results []memfs.SearchResult + ) + + q = epr.HttpRequest.Form.Get("q") + results = srv.http.Options.Memfs.Search(strings.Fields(q), 0) - err = srv.converter.tmplSearch.Execute(&bufSearch, results) + err = srv.converter.tmplSearch.Execute(&buf, results) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } - fhtml := &fileHtml{ - Body: template.HTML(bufSearch.String()), //nolint: gosec + fhtml = &fileHtml{ + Body: template.HTML(buf.String()), } + buf.Reset() + err = srv.converter.tmpl.Execute(&buf, fhtml) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) @@ -107,11 +107,13 @@ func (w *watcher) watchFileMarkup() { ns memfs.NodeState fmarkup *fileMarkup + ext string err error + ok bool ) for ns = range w.watchDir.C { - ext := strings.ToLower(filepath.Ext(ns.Node.SysPath)) + ext = strings.ToLower(filepath.Ext(ns.Node.SysPath)) if !isExtensionMarkup(ext) { continue } @@ -119,7 +121,7 @@ func (w *watcher) watchFileMarkup() { switch ns.State { case memfs.FileStateDeleted: fmt.Printf("%s: %q deleted\n", logp, ns.Node.SysPath) - fmarkup, ok := w.fileMarkups[ns.Node.SysPath] + fmarkup, ok = w.fileMarkups[ns.Node.SysPath] if ok { delete(w.fileMarkups, ns.Node.SysPath) w.changes.Push(fmarkup) diff --git a/watcher_test.go b/watcher_test.go index a2e7f43..0e082f2 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -71,7 +71,9 @@ func TestWatcher(t *testing.T) { func testCreate(t *testing.T) { var ( + got *fileMarkup err error + expBody string gotBody []byte ) @@ -81,11 +83,11 @@ func testCreate(t *testing.T) { t.Fatal(err) } - got := waitChanges() + got = waitChanges() test.Assert(t, "New adoc file created", testFileAdoc, got.path) - expBody := `<!DOCTYPE> + expBody = `<!DOCTYPE> <html> <head><title></title></head> <body> @@ -112,7 +114,9 @@ func testCreate(t *testing.T) { func testUpdate(t *testing.T) { var ( err error + expBody string gotBody []byte + got *fileMarkup ) _, err = testAdocFile.WriteString("= Hello") @@ -124,10 +128,10 @@ func testUpdate(t *testing.T) { t.Fatal(err) } - got := waitChanges() + got = waitChanges() test.Assert(t, "adoc file updated", testFileAdoc, got.path) - expBody := `<!DOCTYPE> + expBody = `<!DOCTYPE> <html> <head><title>Hello</title></head> <body> @@ -154,7 +158,13 @@ func testUpdate(t *testing.T) { } func testDelete(t *testing.T) { - err := testAdocFile.Close() + var ( + err error + got *fileMarkup + gotIsExist bool + ) + + err = testAdocFile.Close() if err != nil { t.Fatal(err) } @@ -164,10 +174,10 @@ func testDelete(t *testing.T) { t.Fatal(err) } - got := waitChanges() + got = waitChanges() test.Assert(t, "adoc file updated", testFileAdoc, got.path) - _, gotIsExist := testWatcher.fileMarkups[testFileAdoc] + _, gotIsExist = testWatcher.fileMarkups[testFileAdoc] test.Assert(t, "adoc file deleted", false, gotIsExist) } |
