aboutsummaryrefslogtreecommitdiffstats
path: root/init.go
diff options
context:
space:
mode:
Diffstat (limited to 'init.go')
-rw-r--r--init.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/init.go b/init.go
new file mode 100644
index 0000000..46b2fc9
--- /dev/null
+++ b/init.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "git.sr.ht/~eugenetriguba/subcmd"
+)
+
+type initCmd struct{}
+
+func (c *initCmd) Name() string { return "init" }
+func (c *initCmd) Synopsis() string { return "initialize a new site" }
+func (c *initCmd) Usage() string {
+ return "init [directory]\n\nSee ssg-init(1) for details.\n"
+}
+func (c *initCmd) SetFlags(f *flag.FlagSet) {}
+func (c *initCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcmd.ExitStatus {
+ dir := "."
+ if f.NArg() > 0 {
+ dir = f.Arg(0)
+ }
+
+ dirs := []string{
+ filepath.Join(dir, "data"),
+ filepath.Join(dir, "templates"),
+ filepath.Join(dir, "static"),
+ }
+
+ for _, d := range dirs {
+ if err := os.MkdirAll(d, 0755); err != nil {
+ fatal("creating %s: %v", d, err)
+ }
+ fmt.Println(d + "/")
+ }
+
+ indexPath := filepath.Join(dir, "templates", "index.html")
+ if _, err := os.Stat(indexPath); os.IsNotExist(err) {
+ index := `<!doctype html>
+<html>
+ <head>
+ <title>My Site</title>
+ </head>
+ <body>
+ <h1>Hello, world!</h1>
+ </body>
+</html>
+`
+ if err := os.WriteFile(indexPath, []byte(index), 0644); err != nil {
+ fatal("creating index.html: %v", err)
+ }
+ fmt.Println("templates/index.html")
+ }
+
+ return subcmd.ExitSuccess
+}