From 86d5654d20f8671a9bf4687469d53c6aca8f274a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Fri, 17 Jun 2022 14:45:37 +0200 Subject: [PATCH] Preserve trailing newline setting when adding to gitignore --- pkg/commands/oscommands/os.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index b9ea928be..3a42d09db 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -109,13 +109,32 @@ func (c *OSCommand) Quote(message string) string { // AppendLineToFile adds a new line in file func (c *OSCommand) AppendLineToFile(filename, line string) error { c.LogCommand(fmt.Sprintf("Appending '%s' to file '%s'", line, filename), false) - f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600) + f, err := os.OpenFile(filename, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0o600) if err != nil { return utils.WrapError(err) } defer f.Close() - _, err = f.WriteString("\n" + line) + info, err := os.Stat(filename) + if err != nil { + return utils.WrapError(err) + } + + // read last char + buf := make([]byte, 1) + if _, err := f.ReadAt(buf, info.Size()-1); err != nil { + return utils.WrapError(err) + } + + // if the last byte of the file is not a newline, add it + if []byte("\n")[0] != buf[0] { + _, err = f.WriteString("\n") + } + + if err == nil { + _, err = f.WriteString(line + "\n") + } + if err != nil { return utils.WrapError(err) }