create: Refactor NewContent to be testable

NewContent is refactored to use the afero.Fs interface that should allow
full testing.  This commit also pulls the metadata creation logic out of
NewContent and into a separate function to decrease the cyclomatic
complexity of NewContent.
This commit is contained in:
Cameron Moore
2016-03-17 13:30:33 -05:00
committed by Bjørn Erik Pedersen
parent bafb77172b
commit 9323707b32
3 changed files with 197 additions and 55 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@ package create
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path"
@@ -23,24 +22,26 @@ import (
"strings"
"time"
"github.com/spf13/afero"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/parser"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
func NewContent(kind, name string) (err error) {
// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(fs afero.Fs, kind, name string) (err error) {
jww.INFO.Println("attempting to create ", name, "of", kind)
location := FindArchetype(kind)
location := FindArchetype(fs, kind)
var by []byte
if location != "" {
by, err = ioutil.ReadFile(location)
by, err = afero.ReadFile(fs, location)
if err != nil {
jww.ERROR.Println(err)
}
@@ -53,58 +54,24 @@ func NewContent(kind, name string) (err error) {
if err != nil {
return err
}
metadata, err := psr.Metadata()
metadata, err := createMetadata(psr, name)
if err != nil {
jww.ERROR.Printf("Error processing archetype file %s: %s\n", location, err)
return err
}
newmetadata, err := cast.ToStringMapE(metadata)
if err != nil {
jww.ERROR.Println("Error processing archetype file:", location)
return err
}
for k := range newmetadata {
switch strings.ToLower(k) {
case "date":
newmetadata[k] = time.Now()
case "title":
newmetadata[k] = helpers.MakeTitle(helpers.Filename(name))
}
}
caseimatch := func(m map[string]interface{}, key string) bool {
for k := range m {
if strings.ToLower(k) == strings.ToLower(key) {
return true
}
}
return false
}
if newmetadata == nil {
newmetadata = make(map[string]interface{})
}
if !caseimatch(newmetadata, "date") {
newmetadata["date"] = time.Now()
}
if !caseimatch(newmetadata, "title") {
newmetadata["title"] = helpers.MakeTitle(helpers.Filename(name))
}
page, err := hugolib.NewPage(name)
if err != nil {
return err
}
if x := parser.FormatSanitize(viper.GetString("MetaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
newmetadata["date"] = time.Now().Format(time.RFC3339)
if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat"))); err != nil {
return
}
//page.Dir = viper.GetString("sourceDir")
page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat")))
page.SetSourceContent(psr.Content())
if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
return
}
@@ -120,21 +87,72 @@ func NewContent(kind, name string) (err error) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return
}
return cmd.Run()
}
return nil
}
func FindArchetype(kind string) (outpath string) {
// createMetadata generates Metadata for a new page based upon the metadata
// found in an archetype.
func createMetadata(archetype parser.Page, name string) (map[string]interface{}, error) {
archMetadata, err := archetype.Metadata()
if err != nil {
return nil, err
}
metadata, err := cast.ToStringMapE(archMetadata)
if err != nil {
return nil, err
}
for k := range metadata {
switch strings.ToLower(k) {
case "date":
metadata[k] = time.Now()
case "title":
metadata[k] = helpers.MakeTitle(helpers.Filename(name))
}
}
caseimatch := func(m map[string]interface{}, key string) bool {
for k := range m {
if strings.ToLower(k) == strings.ToLower(key) {
return true
}
}
return false
}
if metadata == nil {
metadata = make(map[string]interface{})
}
if !caseimatch(metadata, "date") {
metadata["date"] = time.Now()
}
if !caseimatch(metadata, "title") {
metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
}
if x := parser.FormatSanitize(viper.GetString("MetaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
metadata["date"] = time.Now().Format(time.RFC3339)
}
return metadata, nil
}
// FindArchetype takes a given kind/archetype of content and returns an output
// path for that archetype. If no archetype is found, an empty string is
// returned.
func FindArchetype(fs afero.Fs, kind string) (outpath string) {
search := []string{helpers.AbsPathify(viper.GetString("archetypeDir"))}
if viper.GetString("theme") != "" {
themeDir := filepath.Join(helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), "/archetypes/")
if _, err := os.Stat(themeDir); os.IsNotExist(err) {
jww.ERROR.Println("Unable to find archetypes directory for theme :", viper.GetString("theme"), "in", themeDir)
if _, err := fs.Stat(themeDir); os.IsNotExist(err) {
jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", viper.GetString("theme"), themeDir)
} else {
search = append(search, themeDir)
}
@@ -154,7 +172,7 @@ func FindArchetype(kind string) (outpath string) {
for _, p := range pathsToCheck {
curpath := filepath.Join(x, p)
jww.DEBUG.Println("checking", curpath, "for archetypes")
if exists, _ := helpers.Exists(curpath, hugofs.SourceFs); exists {
if exists, _ := helpers.Exists(curpath, fs); exists {
jww.INFO.Println("curpath: " + curpath)
return curpath
}