Skip to content

Commit 608e4cc

Browse files
committed
generateHostname: simplify
No functionality change, just code simplification. Covered by existing test (e.g. "run should also override /etc/hostname" in tests/run.bats). NO NEW TESTS NEEDED Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent f261d66 commit 608e4cc

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

Diff for: run_common.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,8 @@ func (b *Builder) addHostsEntries(file, imageRoot string, entries etchosts.HostE
181181

182182
// generateHostname creates a containers /etc/hostname file
183183
func (b *Builder) generateHostname(rdir, hostname string, chownOpts *idtools.IDPair) (string, error) {
184-
var err error
185-
hostnamePath := "/etc/hostname"
186-
187-
var hostnameBuffer bytes.Buffer
188-
hostnameBuffer.Write([]byte(fmt.Sprintf("%s\n", hostname)))
189-
190-
cfile := filepath.Join(rdir, filepath.Base(hostnamePath))
191-
if err = ioutils.AtomicWriteFile(cfile, hostnameBuffer.Bytes(), 0o644); err != nil {
184+
cfile := filepath.Join(rdir, "hostname")
185+
if err := ioutils.AtomicWriteFile(cfile, append([]byte(hostname), '\n'), 0o644); err != nil {
192186
return "", fmt.Errorf("writing /etc/hostname into the container: %w", err)
193187
}
194188

@@ -198,7 +192,7 @@ func (b *Builder) generateHostname(rdir, hostname string, chownOpts *idtools.IDP
198192
uid = chownOpts.UID
199193
gid = chownOpts.GID
200194
}
201-
if err = os.Chown(cfile, uid, gid); err != nil {
195+
if err := os.Chown(cfile, uid, gid); err != nil {
202196
return "", err
203197
}
204198
if err := relabel(cfile, b.MountLabel, false); err != nil {

0 commit comments

Comments
 (0)