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 /pkg/types/module.go | |
| parent | e28b6f056b57f1a5199f1d233cecf3d5527ad89b (diff) | |
Make types be able to template themselves
Signed-off-by: Nicolas Paul <n@nc0.fr>
Diffstat (limited to 'pkg/types/module.go')
| -rw-r--r-- | pkg/types/module.go | 37 |
1 files changed, 37 insertions, 0 deletions
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 } |
