[lxc-devel] [distrobuilder/master] More consistent new-line handling
stgraber on Github
lxc-bot at linuxcontainers.org
Thu Mar 15 02:15:50 UTC 2018
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 362 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180315/cc3516ad/attachment.bin>
-------------- next part --------------
From cf9a51df820abb86f59557b1e5978a2da5d7171a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 14 Mar 2018 21:26:50 -0400
Subject: [PATCH] More consistent new-line handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
generators/dump.go | 11 ++++++++++-
image/lxc.go | 25 +++++++++++++++++--------
sources/debootstrap.go | 6 ++++++
sources/ubuntu-http.go | 5 +++++
4 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/generators/dump.go b/generators/dump.go
index c5825e3..a03aecb 100644
--- a/generators/dump.go
+++ b/generators/dump.go
@@ -3,6 +3,7 @@ package generators
import (
"os"
"path/filepath"
+ "strings"
"github.com/lxc/distrobuilder/image"
"github.com/lxc/distrobuilder/shared"
@@ -29,18 +30,26 @@ func (g DumpGenerator) Run(cacheDir, sourceDir string, defFile shared.Definition
}
func (g DumpGenerator) dumpFile(path, content string) error {
+ // Create any missing directory
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return err
}
+ // Open the target file (create if needed)
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
- _, err = file.WriteString(content + "\n")
+ // Append final new line if missing
+ if !strings.HasSuffix(content, "\n") {
+ content += "\n"
+ }
+
+ // Write the content
+ _, err = file.WriteString(content)
if err != nil {
return err
}
diff --git a/image/lxc.go b/image/lxc.go
index fc7d190..fa18e98 100644
--- a/image/lxc.go
+++ b/image/lxc.go
@@ -159,8 +159,7 @@ func (l *LXCImage) createMetadata() error {
}
}
- err = l.writeMetadata(filepath.Join(metaDir, "excludes-user"), strings.TrimSuffix(excludesUser, "\n"),
- false)
+ err = l.writeMetadata(filepath.Join(metaDir, "excludes-user"), excludesUser, false)
if err != nil {
return fmt.Errorf("Error writing 'excludes-user': %s", err)
}
@@ -193,17 +192,21 @@ func (l *LXCImage) packMetadata() error {
return nil
}
-func (l *LXCImage) writeMetadata(filename, content string, append bool) error {
+func (l *LXCImage) writeMetadata(filename, content string, appendContent bool) error {
var file *os.File
var err error
- if append {
+ // Open the file either in append or create mode
+ if appendContent {
file, err = os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ return err
+ }
} else {
file, err = os.Create(filename)
- }
- if err != nil {
- return err
+ if err != nil {
+ return err
+ }
}
defer file.Close()
@@ -212,7 +215,13 @@ func (l *LXCImage) writeMetadata(filename, content string, append bool) error {
return err
}
- _, err = file.WriteString(out + "\n")
+ // Append final new line if missing
+ if !strings.HasSuffix(out, "\n") {
+ out += "\n"
+ }
+
+ // Write the content
+ _, err = file.WriteString(out)
if err != nil {
return err
}
diff --git a/sources/debootstrap.go b/sources/debootstrap.go
index 4b39f5f..705d9f0 100644
--- a/sources/debootstrap.go
+++ b/sources/debootstrap.go
@@ -4,6 +4,7 @@ import (
"os"
"path"
"path/filepath"
+ "strings"
"gopkg.in/flosch/pongo2.v3"
@@ -82,6 +83,11 @@ func (s *Debootstrap) Run(source shared.DefinitionSource, release, arch, rootfsD
return err
}
+ // Append final new line if missing
+ if !strings.HasSuffix(out, "\n") {
+ out += "\n"
+ }
+
// Replace content of sources.list with the templated content.
file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", "sources.list"))
if err != nil {
diff --git a/sources/ubuntu-http.go b/sources/ubuntu-http.go
index e7f8a44..182cf86 100644
--- a/sources/ubuntu-http.go
+++ b/sources/ubuntu-http.go
@@ -99,6 +99,11 @@ func (s *UbuntuHTTP) Run(source shared.DefinitionSource, release, arch, rootfsDi
return err
}
+ // Append final new line if missing
+ if !strings.HasSuffix(out, "\n") {
+ out += "\n"
+ }
+
// Replace content of sources.list with the templated content.
file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", "sources.list"))
if err != nil {
More information about the lxc-devel
mailing list