Skip to content

Commit

Permalink
Add a fileList to the statikSkel and build the skel given that fileLi…
Browse files Browse the repository at this point in the history
…st. Setup a separate NewBlog. Move the blog example to /files/blog.

Add multiple subcommands to skel create.
  • Loading branch information
taik0 committed Mar 13, 2018
1 parent 5cca997 commit db60ec9
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 50 deletions.
33 changes: 24 additions & 9 deletions cmd/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,40 @@ var (
outputPath string

skelCmd = &cobra.Command{
Use: "skeleton",
Short: "Run the api2html server.",
Long: "Run the api2html server.",
RunE: skelWrapper{defaultSkelFactory}.Create,
Aliases: []string{"skel"},
Example: "api2html skeleton -o skel_example",
Use: "skel",
Short: "skeleton command",
Aliases: []string{"skeleton"},
Example: "api2html skel",
}

createCmd = &cobra.Command{
Use: "create",
Short: "Creates a skeleton structure.",
Example: "api2html skel create",
}

blogCmd = &cobra.Command{
Use: "blog",
Short: "Creates the blog skeleton example.",
RunE: skelWrapper{defaultBlogSkelFactory}.Create,
Example: "api2html skel create blog",
}
)

func init() {
rootCmd.AddCommand(skelCmd)
skelCmd.AddCommand(createCmd)

blogCmd.PersistentFlags().StringVarP(&outputPath, "outputPath", "o", "blog_example", "Output path for the blog example generation")

createCmd.AddCommand(blogCmd)

skelCmd.PersistentFlags().StringVarP(&outputPath, "outputPath", "o", "skel_example", "Output path for the skel generation")
}

type skelFactory func(outputPath string) skeleton.Skel

func defaultSkelFactory(outputPath string) skeleton.Skel {
return skeleton.New(outputPath)
func defaultBlogSkelFactory(outputPath string) skeleton.Skel {
return skeleton.NewBlog(outputPath)
}

type skelWrapper struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/skeleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Test_defaultSkelFactory(t *testing.T) {
g := defaultSkelFactory("test")
g := defaultBlogSkelFactory("test")
switch g.(type) {
case skeleton.Skel:
default:
Expand Down Expand Up @@ -37,11 +37,11 @@ func Test_skelWrapper(t *testing.T) {
return simpleSkel{outputPath}
}}

defer os.Remove("skel_example")
defer os.Remove("blog_example")
if err := skel.Create(nil, []string{}); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if _, err := os.Stat("skel_example"); err != nil {
if _, err := os.Stat("blog_example"); err != nil {
t.Errorf("cannot locate test output dir: %s", err.Error())
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
69 changes: 37 additions & 32 deletions skeleton/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package skeleton

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -13,9 +12,38 @@ import (
"github.com/rakyll/statik/fs"
)

var (
blogContents = []string{
"/blog/config/es_ES/config.ini",
"/blog/config/es_ES/routes.ini",
"/blog/config/global/config.ini",
"/blog/config/global/routes.ini",
"/blog/i18n/es_ES.ini",
"/blog/i18n/en_US.ini",
"/blog/sources/es_ES/static/404",
"/blog/sources/es_ES/static/500",
"/blog/sources/es_ES/tmpl/home.mustache",
"/blog/sources/global/config.json",
"/blog/sources/global/Dockerfile",
"/blog/sources/global/static/404",
"/blog/sources/global/static/500",
"/blog/sources/global/static/hello.txt",
"/blog/sources/global/static/robots.txt",
"/blog/sources/global/static/sitemap.xml",
"/blog/sources/global/tmpl/home.mustache",
"/blog/sources/global/tmpl/main_layout.mustache",
"/blog/sources/global/tmpl/post.mustache",
}
)

// New returns a statikFS Skel
func New(outputPath string) Skel {
return &statikSkel{outputPath: outputPath}
func New(outputPath string, fileList []string) Skel {
return &statikSkel{outputPath: outputPath, fileList: fileList}
}

// NewBlog returns a statikSkel with the blog example contents
func NewBlog(outputPath string) Skel {
return &statikSkel{outputPath: outputPath, fileList: blogContents}
}

// Skel defines the interface for creating skeleton files
Expand All @@ -25,6 +53,7 @@ type Skel interface {

type statikSkel struct {
outputPath string
fileList []string
}

// Generate the skel from the statikFS
Expand All @@ -34,31 +63,7 @@ func (s *statikSkel) Create() error {
return err
}

if _, err := os.Stat(s.outputPath); err == nil {
return errors.New("output directory already exists")
}

for _, name := range []string{
"/config/es_ES/config.ini",
"/config/es_ES/routes.ini",
"/config/global/config.ini",
"/config/global/routes.ini",
"/i18n/es_ES.ini",
"/i18n/en_US.ini",
"/sources/es_ES/static/404",
"/sources/es_ES/static/500",
"/sources/es_ES/tmpl/home.mustache",
"/sources/global/config.json",
"/sources/global/Dockerfile",
"/sources/global/static/404",
"/sources/global/static/500",
"/sources/global/static/hello.txt",
"/sources/global/static/robots.txt",
"/sources/global/static/sitemap.xml",
"/sources/global/tmpl/home.mustache",
"/sources/global/tmpl/main_layout.mustache",
"/sources/global/tmpl/post.mustache",
} {
for _, name := range s.fileList {
f, err := statikFS.Open(name)
if err != nil {
fmt.Printf("opening file %s: %s\n", name, err.Error())
Expand All @@ -70,19 +75,19 @@ func (s *statikSkel) Create() error {
if err != nil {
return err
}
path := fmt.Sprintf("%s/%s", s.outputPath, filepath.Dir(name))
path := filepath.Join(s.outputPath, filepath.Dir(name))
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
}

err = ioutil.WriteFile(fmt.Sprintf("%s%s", s.outputPath, name), buff.Bytes(), os.ModePerm)
filename := filepath.Join(s.outputPath, name)
err = ioutil.WriteFile(filename, buff.Bytes(), os.ModePerm)
if err != nil {
return err
}
fmt.Printf("Creating skeleton file: %s%s\n", s.outputPath, name)
fmt.Printf("Creating skeleton file: %s\n", filename)
}
return nil
}
40 changes: 34 additions & 6 deletions skeleton/skeleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"testing"
)

func TestNewSkel(t *testing.T) {
defaultSkel := New("test")
func TestNewBlogSkel(t *testing.T) {
defaultSkel := NewBlog("test")
if err := defaultSkel.Create(); err != nil {
t.Errorf("Error creating skeleton files: %s", err.Error())
}
defer os.RemoveAll("test")
if err := defaultSkel.Create(); err.Error() != "output directory already exists" {
t.Errorf("output directory already exists error should have been raised.")
}

counter := 0
err := filepath.Walk("test", func(path string, f os.FileInfo, err error) error {
if _, err := os.Stat("test"); err != nil {
Expand All @@ -32,7 +30,37 @@ func TestNewSkel(t *testing.T) {
if counter != 19 {
t.Error("File count wrong, the test should have been generated 19 files.")
}
hellodata, _ := ioutil.ReadFile("test/sources/global/static/hello.txt")
hellodata, _ := ioutil.ReadFile("test/blog/sources/global/static/hello.txt")
expectedHello := string(`Hello, I am a text/plain content.`)
if string(hellodata) != expectedHello {
t.Error("Invalid content on hello.txt file.")
}
}

func TestNewSkel(t *testing.T) {
defaultSkel := New("test", []string{"/blog/i18n/es_ES.ini", "/blog/sources/global/static/hello.txt"})
if err := defaultSkel.Create(); err != nil {
t.Errorf("Error creating skeleton files: %s", err.Error())
}
defer os.RemoveAll("test")

counter := 0
err := filepath.Walk("test", func(path string, f os.FileInfo, err error) error {
if _, err := os.Stat("test"); err != nil {
return err
}
if !f.IsDir() {
counter++
}
return nil
})
if err != nil {
t.Errorf("Problem walking the test directory: %s", err.Error())
}
if counter != 2 {
t.Error("File count wrong, the test should have been generated 19 files.")
}
hellodata, _ := ioutil.ReadFile("test/blog/sources/global/static/hello.txt")
expectedHello := string(`Hello, I am a text/plain content.`)
if string(hellodata) != expectedHello {
t.Error("Invalid content on hello.txt file.")
Expand Down

0 comments on commit db60ec9

Please sign in to comment.