newpost command: accept --date flag for past or future dates

This commit is contained in:
gutmet 2022-12-05 21:28:19 +01:00
parent 7991ce686b
commit 5d04342b42

View File

@ -2,16 +2,18 @@ package post
import ( import (
"bytes" "bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
goutil "git.gutmet.org/goutil.git/misc"
"git.gutmet.org/wombat.git/blog"
gen "git.gutmet.org/wombat.git/generate"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"text/template" "text/template"
"time" "time"
goutil "git.gutmet.org/goutil.git/misc"
"git.gutmet.org/wombat.git/blog"
gen "git.gutmet.org/wombat.git/generate"
) )
const dateformat string = "2006-01-02" const dateformat string = "2006-01-02"
@ -42,8 +44,7 @@ func ensureDirectory(file string) {
} }
} }
func makePostFile(dirsuffix string, title string, content string) string { func makePostFile(now time.Time, dirsuffix string, title string, content string) string {
now := time.Now().UTC()
file := filename(dirsuffix, title, now) file := filename(dirsuffix, title, now)
ensureDirectory(file) ensureDirectory(file)
post := post{} post := post{}
@ -72,7 +73,13 @@ func postNew(fl postFlags) error {
tmpcontent, _ := ioutil.ReadAll(os.Stdin) tmpcontent, _ := ioutil.ReadAll(os.Stdin)
content = string(tmpcontent) content = string(tmpcontent)
} }
f := makePostFile(fl.blog, fl.title, content) var t time.Time
if fl.datetime.val != nil {
t = *fl.datetime.val
} else {
t = time.Now().UTC()
}
f := makePostFile(t, fl.blog, fl.title, content)
fmt.Println(f) fmt.Println(f)
if content == "" { if content == "" {
wait := true wait := true
@ -89,11 +96,35 @@ func postNew(fl postFlags) error {
return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve}) return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve})
} }
type DateFlag struct {
val *time.Time
}
func (d DateFlag) String() string {
if d.val == nil {
return ""
} else {
return time.Time(*d.val).Format(blog.TimeFormat)
}
}
func (d *DateFlag) Set(s string) error {
if d == nil {
return errors.New("func (d *DateFlag) Set(s string): d is nil")
}
val, err := time.Parse(blog.TimeFormat, s)
if err == nil {
d.val = &val
}
return err
}
type postFlags struct { type postFlags struct {
blog string blog string
title string title string
category string category string
noserve bool noserve bool
datetime DateFlag
} }
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) { func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
@ -103,6 +134,7 @@ func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
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") s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards")
s.Var(&f.datetime, "date", "use date and time other than now (yyyy-mm-dd HH:MM)")
} }
return flagsInit, func([]string) error { return postNew(f) } return flagsInit, func([]string) error { return postNew(f) }
} }