wombat/initer/initer.go

101 lines
1.8 KiB
Go
Raw Normal View History

2019-01-01 19:41:03 +01:00
package initer
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
finstr "git.gutmet.org/finstr.git/initer"
2020-10-11 13:35:13 +02:00
goutil "git.gutmet.org/goutil.git/misc"
2019-01-01 19:41:03 +01:00
"git.gutmet.org/wombat.git/gallery"
)
const (
initflag = ".wombat"
)
var checkpaths []string = []string{initflag, "stage0", "template", "style.css", "script.js"}
2019-01-01 19:41:03 +01:00
func dumpInitFiles() error {
err := goutil.WriteFile("template", defaultTemplate)
if err != nil {
return err
2019-01-01 19:41:03 +01:00
}
err = goutil.WriteFile("rssTemplate", defaultRSSTemplate)
2019-01-01 19:41:03 +01:00
if err != nil {
return err
}
err = goutil.WriteFile("style.css", defaultStyle)
2020-10-29 12:01:30 +01:00
if err != nil {
return err
}
err = goutil.WriteFile("script.js", defaultScript)
2019-01-01 19:41:03 +01:00
if err != nil {
return err
}
err = goutil.WriteFile(filepath.Join("stage0", "index.md"), defaultIndex)
if err != nil {
return err
}
return gallery.New(finstr.IniterFlags{})
2019-01-01 19:41:03 +01:00
}
func isInitialized() bool {
for _, path := range checkpaths {
if !goutil.PathExists(path) {
return false
}
}
return true
}
func initialize() error {
2019-01-01 19:41:03 +01:00
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()
2019-01-01 19:41:03 +01:00
}
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()
2019-01-01 19:41:03 +01:00
}
}