|
4 | 4 | package utils |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "bufio" |
8 | | - "bytes" |
9 | 7 | "fmt" |
10 | 8 | "os" |
11 | 9 | "os/exec" |
@@ -200,55 +198,3 @@ func GetProjectDir() (string, error) { |
200 | 198 | wd = strings.ReplaceAll(wd, "/test/e2e", "") |
201 | 199 | return wd, nil |
202 | 200 | } |
203 | | - |
204 | | -// UncommentCode searches for target in the file and remove the comment prefix |
205 | | -// of the target content. The target content may span multiple lines. |
206 | | -func UncommentCode(filename, target, prefix string) error { |
207 | | - // false positive |
208 | | - // nolint:gosec |
209 | | - content, err := os.ReadFile(filename) |
210 | | - if err != nil { |
211 | | - return fmt.Errorf("failed to read file %q: %w", filename, err) |
212 | | - } |
213 | | - strContent := string(content) |
214 | | - |
215 | | - idx := strings.Index(strContent, target) |
216 | | - if idx < 0 { |
217 | | - return fmt.Errorf("unable to find the code %q to be uncomment", target) |
218 | | - } |
219 | | - |
220 | | - out := new(bytes.Buffer) |
221 | | - _, err = out.Write(content[:idx]) |
222 | | - if err != nil { |
223 | | - return fmt.Errorf("failed to write to output: %w", err) |
224 | | - } |
225 | | - |
226 | | - scanner := bufio.NewScanner(bytes.NewBufferString(target)) |
227 | | - if !scanner.Scan() { |
228 | | - return nil |
229 | | - } |
230 | | - for { |
231 | | - if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil { |
232 | | - return fmt.Errorf("failed to write to output: %w", err) |
233 | | - } |
234 | | - // Avoid writing a newline in case the previous line was the last in target. |
235 | | - if !scanner.Scan() { |
236 | | - break |
237 | | - } |
238 | | - if _, err = out.WriteString("\n"); err != nil { |
239 | | - return fmt.Errorf("failed to write to output: %w", err) |
240 | | - } |
241 | | - } |
242 | | - |
243 | | - if _, err = out.Write(content[idx+len(target):]); err != nil { |
244 | | - return fmt.Errorf("failed to write to output: %w", err) |
245 | | - } |
246 | | - |
247 | | - // false positive |
248 | | - // nolint:gosec |
249 | | - if err = os.WriteFile(filename, out.Bytes(), 0o644); err != nil { |
250 | | - return fmt.Errorf("failed to write file %q: %w", filename, err) |
251 | | - } |
252 | | - |
253 | | - return nil |
254 | | -} |
0 commit comments