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

🐛 kubebuilder edit ignore the errors if the project layout is already on same type #1754

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions pkg/plugin/v2/scaffolds/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func NewEditScaffolder(config *config.Config, multigroup bool) scaffold.Scaffold

// Scaffold implements Scaffolder
func (s *editScaffolder) Scaffold() error {
s.config.MultiGroup = s.multigroup
filename := "Dockerfile"
bs, err := ioutil.ReadFile(filename)
if err != nil {
Expand All @@ -56,21 +55,29 @@ func (s *editScaffolder) Scaffold() error {
str,
"COPY api/ api/",
`COPY apis/ apis/`)
if err != nil {
return err
}
} else {
str, err = ensureExistAndReplace(
str,
"COPY apis/ apis/",
`COPY api/ api/`)
if err != nil {
return err
}
}
// false positive
// nolint:gosec
return ioutil.WriteFile(filename, []byte(str), 0644)

// Ignore the error encountered, if the file is already in desired format.
if err != nil && s.multigroup != s.config.MultiGroup {
return err
}

s.config.MultiGroup = s.multigroup

// Check if the str is not empty, because when the file is already in desired format it will return empty string
// because there is nothing to replace.
if str != "" {
// false positive
// nolint:gosec
return ioutil.WriteFile(filename, []byte(str), 0644)
}

return nil
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
}

func ensureExistAndReplace(input, match, replace string) (string, error) {
Expand Down
28 changes: 18 additions & 10 deletions pkg/plugin/v3/scaffolds/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func NewEditScaffolder(config *config.Config, multigroup bool) scaffold.Scaffold

// Scaffold implements Scaffolder
func (s *editScaffolder) Scaffold() error {
s.config.MultiGroup = s.multigroup
filename := "Dockerfile"
bs, err := ioutil.ReadFile(filename)
if err != nil {
Expand All @@ -56,21 +55,30 @@ func (s *editScaffolder) Scaffold() error {
str,
"COPY api/ api/",
`COPY apis/ apis/`)
if err != nil {
return err
}

} else {
str, err = ensureExistAndReplace(
str,
"COPY apis/ apis/",
`COPY api/ api/`)
if err != nil {
return err
}
}
// false positive
// nolint:gosec
return ioutil.WriteFile(filename, []byte(str), 0644)

// Ignore the error encountered, if the file is already in desired format.
if err != nil && s.multigroup != s.config.MultiGroup {
return err
}

s.config.MultiGroup = s.multigroup

// Check if the str is not empty, because when the file is already in desired format it will return empty string
// because there is nothing to replace.
if str != "" {
// false positive
// nolint:gosec
return ioutil.WriteFile(filename, []byte(str), 0644)
}

return nil
}

func ensureExistAndReplace(input, match, replace string) (string, error) {
Expand Down