Skip to content

Commit 95c61d2

Browse files
committed
fix: write content of dest file in place
1 parent cff2ec4 commit 95c61d2

File tree

1 file changed

+6
-37
lines changed

1 file changed

+6
-37
lines changed

internal/template/template.go

+6-37
Original file line numberDiff line numberDiff line change
@@ -190,48 +190,17 @@ func GenerateFile(config config.Config, containers context.Context) bool {
190190
}
191191

192192
if config.Dest != "" {
193-
dest, err := os.CreateTemp(filepath.Dir(config.Dest), "docker-gen")
194-
defer func() {
195-
dest.Close()
196-
os.Remove(dest.Name())
197-
}()
198-
if err != nil {
199-
log.Fatalf("Unable to create temp file: %s\n", err)
200-
}
201-
202-
if n, err := dest.Write(contents); n != len(contents) || err != nil {
203-
log.Fatalf("Failed to write to temp file: wrote %d, exp %d, err=%v", n, len(contents), err)
204-
}
205-
206-
oldContents := []byte{}
207-
if fi, err := os.Stat(config.Dest); err == nil || os.IsNotExist(err) {
208-
if err != nil && os.IsNotExist(err) {
209-
emptyFile, err := os.Create(config.Dest)
210-
if err != nil {
211-
log.Fatalf("Unable to create empty destination file: %s\n", err)
212-
} else {
213-
emptyFile.Close()
214-
fi, _ = os.Stat(config.Dest)
215-
}
216-
}
217-
218-
if err := dest.Chmod(fi.Mode()); err != nil {
219-
log.Fatalf("Unable to chmod temp file: %s\n", err)
220-
}
221-
222-
chown(dest, fi)
223-
224-
oldContents, err = os.ReadFile(config.Dest)
225-
if err != nil {
226-
log.Fatalf("Unable to compare current file contents: %s: %s\n", config.Dest, err)
227-
}
193+
oldContents, err := os.ReadFile(config.Dest)
194+
if err != nil && !os.IsNotExist(err) {
195+
log.Fatalf("Unable to compare current file contents: %s: %s\n", config.Dest, err)
228196
}
229197

230198
if !bytes.Equal(oldContents, contents) {
231-
err = os.Rename(dest.Name(), config.Dest)
199+
err := os.WriteFile(config.Dest, contents, 0644)
232200
if err != nil {
233-
log.Fatalf("Unable to create dest file %s: %s\n", config.Dest, err)
201+
log.Fatalf("Unable to write to dest file %s: %s\n", config.Dest, err)
234202
}
203+
235204
log.Printf("Generated '%s' from %d containers", config.Dest, len(filteredContainers))
236205
return true
237206
}

0 commit comments

Comments
 (0)