Add a --write and --diff flag; require --write to be set before making any changes, for safety

This commit is contained in:
Pim van Pelt
2025-08-28 11:18:59 +02:00
parent 6bc0071bdb
commit d027ec9108
8 changed files with 133 additions and 26 deletions

View File

@@ -8,7 +8,7 @@ import (
"strings"
)
func generateEnv(yamlFile string) {
func generateEnv(yamlFile string, wantDiff bool, allowWrite bool, useColor bool) {
config := loadConfig(yamlFile)
// Check that all local directories exist
@@ -24,7 +24,7 @@ func generateEnv(yamlFile string) {
// Create combined roots.pem file
rootsPemPath := filepath.Join(logEntry.LocalDirectory, "roots.pem")
err := createCombinedRootsPemWithStatus(config.Roots, logEntry.ExtraRoots, rootsPemPath)
err := createCombinedRootsPemWithStatus(config.Roots, logEntry.ExtraRoots, rootsPemPath, wantDiff, allowWrite, useColor)
if err != nil {
log.Fatalf("Failed to create %s: %v", rootsPemPath, err)
}
@@ -53,14 +53,14 @@ func generateEnv(yamlFile string) {
tesseractArgs := strings.Join(args, " ")
envContent := fmt.Sprintf("TESSERACT_ARGS=\"%s\"\nOTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318\n", tesseractArgs)
err = writeFileWithStatus(envPath, []byte(envContent))
err = writeFileWithStatus(envPath, []byte(envContent), wantDiff, allowWrite, useColor)
if err != nil {
log.Fatalf("Failed to write %s: %v", envPath, err)
}
}
}
func createCombinedRootsPemWithStatus(rootsFile, extraRootsFile, outputPath string) error {
func createCombinedRootsPemWithStatus(rootsFile, extraRootsFile, outputPath string, wantDiff bool, allowWrite bool, useColor bool) error {
// Read main roots file
var combinedContent []byte
if rootsFile != "" {
@@ -80,5 +80,5 @@ func createCombinedRootsPemWithStatus(rootsFile, extraRootsFile, outputPath stri
combinedContent = append(combinedContent, extraRootsData...)
}
return writeFileWithStatus(outputPath, combinedContent)
return writeFileWithStatus(outputPath, combinedContent, wantDiff, allowWrite, useColor)
}