Skip to content

Commit 40fec84

Browse files
committed
🐛 Ignore the errors if the project layout is already on same type
1 parent 70108bf commit 40fec84

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

pkg/plugin/v2/scaffolds/edit.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func NewEditScaffolder(config *config.Config, multigroup bool) scaffold.Scaffold
4242

4343
// Scaffold implements Scaffolder
4444
func (s *editScaffolder) Scaffold() error {
45-
s.config.MultiGroup = s.multigroup
4645
filename := "Dockerfile"
4746
bs, err := ioutil.ReadFile(filename)
4847
if err != nil {
@@ -56,21 +55,29 @@ func (s *editScaffolder) Scaffold() error {
5655
str,
5756
"COPY api/ api/",
5857
`COPY apis/ apis/`)
59-
if err != nil {
60-
return err
61-
}
6258
} else {
6359
str, err = ensureExistAndReplace(
6460
str,
6561
"COPY apis/ apis/",
6662
`COPY api/ api/`)
67-
if err != nil {
68-
return err
69-
}
7063
}
71-
// false positive
72-
// nolint:gosec
73-
return ioutil.WriteFile(filename, []byte(str), 0644)
64+
65+
// Ignore the error encountered, if the file is already in desired format.
66+
if err != nil && s.multigroup != s.config.MultiGroup {
67+
return err
68+
}
69+
70+
s.config.MultiGroup = s.multigroup
71+
72+
// Check if the str is not empty, because when the file is already in desired format it will return empty string
73+
// because there is nothing to replace.
74+
if str != "" {
75+
// false positive
76+
// nolint:gosec
77+
return ioutil.WriteFile(filename, []byte(str), 0644)
78+
}
79+
80+
return nil
7481
}
7582

7683
func ensureExistAndReplace(input, match, replace string) (string, error) {

pkg/plugin/v3/scaffolds/edit.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func NewEditScaffolder(config *config.Config, multigroup bool) scaffold.Scaffold
4242

4343
// Scaffold implements Scaffolder
4444
func (s *editScaffolder) Scaffold() error {
45-
s.config.MultiGroup = s.multigroup
4645
filename := "Dockerfile"
4746
bs, err := ioutil.ReadFile(filename)
4847
if err != nil {
@@ -56,21 +55,30 @@ func (s *editScaffolder) Scaffold() error {
5655
str,
5756
"COPY api/ api/",
5857
`COPY apis/ apis/`)
59-
if err != nil {
60-
return err
61-
}
58+
6259
} else {
6360
str, err = ensureExistAndReplace(
6461
str,
6562
"COPY apis/ apis/",
6663
`COPY api/ api/`)
67-
if err != nil {
68-
return err
69-
}
7064
}
71-
// false positive
72-
// nolint:gosec
73-
return ioutil.WriteFile(filename, []byte(str), 0644)
65+
66+
// Ignore the error encountered, if the file is already in desired format.
67+
if err != nil && s.multigroup != s.config.MultiGroup {
68+
return err
69+
}
70+
71+
s.config.MultiGroup = s.multigroup
72+
73+
// Check if the str is not empty, because when the file is already in desired format it will return empty string
74+
// because there is nothing to replace.
75+
if str != "" {
76+
// false positive
77+
// nolint:gosec
78+
return ioutil.WriteFile(filename, []byte(str), 0644)
79+
}
80+
81+
return nil
7482
}
7583

7684
func ensureExistAndReplace(input, match, replace string) (string, error) {

0 commit comments

Comments
 (0)