111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
|
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) }
|
||
|
}
|