-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,309 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package scaffold | ||
|
||
// ConcreteEntry is an existing file or directory that created from the scaffold template | ||
type ConcreteEntry interface { | ||
Entry | ||
TemplatePath() string | ||
} | ||
|
||
type concreteEntry struct { | ||
Entry | ||
templatePath string | ||
} | ||
|
||
// NewConcreteFile returns a new TemplateEntry object treated as a file | ||
func NewConcreteFile(path, content, templatePath string) ConcreteEntry { | ||
return NewConcreteEntry(path, content, false, templatePath) | ||
} | ||
|
||
// NewConcreteDir returns a new TemplateEntry object treated as a directory | ||
func NewConcreteDir(path, templatePath string) ConcreteEntry { | ||
return NewConcreteEntry(path, "", true, templatePath) | ||
} | ||
|
||
// NewConcreteEntry returns a new Entry object | ||
func NewConcreteEntry(path, content string, dir bool, templatePath string) ConcreteEntry { | ||
return &concreteEntry{ | ||
Entry: NewEntry(path, content, dir), | ||
templatePath: templatePath, | ||
} | ||
} | ||
|
||
func (e *concreteEntry) TemplatePath() string { | ||
return e.templatePath | ||
} |
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,92 @@ | ||
package scaffold | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_NewConcreteEntry(t *testing.T) { | ||
cases := []struct { | ||
path string | ||
content string | ||
dir bool | ||
tmplPath string | ||
}{ | ||
{ | ||
path: "/app", | ||
content: "", | ||
dir: true, | ||
tmplPath: "/{{name}}", | ||
}, | ||
{ | ||
path: "/app/foo.go", | ||
content: "package app", | ||
dir: false, | ||
tmplPath: "/{{name}}/foo.go", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
e := NewConcreteEntry(c.path, c.content, c.dir, c.tmplPath) | ||
|
||
if actual, expected := e.Path(), c.path; actual != expected { | ||
t.Errorf("Path() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.Content(), c.content; actual != expected { | ||
t.Errorf("Content() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.IsDir(), c.dir; actual != expected { | ||
t.Errorf("IsDir() returns %t, want %t", actual, expected) | ||
} | ||
|
||
if actual, expected := e.TemplatePath(), c.tmplPath; actual != expected { | ||
t.Errorf("TemplatePath() returns %q, want %q", actual, expected) | ||
} | ||
} | ||
} | ||
|
||
func Test_NewConcreteFile(t *testing.T) { | ||
path := "/app/foo.go" | ||
content := "package app" | ||
tmplPath := "/{{name}}/foo.go" | ||
e := NewConcreteFile(path, content, tmplPath) | ||
|
||
if actual, expected := e.Path(), path; actual != expected { | ||
t.Errorf("Path() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.Content(), content; actual != expected { | ||
t.Errorf("Content() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.IsDir(), false; actual != expected { | ||
t.Errorf("IsDir() returns %t, want %t", actual, expected) | ||
} | ||
|
||
if actual, expected := e.TemplatePath(), tmplPath; actual != expected { | ||
t.Errorf("TemplatePath() returns %q, want %q", actual, expected) | ||
} | ||
} | ||
|
||
func Test_NewConcreteDir(t *testing.T) { | ||
path := "/app/foo.go" | ||
tmplPath := "/{{name}}/foo.go" | ||
e := NewConcreteDir(path, tmplPath) | ||
|
||
if actual, expected := e.Path(), path; actual != expected { | ||
t.Errorf("Path() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.Content(), ""; actual != expected { | ||
t.Errorf("Content() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.IsDir(), true; actual != expected { | ||
t.Errorf("IsDir() returns %t, want %t", actual, expected) | ||
} | ||
|
||
if actual, expected := e.TemplatePath(), tmplPath; actual != expected { | ||
t.Errorf("TemplatePath() returns %q, want %q", actual, expected) | ||
} | ||
} |
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,35 @@ | ||
package scaffold | ||
|
||
// Entry represents a filesystem file or directory | ||
type Entry interface { | ||
Path() string | ||
IsDir() bool | ||
Content() string | ||
} | ||
|
||
type entry struct { | ||
path string | ||
content string | ||
dir bool | ||
} | ||
|
||
// NewEntry returns a new Entry object | ||
func NewEntry(path, content string, dir bool) Entry { | ||
return &entry{ | ||
path: path, | ||
content: content, | ||
dir: dir, | ||
} | ||
} | ||
|
||
func (e *entry) Path() string { | ||
return e.path | ||
} | ||
|
||
func (e *entry) Content() string { | ||
return e.content | ||
} | ||
|
||
func (e *entry) IsDir() bool { | ||
return e.dir | ||
} |
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,38 @@ | ||
package scaffold | ||
|
||
import "testing" | ||
|
||
func Test_NewEntry(t *testing.T) { | ||
cases := []struct { | ||
path string | ||
content string | ||
dir bool | ||
}{ | ||
{ | ||
path: "/app", | ||
content: "", | ||
dir: true, | ||
}, | ||
{ | ||
path: "/app/foo.go", | ||
content: "package app", | ||
dir: false, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
e := NewEntry(c.path, c.content, c.dir) | ||
|
||
if actual, expected := e.Path(), c.path; actual != expected { | ||
t.Errorf("Path() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.Content(), c.content; actual != expected { | ||
t.Errorf("Content() returns %q, want %q", actual, expected) | ||
} | ||
|
||
if actual, expected := e.IsDir(), c.dir; actual != expected { | ||
t.Errorf("IsDir() returns %t, want %t", actual, expected) | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package scaffold | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// TemplateEntry represents a scaffold template entry | ||
type TemplateEntry interface { | ||
Entry | ||
Compile(v interface{}) (Entry, error) | ||
} | ||
|
||
type templateEntry struct { | ||
path TemplateString | ||
content TemplateString | ||
dir bool | ||
} | ||
|
||
// NewTemplateFile returns a new TemplateEntry object treated as a file | ||
func NewTemplateFile(path, content TemplateString) TemplateEntry { | ||
return NewTemplateEntry(path, content, false) | ||
} | ||
|
||
// NewTemplateDir returns a new TemplateEntry object treated as a directory | ||
func NewTemplateDir(path TemplateString) TemplateEntry { | ||
return NewTemplateEntry(path, "", true) | ||
} | ||
|
||
// NewTemplateEntry returns a new TemplateEntry object | ||
func NewTemplateEntry(path, content TemplateString, dir bool) TemplateEntry { | ||
return &templateEntry{ | ||
path: path, | ||
content: content, | ||
dir: dir, | ||
} | ||
} | ||
|
||
func (e *templateEntry) Path() string { | ||
return string(e.path) | ||
} | ||
|
||
func (e *templateEntry) IsDir() bool { | ||
return e.dir | ||
} | ||
|
||
func (e *templateEntry) Content() string { | ||
return string(e.content) | ||
} | ||
|
||
func (e *templateEntry) Compile(v interface{}) (Entry, error) { | ||
path, err := e.path.Compile(string(e.path), v) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Could not compile path: %q", e.path) | ||
} | ||
content, err := e.content.Compile(string(e.path), v) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Could not compile content: %q", e.path) | ||
} | ||
return NewEntry(path, content, e.dir), nil | ||
} |
Oops, something went wrong.