diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 06:44:59 -0500 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 06:44:59 -0500 |
| commit | 93670740c05def63ae4b02c99eb49391f663ff38 (patch) | |
| tree | 77bbd8d664622154155fc06e2c4228cfac4fec04 /init.go | |
| parent | Add license (diff) | |
| download | ssg-93670740c05def63ae4b02c99eb49391f663ff38.zip | |
Add initial version of ssg
Diffstat (limited to 'init.go')
| -rw-r--r-- | init.go | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -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 +} |