writeFileWithStatus() which shows 'Creating' for new, 'Updating' for changed and 'Unchanged' for files that won't change

This commit is contained in:
Pim van Pelt
2025-08-25 11:51:41 +02:00
parent c9c1e81619
commit 38fe915b37
5 changed files with 65 additions and 71 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"bytes"
"fmt"
"net/url"
"os"
@@ -113,22 +114,20 @@ func generateNginx(yamlFile string) {
outputFilename := fmt.Sprintf("%s.conf", hostname)
outputPath := filepath.Join(log.LocalDirectory, outputFilename)
// Create output file
file, err := os.Create(outputPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create nginx config file %s: %v\n", outputPath, err)
continue
}
defer file.Close()
// Execute template
err = tmpl.Execute(file, data)
// Execute template to buffer
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to execute nginx template for %s: %v\n", outputPath, err)
continue
}
fmt.Printf("Generated nginx config: %s\n", outputPath)
// Write file with status
err = writeFileWithStatus(outputPath, buf.Bytes())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write nginx config file %s: %v\n", outputPath, err)
continue
}
}
}