Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Scaffold enhancement] File interface #1226

Closed
Adirio opened this issue Nov 26, 2019 · 1 comment · Fixed by #1375
Closed

[Scaffold enhancement] File interface #1226

Adirio opened this issue Nov 26, 2019 · 1 comment · Fixed by #1375
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@Adirio
Copy link
Contributor

Adirio commented Nov 26, 2019

Context

Current situation

The current File interface is as follows:

// File is a scaffoldable file
type File interface {
// GetInput returns the Input for creating a scaffold file
GetInput() (Input, error)
}

The single method is not descriptive enough and does different tasks:

  1. Sets default values
  2. Returns a non-descriptive Input struct which contains:
    • Two fields needed to prepare the template: Path and TemplateBody
    • Several other fileds that are not used

Note: the Input struct also works as a parent class for mostly all File-implementing structs so that they can be injected some data from the scaffold. By mostly I mean all except the PROJECT file, which is an special-cased yaml file which doesn't have an embedded Input.

GetInput usage

I have tracked GetInput usages.

pkg/scaffold/project.go

Lines 101 and 106 for v1, 192 and 197 for v2:

projectInput, err := p.Project.GetInput()

bpInput, err := p.Boilerplate.GetInput()

projectInput, err := p.Project.GetInput()

bpInput, err := p.Boilerplate.GetInput()

The created variables are used in lines 113 and 127 for v1, and 204 and 218 for v2:
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},

input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},

input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},

input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},

Only the path is needed. Setting the default values (which is also done by GetInput) is not needed here except for the path itself.

pkg/scaffold/scaffold.go

Line 229:

i, err := e.GetInput()

Which is later used in line 235 and 238:
Path: i.Path,

if b, err := s.doTemplate(i, e); err != nil {

The usage in doTemplate are in lines 278, 291 and 292:
temp, err := newTemplate(e).Parse(i.TemplateBody)

if filepath.Ext(i.Path) == ".go" {
b, err = imports.Process(i.Path, b, nil)

The path is needed in lines 235, 291 and 292. The template body is needed in line 278. The side-effect of setting the default values is important here as the files are used to execute the templates in line 284:

err = temp.Execute(out, e)

pkg/scaffold/project/project_test.go

Line 49:

It("should skip writing boilerplate if the file exists", func() {
i, err := (&project.Boilerplate{}).GetInput()
Expect(err).NotTo(HaveOccurred())
Expect(i.IfExistsAction).To(Equal(input.Skip))
})

It is used to set the default values.

Proposal

A new interface is proposed:

type File interface {
	GetPath() string
	GetTemplateBody() string
	SetDefaults() error
}
Method usage
  • GetPath would be used in lines 113, 127, 204 and 218 of pkg/scaffold/project.go and in lines 235, 291 and 292 of pkg/scaffold/scaffold.go.
  • GetTemplateBody would be used in line 278 of pkg/scaffold/scaffold.go.
  • SetDefaults would be called before line 284 of pkg/scaffold/scaffold.go and in line 49 of pkg/scaffold/project/project_test.go.
  • All the GetInput calls with their error handlings could be removed.

This feature is part of a bigger change tracked in #1218

/kind feature

@Adirio Adirio added the kind/feature Categorizes issue or PR as related to a new feature. label Nov 26, 2019
@Adirio

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
1 participant