wombat/initer/initer.go
2024-06-18 23:01:52 +02:00

101 lines
1.8 KiB
Go

package initer
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
finstr "git.fireandbrimst.one/aw/finstr/initer"
goutil "git.fireandbrimst.one/aw/goutil/misc"
"git.fireandbrimst.one/aw/wombat/gallery"
)
const (
initflag = ".wombat"
)
var checkpaths []string = []string{initflag, "stage0", "template", "style.css", "script.js"}
func dumpInitFiles() error {
err := goutil.WriteFile("template", defaultTemplate)
if err != nil {
return err
}
err = goutil.WriteFile("rssTemplate", defaultRSSTemplate)
if err != nil {
return err
}
err = goutil.WriteFile("style.css", defaultStyle)
if err != nil {
return err
}
err = goutil.WriteFile("script.js", defaultScript)
if err != nil {
return err
}
err = goutil.WriteFile(filepath.Join("stage0", "index.md"), defaultIndex)
if err != nil {
return err
}
return gallery.New(finstr.IniterFlags{})
}
func isInitialized() bool {
for _, path := range checkpaths {
if !goutil.PathExists(path) {
return false
}
}
return true
}
func initialize() error {
var err error
if !isInitialized() {
dir, err := os.Getwd()
if err != nil {
return err
}
fmt.Println("Initializing " + dir)
err = goutil.WriteFile(initflag, "")
if err != nil {
return err
}
err = os.Mkdir("stage0", 0755)
if err != nil {
return err
}
err = os.Mkdir("stage0/blog", 0755)
if err != nil {
return err
}
err = dumpInitFiles()
}
return err
}
func InitializedOrDie() {
if !isInitialized() {
dir, err := os.Getwd()
if err == nil {
err = errors.New(dir + " is not an initialized wombat directory")
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type initializeFlags struct {
style string
}
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
flagsInit := func(s *flag.FlagSet) {
}
return flagsInit, func([]string) error {
return initialize()
}
}