command unification

* 'post' > 'newpost'
* 'newpage' as new command
* fixed 'newgallery' to check for init
This commit is contained in:
gutmet 2021-06-26 17:09:41 +02:00
parent 634c427f07
commit 3e86c03557
5 changed files with 130 additions and 12 deletions

View File

@ -15,7 +15,7 @@ description: This describes foobar
Blog posts (located in stage0/blog/) have a similar header with values for title, date and categories. Blog posts (located in stage0/blog/) have a similar header with values for title, date and categories.
Normal pages are created by hand, new blog posts via "wombat post". Normal pages are created via "wombat newpage", new blog posts via "wombat newpost".
build build

View File

@ -9,7 +9,6 @@ import (
"git.gutmet.org/wombat.git/blog" "git.gutmet.org/wombat.git/blog"
"git.gutmet.org/wombat.git/converter" "git.gutmet.org/wombat.git/converter"
"git.gutmet.org/wombat.git/gallery" "git.gutmet.org/wombat.git/gallery"
"git.gutmet.org/wombat.git/initer"
"git.gutmet.org/wombat.git/site" "git.gutmet.org/wombat.git/site"
"git.gutmet.org/wombat.git/splitter" "git.gutmet.org/wombat.git/splitter"
"os" "os"
@ -60,7 +59,6 @@ func stage2() {
} }
func Generate(f GenerateFlags) error { func Generate(f GenerateFlags) error {
initer.InitializedOrDie()
clean() clean()
gallery.Generate(gallery.Flags{}) gallery.Generate(gallery.Flags{})
stage1() stage1()

110
page/page.go Normal file
View File

@ -0,0 +1,110 @@
package page
import (
"bytes"
"errors"
"flag"
"fmt"
goutil "git.gutmet.org/goutil.git/misc"
gen "git.gutmet.org/wombat.git/generate"
"io/ioutil"
"os"
"path/filepath"
"text/template"
)
var pageStub string = `---
title: {{.Title}}
description: {{.Description}}
---
`
type page struct {
Title string
Description string
}
type pageFlags struct {
title string
description string
path string
noserve bool
}
func ensureDirectory(file string) {
dir := filepath.Dir(file)
err := os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
func makePageFile(path string, title string, description string, content string) error {
ensureDirectory(path)
page := page{}
page.Title = title
page.Description = description
t := template.Must(template.New("pageStub").Parse(pageStub))
buf := new(bytes.Buffer)
t.Execute(buf, page)
err := goutil.WriteFile(path, buf.String()+content)
return err
}
func newPage(fl pageFlags) error {
var err error
var content string
if fl.path == "" {
return errors.New("need path")
}
if filepath.Ext(fl.path) != ".md" {
fl.path += ".md"
}
if goutil.PathExists(fl.path) {
return errors.New(fl.path + " already exists...")
}
if fl.description != "" && fl.title != "" {
tmpcontent, _ := ioutil.ReadAll(os.Stdin)
content = string(tmpcontent)
}
if fl.title == "" {
fl.title, err = goutil.AskFor("Title")
if err != nil {
return err
}
}
if fl.description == "" {
fl.description, err = goutil.AskFor("Description")
if err != nil {
return err
}
}
err = makePageFile(fl.path, fl.title, fl.description, content)
if err != nil {
return err
}
if content == "" {
wait := true
err = goutil.OpenInEditor(fl.path, wait)
if err != nil {
fmt.Println(err)
fmt.Println("trying default application...")
err = goutil.OpenInDefaultApp(fl.path, wait)
}
}
if err != nil {
return err
}
return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve})
}
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
f := pageFlags{}
flagsInit := func(s *flag.FlagSet) {
s.StringVar(&f.path, "path", "", "file to post to")
s.StringVar(&f.title, "title", "", "if title and description are set: read content from stdin")
s.StringVar(&f.description, "description", "", "if title and description are set: read content from stdin")
s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards")
}
return flagsInit, func([]string) error { return newPage(f) }
}

View File

@ -7,7 +7,6 @@ import (
goutil "git.gutmet.org/goutil.git/misc" goutil "git.gutmet.org/goutil.git/misc"
"git.gutmet.org/wombat.git/blog" "git.gutmet.org/wombat.git/blog"
gen "git.gutmet.org/wombat.git/generate" gen "git.gutmet.org/wombat.git/generate"
"git.gutmet.org/wombat.git/initer"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -62,7 +61,6 @@ func makePostFile(dirsuffix string, title string, content string) string {
func postNew(fl postFlags) error { func postNew(fl postFlags) error {
var err error var err error
initer.InitializedOrDie()
if fl.title == "" { if fl.title == "" {
fl.title, err = goutil.AskFor("Title") fl.title, err = goutil.AskFor("Title")
if err != nil { if err != nil {
@ -88,14 +86,14 @@ func postNew(fl postFlags) error {
if err != nil { if err != nil {
return err return err
} }
noserve := (content != "") return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve})
return gen.Generate(gen.GenerateFlags{Noserve: noserve})
} }
type postFlags struct { type postFlags struct {
blog string blog string
title string title string
category string category string
noserve bool
} }
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) { func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
@ -104,6 +102,7 @@ func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
s.StringVar(&f.blog, "blog", "", "which blog folder to post to, e.g. -blog=foo for 'blogfoo' (default \"blog\")") s.StringVar(&f.blog, "blog", "", "which blog folder to post to, e.g. -blog=foo for 'blogfoo' (default \"blog\")")
s.StringVar(&f.title, "title", "", "if title and category are set: read content from stdin") s.StringVar(&f.title, "title", "", "if title and category are set: read content from stdin")
s.StringVar(&f.category, "category", "", "if title and category are set: read content from stdin") s.StringVar(&f.category, "category", "", "if title and category are set: read content from stdin")
s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards")
} }
return flagsInit, func([]string) error { return postNew(f) } return flagsInit, func([]string) error { return postNew(f) }
} }

View File

@ -7,6 +7,7 @@ import (
"git.gutmet.org/wombat.git/gallery" "git.gutmet.org/wombat.git/gallery"
"git.gutmet.org/wombat.git/generate" "git.gutmet.org/wombat.git/generate"
"git.gutmet.org/wombat.git/initer" "git.gutmet.org/wombat.git/initer"
"git.gutmet.org/wombat.git/page"
"git.gutmet.org/wombat.git/post" "git.gutmet.org/wombat.git/post"
"os" "os"
) )
@ -16,14 +17,24 @@ func serve(args []string) error {
return nil return nil
} }
func ifInited(cmd goutil.Command) goutil.Command {
oldExec := cmd.Exec
cmd.Exec = func(args []string) error {
initer.InitializedOrDie()
return oldExec(args)
}
return cmd
}
func main() { func main() {
commands := []goutil.Command{ commands := []goutil.Command{
goutil.NewCommandWithFlags("init", initer.Command, "initialize wombat directory"), goutil.NewCommandWithFlags("init", initer.Command, "initialize wombat directory"),
goutil.NewCommandWithFlags("gen", generate.Command, "generate the website"), ifInited(goutil.NewCommandWithFlags("gen", generate.Command, "generate the website")),
goutil.NewCommandWithFlags("post", post.Command, "create a new blogpost"), ifInited(goutil.NewCommand("serve", serve, "just execute web server")),
goutil.NewCommand("serve", serve, "just execute web server"), ifInited(goutil.NewCommandWithFlags("newpost", post.Command, "create a new blogpost")),
goutil.NewCommandWithFlags("genpics", gallery.Command, "generate galleries"), ifInited(goutil.NewCommandWithFlags("newpage", page.Command, "create a new page")),
goutil.NewCommandWithFlags("newgallery", gallery.NewCommand, "init new gallery"), ifInited(goutil.NewCommandWithFlags("newgallery", gallery.NewCommand, "init new gallery")),
ifInited(goutil.NewCommandWithFlags("genpics", gallery.Command, "generate galleries")),
} }
err := goutil.Execute(commands) err := goutil.Execute(commands)
if err != nil { if err != nil {