package initer
import (
"flag"
"git.gutmet.org/goutil.git"
"os"
"path/filepath"
)
const htmlTemplate = `
{{.Title}}
{{.Title}}
{{if .Links}}{{range $index, $link := .Links}}
{{$link.Name}}{{if $link.Space}}
{{end}}{{end}}{{end}}
{{if .Images}}{{range $index, $img := .Images}}
{{if $.Artsy}}
{{end}}{{end}}{{end}}
{{.Back.Name}}
`
const mdTemplate = `---
title: {{.Title}}
---
{{if .Links}}{{range $index, $link := .Links}}{{$link.Name}}
{{if $link.Space}}
{{end}}{{end}}{{end}}
{{if .Images}}{{range $index, $img := .Images}}{{if $.Artsy}}
{{end}}{{end}}{{end}}
{{.Back.Name}}
`
type IniterFlags struct {
Dir string
Markdown bool
Artsy bool
}
func InitializedOrDie(dir string) {
paths := []string{imgFolder(dir), InitFlag(dir), TemplateFile(dir)}
for _, p := range paths {
if !goutil.PathExists(p) {
panic("Not a finstr folder? Did not find " + p)
}
}
}
func imgFolder(dir string) string {
return filepath.Join(dir, "img")
}
func InitFlag(dir string) string {
return filepath.Join(dir, ".finstr")
}
func readme(dir string) string {
return filepath.Join(imgFolder(dir), "Readme")
}
func TemplateFile(dir string) string {
return filepath.Join(dir, "template")
}
func Init(fl IniterFlags) error {
dir := fl.Dir
err := os.MkdirAll(imgFolder(dir), 0755)
var style string
if fl.Artsy {
style = "artsy"
} else {
style = "square"
}
var template string
if fl.Markdown {
template = mdTemplate
} else {
template = htmlTemplate
}
err = goutil.OptDo(err, func() error { return goutil.WriteFile(InitFlag(dir), style) })
err = goutil.OptDo(err, func() error { return goutil.WriteFile(readme(dir), "Put your .jpgs and .tags here") })
err = goutil.OptDo(err, func() error { return goutil.WriteFile(TemplateFile(dir), template) })
return err
}
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
f := IniterFlags{}
flagsInit := func(s *flag.FlagSet) {
s.BoolVar(&f.Markdown, "markdown", false, "init with Markdown template instead of HTML")
s.StringVar(&f.Dir, "dir", "", "initialize 'dir' instead of current working directory")
s.BoolVar(&f.Artsy, "artsy", false, "mark gallery to generate thumbnails with original ratio instead of square")
}
return flagsInit, func([]string) error {
return Init(f)
}
}