-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added skeleton module to create an example project. A go generate com…
…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
Showing
27 changed files
with
245 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
api2html | ||
generator/test/output | ||
coverage.out | ||
vendor | ||
vendor | ||
statik |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package main | ||
|
||
//go:generate statik -src=skeleton/files | ||
|
||
import ( | ||
"log" | ||
|
||
|
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |