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"
"crypto/x509"
"encoding/base64"
"encoding/json"
@@ -8,7 +9,6 @@ import (
"fmt"
"log"
"net/http"
"os"
"strings"
)
@@ -67,15 +67,10 @@ func generateRoots(args []string) {
log.Fatalf("Failed to parse JSON response: %v", err)
}
// Create output file
outFile, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Failed to create output file %s: %v", outputFile, err)
}
defer outFile.Close()
// Write each certificate as PEM
// Collect all valid certificates in a buffer
var pemBuffer bytes.Buffer
validCertCount := 0
for _, certBase64 := range rootsResp.Certificates {
// Decode base64 certificate
certBytes, err := base64.StdEncoding.DecodeString(certBase64)
@@ -102,14 +97,20 @@ func generateRoots(args []string) {
Bytes: certBytes,
}
// Write PEM to file
err = pem.Encode(outFile, pemBlock)
// Write PEM to buffer
err = pem.Encode(&pemBuffer, pemBlock)
if err != nil {
log.Fatalf("Failed to write PEM certificate: %v", err)
log.Fatalf("Failed to encode PEM certificate: %v", err)
}
validCertCount++
}
// Write all certificates to file with status
err = writeFileWithStatus(outputFile, pemBuffer.Bytes())
if err != nil {
log.Fatalf("Failed to write output file %s: %v", outputFile, err)
}
fmt.Printf("Successfully wrote %d certificates to %s (out of %d total)\n", validCertCount, outputFile, len(rootsResp.Certificates))
}