diff options
| author | Nicolas Paul <n@nc0.fr> | 2023-05-31 12:58:27 +0200 |
|---|---|---|
| committer | Nicolas Paul <n@nc0.fr> | 2023-05-31 12:58:27 +0200 |
| commit | cf989e2bbe6ec1f2792a588ee0d5fddb1d425bb3 (patch) | |
| tree | bb797defe57d982f08c9df605680a32a8ff9daf5 | |
| parent | e28b6f056b57f1a5199f1d233cecf3d5527ad89b (diff) | |
Make types be able to template themselves
Signed-off-by: Nicolas Paul <n@nc0.fr>
| -rw-r--r-- | pkg/types/index.go | 35 | ||||
| -rw-r--r-- | pkg/types/module.go | 37 |
2 files changed, 71 insertions, 1 deletions
diff --git a/pkg/types/index.go b/pkg/types/index.go index f58d5f2..623555c 100644 --- a/pkg/types/index.go +++ b/pkg/types/index.go @@ -1,6 +1,11 @@ package types -import "sync" +import ( + "go.nc0.fr/svgu/pkg/templates" + "os" + "path" + "sync" +) // Index is the global object representing the Starlark configuration. type Index struct { @@ -45,3 +50,31 @@ func (i *Index) CheckModule(n string) bool { _, ok := i.Modules[n] return ok } + +// GenerateFile generates the index file. +func (i *Index) GenerateFile(out string) error { + i.lock.Lock() + defer i.lock.Unlock() + + f := path.Join(out, "index.html") + + // Create the file. + fd, err := os.Create(f) + if err != nil { + return err + } + defer func(fd *os.File) { + err := fd.Close() + if err != nil { + panic(err) + } + }(fd) + + // Execute the template and write the output to the file. + if err := templates.ExecIndex(fd, + "https://pkg.go.dev", 2); err != nil { + return err + } + + return nil +} diff --git a/pkg/types/module.go b/pkg/types/module.go index 0c4e8af..6cee7b1 100644 --- a/pkg/types/module.go +++ b/pkg/types/module.go @@ -1,5 +1,12 @@ package types +import ( + "go.nc0.fr/svgu/pkg/templates" + "os" + "path" + "sync" +) + // Vcs is an enum for version control systems supported by the standard Go // toolchain. // @@ -22,4 +29,34 @@ type Module struct { Repo string // repository's home Dir string // url template File string // url template + // internal + lock sync.Mutex +} + +// GenerateFile generates the index file. +func (m *Module) GenerateFile(out string, domain string) error { + m.lock.Lock() + defer m.lock.Unlock() + + f := path.Join(out, m.Path+".html") + + // Create the file. + fd, err := os.Create(f) + if err != nil { + return err + } + defer func(fd *os.File) { + err := fd.Close() + if err != nil { + panic(err) + } + }(fd) + + // Execute the template and write the output to the file. + if err := templates.ExecModule(fd, domain+m.Path, string(m.Vcs), + m.Repo, m.Dir, m.File); err != nil { + return err + } + + return nil } |
