Skip to content

Commit 0405379

Browse files
committed
fix: replace stream copy with placeholder replacement for non-template files
## What Non-template files copied during plugin generation now have __SERVICE_NAME__ and __ORGANIZATION__ placeholders replaced with actual values from the catalog data. The stream-based copy was replaced with a read-replace-write approach and the unused io import was removed. ## Why The .goreleaser template in plugin-generator-templates uses goreleaser's own template syntax (e.g. {{ title .Os }}) which conflicts with Go's text/template parser. By switching non-template files to placeholder-based substitution, files with their own template syntax can coexist without escaping gymnastics. ## Notes - The companion change in plugin-generator-templates renames .goreleaser.tmpl to .goreleaser.yaml with __SERVICE_NAME__ placeholder - Any non-template file in the templates repo can now use __SERVICE_NAME__ and __ORGANIZATION__ placeholders without being a .tmpl file - The logger parameter on copyNonTemplateFile is now unused but retained for signature consistency; could be cleaned up later Signed-off-by: jmeridth <jmeridth@gmail.com>
1 parent f8f5e6e commit 0405379

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

command/generate-plugin.go

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io"
98
"net/url"
109
"os"
1110
"path/filepath"
@@ -164,9 +163,9 @@ func generateFileFromTemplate(data CatalogData, templatePath, templatesDir, outp
164163
return fmt.Errorf("error calculating relative path for %s: %w", templatePath, err)
165164
}
166165

167-
// If the file is not a template, copy it over as-is (preserve mode)
166+
// If the file is not a template, copy it over with placeholder replacement
168167
if filepath.Ext(templatePath) != ".tmpl" {
169-
return copyNonTemplateFile(templatePath, relativeFilepath, outputDir, logger)
168+
return copyNonTemplateFile(data, templatePath, relativeFilepath, outputDir, logger)
170169
}
171170

172171
tmpl, err := template.New("plugin").Funcs(template.FuncMap{
@@ -282,39 +281,29 @@ func resolveSourcePath(sourcePath string) (string, error) {
282281
return sourcePath, nil
283282
}
284283

285-
func copyNonTemplateFile(templatePath, relativeFilepath, outputDir string, logger hclog.Logger) error {
284+
func copyNonTemplateFile(data CatalogData, templatePath, relativeFilepath, outputDir string, logger hclog.Logger) error {
286285
outputPath := filepath.Join(outputDir, relativeFilepath)
287286
if err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm); err != nil {
288287
return fmt.Errorf("error creating directories for %s: %w", outputPath, err)
289288
}
290289

291-
// Copy file contents
292-
srcFile, err := os.Open(templatePath)
290+
content, err := os.ReadFile(templatePath)
293291
if err != nil {
294-
return fmt.Errorf("error opening source file %s: %w", templatePath, err)
292+
return fmt.Errorf("error reading source file %s: %w", templatePath, err)
295293
}
296-
defer func() {
297-
err := srcFile.Close()
298-
if err != nil {
299-
logger.Error("error closing output file %s: %w", templatePath, err)
300-
}
301-
}()
302-
303-
dstFile, err := os.Create(outputPath)
304-
if err != nil {
305-
return fmt.Errorf("error creating destination file %s: %w", outputPath, err)
306-
}
307-
defer func() {
308-
_ = dstFile.Close()
309-
}()
310294

311-
if _, err := io.Copy(dstFile, srcFile); err != nil {
312-
return fmt.Errorf("error copying file to %s: %w", outputPath, err)
313-
}
295+
// Replace placeholders in non-template files
296+
output := strings.ReplaceAll(string(content), "__SERVICE_NAME__", data.ServiceName)
297+
output = strings.ReplaceAll(output, "__ORGANIZATION__", data.Organization)
314298

315299
// Try to preserve file mode from source
300+
mode := os.FileMode(0644)
316301
if fi, err := os.Stat(templatePath); err == nil {
317-
_ = os.Chmod(outputPath, fi.Mode())
302+
mode = fi.Mode()
303+
}
304+
305+
if err := os.WriteFile(outputPath, []byte(output), mode); err != nil {
306+
return fmt.Errorf("error writing file %s: %w", outputPath, err)
318307
}
319308

320309
return nil

0 commit comments

Comments
 (0)