Rework config handling
Anirudh Oppiliappan x@icyphox.sh
Sun, 08 Aug 2021 09:58:50 +0530
5 files changed,
32 insertions(+),
26 deletions(-)
M
atom/feed.go
→
atom/feed.go
@@ -52,7 +52,8 @@
// Creates a new Atom feed. func NewAtomFeed(srcDir string, posts []markdown.Output) ([]byte, error) { entries := []AtomEntry{} - config := config.Config + cfg := config.ConfigYaml{} + cfg.ParseConfig() for _, p := range posts { dateStr := p.Meta["date"] date, err := time.Parse("2006-01-02", dateStr)@@ -67,11 +68,11 @@ Updated: rfc3339,
// tag:icyphox.sh,2019-10-23:blog/some-post/ ID: fmt.Sprintf( "tag:%s,%s:%s", - config.URL[8:], // strip https:// + cfg.URL[8:], // strip https:// dateStr, filepath.Join(srcDir, p.Meta["slug"]), ), - Link: &AtomLink{Href: filepath.Join(config.URL, srcDir, p.Meta["slug"])}, + Link: &AtomLink{Href: filepath.Join(cfg.URL, srcDir, p.Meta["slug"])}, Summary: &AtomSummary{Content: string(p.HTML), Type: "html"}, } entries = append(entries, entry)@@ -81,13 +82,13 @@ // 2021-07-14T00:00:00Z
now := time.Now().Format(time.RFC3339) feed := &AtomFeed{ Xmlns: "http://www.w3.org/2005/Atom", - Title: config.Title, - ID: config.URL, - Subtitle: config.Desc, - Link: &AtomLink{Href: config.URL}, + Title: cfg.Title, + ID: cfg.URL, + Subtitle: cfg.Desc, + Link: &AtomLink{Href: cfg.URL}, Author: &AtomAuthor{ - Name: config.Author.Name, - Email: config.Author.Email, + Name: cfg.Author.Name, + Email: cfg.Author.Email, }, Updated: now, Entries: entries,
M
commands/build.go
→
commands/build.go
@@ -70,6 +70,10 @@ }
out := markdown.Output{} out.RenderMarkdown(fb) + cfg := config.ConfigYaml{} + if err := cfg.ParseConfig(); err != nil { + return err + } if err = out.RenderHTML( htmlFile, TEMPLATES,@@ -77,7 +81,7 @@ struct {
Cfg config.ConfigYaml Meta markdown.Matter Body string - }{config.Config, out.Meta, string(out.HTML)}, + }{cfg, out.Meta, string(out.HTML)}, ); err != nil { return err }@@ -125,6 +129,10 @@ }
out := markdown.Output{} out.RenderMarkdown(fb) + cfg := config.ConfigYaml{} + if err := cfg.ParseConfig(); err != nil { + return err + } if err = out.RenderHTML( htmlFile,@@ -133,7 +141,7 @@ struct {
Cfg config.ConfigYaml Meta markdown.Matter Body string - }{config.Config, out.Meta, string(out.HTML)}, + }{cfg, out.Meta, string(out.HTML)}, ); err != nil { return err }@@ -160,13 +168,17 @@ return err
} out := markdown.Output{} out.RenderMarkdown(indexMd) + cfg := config.ConfigYaml{} + if err := cfg.ParseConfig(); err != nil { + return err + } out.RenderHTML(indexHTML, TEMPLATES, struct { Cfg config.ConfigYaml Meta markdown.Matter Body string Posts []markdown.Output - }{config.Config, out.Meta, string(out.HTML), posts}) + }{cfg, out.Meta, string(out.HTML), posts}) // Create feeds // ex: build/blog/feed.xml
M
commands/init.go
→
commands/init.go
@@ -18,6 +18,6 @@ return err
} } fp, _ := filepath.Abs(path) - fmt.Printf("vite: created project at %q\n", fp) + fmt.Printf("vite: created project at %s\n", fp) return nil }
M
commands/new.go
→
commands/new.go
@@ -25,5 +25,6 @@ if err != nil {
return err } os.WriteFile(path, []byte(content), 0755) + fmt.Printf("vite: created new post at %s\n", path) return nil }
M
config/config.go
→
config/config.go
@@ -1,7 +1,6 @@
package config import ( - "fmt" "os" "gopkg.in/yaml.v3"@@ -19,20 +18,13 @@ // Prebuild []string `yaml:"prebuild"`
// Postbuild []string `yaml:"postbuild"` } -var Config ConfigYaml - -func init() { +func (c *ConfigYaml) ParseConfig() error { cf, err := os.ReadFile("config.yaml") if err != nil { - fmt.Fprintf(os.Stderr, "error: %+v\n", err) - os.Exit(1) + return err } - if err = Config.ParseConfig(cf); err != nil { - fmt.Fprintf(os.Stderr, "error: %+v\n", err) + if err = yaml.Unmarshal(cf, c); err != nil { + return err } -} - -func (c *ConfigYaml) ParseConfig(cf []byte) error { - err := yaml.Unmarshal(cf, c) - return err + return nil }