Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify text file line break even if file exists #124

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed build/initContainer/bscp.exe
Binary file not shown.
16 changes: 8 additions & 8 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,6 @@ func (c *ConfigItemFile) SaveToFile(dst string) error {
return err
}
}
// 3. check whether need to convert line break
if c.FileMeta.ConfigItemSpec.FileType == "text" && c.TextLineBreak != "" {
if err := util.ConvertTextLineBreak(dst, c.TextLineBreak); err != nil {
logger.Error("convert text file line break failed", slog.String("file", dst), logger.ErrAttr(err))
return err
}
}

return nil
}
Expand Down Expand Up @@ -580,7 +573,14 @@ func updateFiles(filesDir string, files []*ConfigItemFile, successDownloads *int
logger.Debug("file is already exists and has not been modified, skip download",
slog.String("file", filePath))
}
// 3. set file permission
// 4. check whether need to convert line break
if file.FileMeta.ConfigItemSpec.FileType == "text" && file.TextLineBreak != "" {
if err := util.ConvertTextLineBreak(filePath, file.TextLineBreak); err != nil {
logger.Error("convert text file line break failed", slog.String("file", filePath), logger.ErrAttr(err))
return err
}
}
// 5. set file permission
if runtime.GOOS != "windows" {
if err := util.SetFilePermission(filePath, file.FileMeta.ConfigItemSpec.Permission); err != nil {
logger.Warn("set file permission failed", slog.String("file", filePath), logger.ErrAttr(err))
Expand Down
2 changes: 1 addition & 1 deletion cmd/bscp/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func init() {
mustBindPFlag(pullViper, "file_cache.threshold_gb", PullCmd.Flags().Lookup("cache-threshold-gb"))
PullCmd.Flags().BoolP("enable-resource", "e", true, "enable report resource usage")
mustBindPFlag(pullViper, "enable_resource", PullCmd.Flags().Lookup("enable-resource"))
PullCmd.Flags().BoolP("text-line-break", "", false, "text file line break, default as LF")
PullCmd.Flags().StringP("text-line-break", "", "", "text file line break, default as LF")
mustBindPFlag(pullViper, "text_line_break", PullCmd.Flags().Lookup("text-line-break"))

for key, envName := range commonEnvs {
Expand Down
9 changes: 7 additions & 2 deletions internal/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ func ConvertTextLineBreak(filePath string, lineBreak string) error {
return err
}

// 将所有换行符规范化为 LF,使函数可重入执行
normalizedContent := strings.ReplaceAll(string(content), "\r\n", "\n")
normalizedContent = strings.ReplaceAll(normalizedContent, "\r", "\n")

var targetLineBreak string
switch lineBreak {
case "", "LF":
// default line break type is LF, no need to convert
return nil
targetLineBreak = "\n"
case "CRLF":
targetLineBreak = "\r\n"
case "CR":
Expand All @@ -88,7 +92,8 @@ func ConvertTextLineBreak(filePath string, lineBreak string) error {
return fmt.Errorf("invalid line break type: %s", lineBreak)
}

updatedContent := strings.ReplaceAll(string(content), "\n", targetLineBreak)
// 替换换行符
updatedContent := strings.ReplaceAll(normalizedContent, "\n", targetLineBreak)

// 写回文件
return os.WriteFile(filePath, []byte(updatedContent), 0644)
Expand Down
Loading