Skip to content

Commit

Permalink
Added skeleton module to create an example project. A go generate com…
Browse files Browse the repository at this point in the history
…mand scans the files in skeleton/files and creates a statikFS module (rakyll/statik) to store the files in the binary. Added skeleton to the cmd package. Update Gopkg dependency files.
  • Loading branch information
taik0 committed Mar 13, 2018
1 parent 70a906b commit 3105208
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
api2html
generator/test/output
coverage.out
vendor
vendor
statik
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@
[[constraint]]
name = "github.com/newrelic/go-agent"
version = "1.11.0"

[[constraint]]
name = "github.com/rakyll/statik"
version = "0.1.1"
39 changes: 39 additions & 0 deletions cmd/skeleton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"github.com/devopsfaith/api2html/skeleton"
"github.com/spf13/cobra"
)

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",
}
)

func init() {
rootCmd.AddCommand(skelCmd)

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)
}

type skelWrapper struct {
sk skelFactory
}

func (sw skelWrapper) Create(_ *cobra.Command, _ []string) error {
return sw.sk(outputPath).Create()
}
63 changes: 63 additions & 0 deletions cmd/skeleton_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/devopsfaith/api2html/skeleton"
)

func Test_defaultSkelFactory(t *testing.T) {
g := defaultSkelFactory("test")
switch g.(type) {
case skeleton.Skel:
default:
t.Errorf("unexpected generator type: %T", g)
}
}

func Test_skelWrapper_koErroredSkel(t *testing.T) {
expectedError := fmt.Errorf("expect me")

skel := skelWrapper{func(_ string) skeleton.Skel {
return erroredSkel{expectedError}
}}

if err := skel.Create(nil, []string{}); err == nil {
t.Error("expecting error!")
} else if err != expectedError {
t.Errorf("unexpected error! want: %s, got: %s", expectedError.Error(), err.Error())
}
}

func Test_skelWrapper(t *testing.T) {

skel := skelWrapper{func(_ string) skeleton.Skel {
return simpleSkel{outputPath}
}}

defer os.Remove("skel_example")
if err := skel.Create(nil, []string{}); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if _, err := os.Stat("skel_example"); err != nil {
t.Errorf("cannot locate test output dir: %s", err.Error())
}
}

type erroredSkel struct {
err error
}

func (e erroredSkel) Create() error {
return e.err
}

type simpleSkel struct {
outputPath string
}

func (s simpleSkel) Create() error {
return os.Mkdir(s.outputPath, os.ModePerm)
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

//go:generate statik -src=skeleton/files

import (
"log"

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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions skeleton/skeleton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package skeleton

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

// Import the statikFS from the generated files
_ "github.com/devopsfaith/api2html/statik"
"github.com/rakyll/statik/fs"
)

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

// Skel defines the interface for creating skeleton files
type Skel interface {
Create() error
}

type statikSkel struct {
outputPath string
}

// Generate the skel from the statikFS
func (s *statikSkel) Create() error {
statikFS, err := fs.New()
if err != nil {
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",
} {
f, err := statikFS.Open(name)
if err != nil {
fmt.Printf("opening file %s: %s\n", name, err.Error())
return err
}
defer f.Close()
buff := new(bytes.Buffer)
buff.ReadFrom(f)
if err != nil {
return err
}
path := fmt.Sprintf("%s/%s", 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)
if err != nil {
return err
}
fmt.Printf("Creating skeleton file: %s%s\n", s.outputPath, name)
}
return nil
}
40 changes: 40 additions & 0 deletions skeleton/skeleton_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package skeleton

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestNewSkel(t *testing.T) {
defaultSkel := New("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 {
return err
}
if !f.IsDir() {
counter++
}
return nil
})
if err != nil {
t.Errorf("Problem walking the test directory: %s", err.Error())
}
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")
expectedHello := string(`Hello, I am a text/plain content.`)
if string(hellodata) != expectedHello {
t.Error("Invalid content on hello.txt file.")
}
}

0 comments on commit 3105208

Please sign in to comment.