summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/types/index.go35
-rw-r--r--pkg/types/module.go37
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
}