Skip to content

Commit 9f80d59

Browse files
⚠️ 🐛 fix: remove duplicate HasFragment method, replaced by HasFileContentWith (#4191)
The HasFragment method was mistakenly added, leading to code duplication. The existing HasFileContentWith method already handles the same functionality, so HasFragment has been removed to avoid redundancy.
1 parent 5950493 commit 9f80d59

File tree

3 files changed

+6
-25
lines changed

3 files changed

+6
-25
lines changed

pkg/plugin/util/util.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,6 @@ func EnsureExistAndReplace(input, match, replace string) (string, error) {
226226
return strings.Replace(input, match, replace, -1), nil
227227
}
228228

229-
func HasFragment(path, target string) (bool, error) {
230-
_, err := os.Stat(path)
231-
if err != nil {
232-
return false, err
233-
}
234-
235-
// false positive
236-
// nolint:gosec
237-
b, err := os.ReadFile(path)
238-
if err != nil {
239-
return false, err
240-
}
241-
242-
if !strings.Contains(string(b), target) {
243-
return false, nil
244-
}
245-
return true, nil
246-
}
247-
248229
// ReplaceInFile replaces all instances of old with new in the file at path.
249230
func ReplaceInFile(path, old, new string) error {
250231
info, err := os.Stat(path)

pkg/plugins/common/kustomize/v2/scaffolds/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (s *apiScaffolder) Scaffold() error {
9393
kustomizeFilePath := "config/default/kustomization.yaml"
9494
err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../crd", `#`)
9595
if err != nil {
96-
hasCRUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../crd")
96+
hasCRUncommented, err := pluginutil.HasFileContentWith(kustomizeFilePath, "- ../crd")
9797
if !hasCRUncommented || err != nil {
9898
log.Errorf("Unable to find the target #- ../crd to uncomment in the file "+
9999
"%s.", kustomizeFilePath)

pkg/plugins/common/kustomize/v2/scaffolds/webhook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *webhookScaffolder) Scaffold() error {
101101
kustomizeFilePath := "config/default/kustomization.yaml"
102102
err = pluginutil.UncommentCode(kustomizeFilePath, "#- ../webhook", `#`)
103103
if err != nil {
104-
hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../webhook")
104+
hasWebHookUncommented, err := pluginutil.HasFileContentWith(kustomizeFilePath, "- ../webhook")
105105
if !hasWebHookUncommented || err != nil {
106106
log.Errorf("Unable to find the target #- ../webhook to uncomment in the file "+
107107
"%s.", kustomizeFilePath)
@@ -110,7 +110,7 @@ func (s *webhookScaffolder) Scaffold() error {
110110

111111
err = pluginutil.UncommentCode(kustomizeFilePath, "#patches:", `#`)
112112
if err != nil {
113-
hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "patches:")
113+
hasWebHookUncommented, err := pluginutil.HasFileContentWith(kustomizeFilePath, "patches:")
114114
if !hasWebHookUncommented || err != nil {
115115
log.Errorf("Unable to find the line '#patches:' to uncomment in the file "+
116116
"%s.", kustomizeFilePath)
@@ -119,7 +119,7 @@ func (s *webhookScaffolder) Scaffold() error {
119119

120120
err = pluginutil.UncommentCode(kustomizeFilePath, "#- path: manager_webhook_patch.yaml", `#`)
121121
if err != nil {
122-
hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- path: manager_webhook_patch.yaml")
122+
hasWebHookUncommented, err := pluginutil.HasFileContentWith(kustomizeFilePath, "- path: manager_webhook_patch.yaml")
123123
if !hasWebHookUncommented || err != nil {
124124
log.Errorf("Unable to find the target #- path: manager_webhook_patch.yaml to uncomment in the file "+
125125
"%s.", kustomizeFilePath)
@@ -129,7 +129,7 @@ func (s *webhookScaffolder) Scaffold() error {
129129
crdKustomizationsFilePath := "config/crd/kustomization.yaml"
130130
err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#- path: patches/webhook", `#`)
131131
if err != nil {
132-
hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- path: patches/webhook")
132+
hasWebHookUncommented, err := pluginutil.HasFileContentWith(crdKustomizationsFilePath, "- path: patches/webhook")
133133
if !hasWebHookUncommented || err != nil {
134134
log.Errorf("Unable to find the target(s) #- path: patches/webhook/* to uncomment in the file "+
135135
"%s.", crdKustomizationsFilePath)
@@ -138,7 +138,7 @@ func (s *webhookScaffolder) Scaffold() error {
138138

139139
err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#configurations:\n#- kustomizeconfig.yaml", `#`)
140140
if err != nil {
141-
hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- kustomizeconfig.yaml")
141+
hasWebHookUncommented, err := pluginutil.HasFileContentWith(crdKustomizationsFilePath, "- kustomizeconfig.yaml")
142142
if !hasWebHookUncommented || err != nil {
143143
log.Errorf("Unable to find the target(s) #configurations:\n#- kustomizeconfig.yaml to uncomment in the file "+
144144
"%s.", crdKustomizationsFilePath)

0 commit comments

Comments
 (0)