aboutsummaryrefslogtreecommitdiffstats
path: root/init.go
blob: 46b2fc98705627b754a6dae1281597817b13bcdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
}