AddToGitIgnore adds an entry to the .gitignore file if it doesn't already exist.
(logger *zap.Logger, path string, ignoreString string)
| 1317 | |
| 1318 | // AddToGitIgnore adds an entry to the .gitignore file if it doesn't already exist. |
| 1319 | func AddToGitIgnore(logger *zap.Logger, path string, ignoreString string) error { |
| 1320 | gitignorePath := path + "/.gitignore" |
| 1321 | |
| 1322 | file, err := os.OpenFile(gitignorePath, os.O_RDWR|os.O_CREATE, 0644) |
| 1323 | if err != nil { |
| 1324 | return fmt.Errorf("error opening or creating .gitignore file: %v", err) |
| 1325 | } |
| 1326 | |
| 1327 | defer func() { |
| 1328 | if err := file.Close(); err != nil { |
| 1329 | logger.Error("error closing .gitignore file", zap.Error(err)) |
| 1330 | } |
| 1331 | }() |
| 1332 | |
| 1333 | scanner := bufio.NewScanner(file) |
| 1334 | found := false |
| 1335 | for scanner.Scan() { |
| 1336 | if strings.TrimSpace(scanner.Text()) == ignoreString { |
| 1337 | found = true |
| 1338 | break |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if !found { |
| 1343 | if _, err := file.WriteString("\n" + ignoreString + "\n"); err != nil { |
| 1344 | return fmt.Errorf("error writing to .gitignore file: %v", err) |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | return nil |
| 1349 | } |
| 1350 | |
| 1351 | func Hash(data []byte) string { |
| 1352 | hasher := sha256.New() |
no test coverage detected