wombat/initer/initer.go

118 lines
2.2 KiB
Go

package initer
import (
"errors"
"flag"
"fmt"
finstr "git.gutmet.org/finstr.git/initer"
"git.gutmet.org/goutil.git"
"git.gutmet.org/wombat.git/gallery"
"os"
"path/filepath"
)
const (
initflag = ".wombat"
)
type Style int
const (
Minimal Style = 0
BSStarter Style = 1
)
func templateAndCSS(style Style) (string, string) {
switch style {
case BSStarter:
return bsStarterTemplate, bsStarterStyle
case Minimal:
fallthrough
default:
return defaultTemplate, defaultStyle
}
}
var checkpaths []string = []string{initflag, "stage0", "template", "style.css"}
func dumpInitFiles(style Style) error {
template, css := templateAndCSS(style)
err := goutil.WriteFile("template", template)
if err != nil {
return err
}
err = goutil.WriteFile("style.css", css)
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(style Style) 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(style)
}
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) {
f := initializeFlags{}
flagsInit := func(s *flag.FlagSet) {
s.StringVar(&f.style, "style", "minimal", "\"minimal\" or \"evil\" (Bootstrap)")
}
return flagsInit, func([]string) error {
if f.style == "evil" {
fmt.Println("You should feel bad, but anyway...")
return initialize(BSStarter)
} else {
return initialize(Minimal)
}
}
}