Skip to content

Commit

Permalink
Merge pull request #1754 from prafull01/kb-edit-validation
Browse files Browse the repository at this point in the history
🐛 kubebuilder edit ignore the errors if the project layout is already on same type
  • Loading branch information
k8s-ci-robot committed Nov 6, 2020
2 parents b249df4 + 40fec84 commit 8d5e286
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
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) cmdutil.Scaffolde

// 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
}

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) cmdutil.Scaffolde

// 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

0 comments on commit 8d5e286

Please sign in to comment.