1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-23 17:12:18 +01:00

Add 'kiln new' command

This commit is contained in:
adnano 2021-04-20 16:00:39 -04:00
parent 3bebd0bf12
commit 3dfdf507dc
4 changed files with 78 additions and 15 deletions

16
config.toml Normal file

@ -0,0 +1,16 @@
# Site title
title = ""
# Site URLs
urls = []
# Site feeds
[feeds]
# "/blog/" = "Example blog"
# Site tasks
[tasks.gemini]
input_ext = ".gmi"
output_ext = ".gmi"
template_ext = ".gmi"
static_dir = "static"
output_dir = "public"

@ -6,14 +6,30 @@ kiln - a simple static site generator
# SYNOPSIS # SYNOPSIS
_kiln_ [-c <config>] [-t <task>] *kiln* build++
[-c _config_]++
[-t _task_]
*kiln* new _path_
*kiln* version
# DESCRIPTION
*kiln build* builds a kiln site.
*kiln new* creates a new kiln site at the given path.
*kiln version* prints version information for the *kiln* program.
# OPTIONS # OPTIONS
\-c <config> ## kiln build
*-c* _config_
Specifies the configuration file to use. Defaults to "config.toml". Specifies the configuration file to use. Defaults to "config.toml".
\-t <task> *-t* _task_
Specifies the task to run. Defaults to "all", which runs all tasks. Specifies the task to run. Defaults to "all", which runs all tasks.
# OVERVIEW # OVERVIEW

49
main.go

@ -7,6 +7,7 @@ import (
"io/fs" "io/fs"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -16,22 +17,38 @@ var (
) )
func main() { func main() {
if len(os.Args) >= 2 {
switch os.Args[1] {
case "build":
build()
return
case "new":
if len(os.Args) < 3 {
fmt.Println("usage: kiln new <path>")
os.Exit(1)
}
newSite(os.Args[2])
return
case "version":
fmt.Println("kiln", Version)
return
}
}
fmt.Println("usage: kiln <build | new | version> args...")
os.Exit(1)
}
func build() {
var ( var (
task string task string
config string config string
version bool
) )
flag.StringVar(&task, "t", "all", "the task to run") flag.StringVar(&task, "t", "all", "the task to run")
flag.StringVar(&config, "c", "config.toml", "the configuration file to use") flag.StringVar(&config, "c", "config.toml", "the configuration file to use")
flag.BoolVar(&version, "v", false, "print version")
flag.Parse() flag.Parse()
if version {
fmt.Println("kiln", Version)
return
}
// Load config // Load config
cfg, err := LoadConfig(config) cfg, err := LoadConfig(config)
if err != nil { if err != nil {
@ -123,3 +140,17 @@ func copyAll(srcDir, dstDir string) error {
return nil return nil
}) })
} }
func newSite(name string) {
name = path.Clean(name)
os.Mkdir(name, 0755)
os.Mkdir(path.Join(name, "content"), 0755)
os.Mkdir(path.Join(name, "templates"), 0755)
os.Mkdir(path.Join(name, "static"), 0755)
config, _ := builtin.ReadFile("config.toml")
os.WriteFile(path.Join(name, "config.toml"), config, 0644)
index := []byte("# Hello, world!\n")
os.WriteFile(path.Join(name, "content/index.gmi"), index, 0644)
}

@ -11,8 +11,8 @@ import (
"text/template" "text/template"
) )
//go:embed templates //go:embed templates config.toml
var builtinTemplates embed.FS var builtin embed.FS
// Templates contains site templates. // Templates contains site templates.
type Templates struct { type Templates struct {
@ -43,7 +43,7 @@ func (t *Templates) LoadDefault() {
} }
func (t *Templates) loadDefault(name string) { func (t *Templates) loadDefault(name string) {
b, err := builtinTemplates.ReadFile("templates/" + name) b, err := builtin.ReadFile("templates/" + name)
if err != nil { if err != nil {
panic(err) panic(err)
} }