Skip to content

Commit

Permalink
Split injecting fields and validating files
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Jan 13, 2020
1 parent a173def commit 2326246
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions pkg/scaffold/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ type File interface {
GetInput() (Input, error)
}

// Validate validates input
type Validate interface {
// RequiresValidation is a file that requires validation
type RequiresValidation interface {
File
// Validate returns true if the template has valid values
Validate() error
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Plugin interface {
Pipe(u *model.Universe) error
}

func (s *Scaffold) setFieldsAndValidate(t input.File) error {
func (s *Scaffold) setFields(t input.File) {
// Set boilerplate on templates
if b, ok := t.(input.BoilerplatePath); ok {
b.SetBoilerplatePath(s.BoilerplatePath)
Expand All @@ -98,12 +98,13 @@ func (s *Scaffold) setFieldsAndValidate(t input.File) error {
if b, ok := t.(input.MultiGroup); ok {
b.SetMultiGroup(s.Project.MultiGroup)
}
// Validate the template is ok
if v, ok := t.(input.Validate); ok {
if err := v.Validate(); err != nil {
return err
}
}

func (_ *Scaffold) validate(file input.File) error {
if reqValFile, ok := file.(input.RequiresValidation); ok {
return reqValFile.Validate()
}

return nil
}

Expand Down Expand Up @@ -234,8 +235,10 @@ func isAlreadyExistsError(e error) bool {
// doFile scaffolds a single file
func (s *Scaffold) buildFileModel(e input.File) (*model.File, error) {
// Set common fields
err := s.setFieldsAndValidate(e)
if err != nil {
s.setFields(e)

// Validate the file scaffold
if err := s.validate(e); err != nil {
return nil, err
}

Expand Down

0 comments on commit 2326246

Please sign in to comment.