@@ -51,10 +51,8 @@ func (y *yamlMerger) Run(
51
51
52
52
// convert validates yamlMerger configuration against a JSON schema and
53
53
// converts it into a builtin.YAMLMergeConfig struct.
54
- func (y * yamlMerger ) convert (
55
- cfg promotion.Config ,
56
- ) (builtin.YAMLMergeConfig , error ) {
57
- return validateAndConvert [builtin.YAMLMergeConfig ](y .schemaLoader , cfg , f .Name ())
54
+ func (y * yamlMerger ) convert (cfg promotion.Config ) (builtin.YAMLMergeConfig , error ) {
55
+ return validateAndConvert [builtin.YAMLMergeConfig ](y .schemaLoader , cfg , y .Name ())
58
56
}
59
57
60
58
// validate validates yamlMerger configuration against a JSON schema.
@@ -71,63 +69,51 @@ func (y *yamlMerger) run(
71
69
result := promotion.StepResult {Status : kargoapi .PromotionStepStatusSucceeded }
72
70
failure := promotion.StepResult {Status : kargoapi .PromotionStepStatusErrored }
73
71
74
- // Keep track of files actually merged.
75
- mergedFiles := make ([]string , 0 , len (cfg .InFiles ))
72
+ // sanity check
73
+ if len (cfg .InFiles ) == 0 || cfg .OutFile == "" {
74
+ return failure , fmt .Errorf ("inFiles and OutFile must not be empty" )
75
+ }
76
76
77
- // Secure join the paths to prevent path traversal attacks.
78
- yamlData := []string {}
77
+ // Secure join the input paths to prevent path traversal attacks.
78
+ filePaths := []string {}
79
79
for _ , path := range cfg .InFiles {
80
80
inFile , err := securejoin .SecureJoin (stepCtx .WorkDir , path )
81
81
if err != nil {
82
- return promotion.StepResult {Status : kargoapi .PromotionStepStatusErrored },
83
- fmt .Errorf ("could not secure join input file %q: %w" , path , err )
82
+ return failure , fmt .Errorf ("could not secure join input file %q: %w" , path , err )
84
83
}
85
84
86
- inBytes , err := os .ReadFile (inFile )
85
+ // only add existing files
86
+ _ , err = os .Stat (inFile )
87
87
if err != nil {
88
- // we skip if file does not exist
89
- if cfg .IgnoreMissingFiles && os .IsNotExist (err ) {
88
+ if cfg .IgnoreMissingFiles {
90
89
continue
91
90
}
92
- return failure , fmt .Errorf (
93
- "error reading file %q: %w" ,
94
- inFile ,
95
- err ,
96
- )
97
- }
91
+ return failure , fmt .Errorf ("input file not found: %s" , inFile )
98
92
99
- // we skip if file is empty
100
- if len (inBytes ) == 0 {
101
- continue
102
93
}
94
+ filePaths = append (filePaths , inFile )
103
95
104
- mergedFiles = append (mergedFiles , path )
105
- yamlData = append (yamlData , string (inBytes ))
106
96
}
107
97
108
- // Merge
109
- outYAML , err := yaml .MergeYAMLFiles (yamlData )
110
- if err != nil {
111
- return promotion.StepResult {Status : kargoapi .PromotionStepStatusErrored },
112
- fmt .Errorf ("could not merge YAML files: %w" , err )
113
- }
114
-
115
- // Write out a single YAML file.
98
+ // Secure join the output path to prevent path traversal attacks.
116
99
outFile , err := securejoin .SecureJoin (stepCtx .WorkDir , cfg .OutFile )
117
100
if err != nil {
118
- return promotion.StepResult {Status : kargoapi .PromotionStepStatusErrored },
119
- fmt .Errorf ("could not secure join outFile %q: %w" , cfg .OutFile , err )
101
+ return failure , fmt .Errorf ("could not secure join outFile %q: %w" , cfg .OutFile , err )
120
102
}
121
103
104
+ // ensure output path fully exist
122
105
if err = os .MkdirAll (filepath .Dir (outFile ), 0o700 ); err != nil {
123
106
return failure , fmt .Errorf ("error creating directory structure %s: %w" , filepath .Dir (outFile ), err )
124
107
}
125
- if err = os .WriteFile (outFile , []byte (outYAML ), 0o600 ); err != nil {
126
- return failure , fmt .Errorf ("error writing to file %s: %w" , outFile , err )
108
+
109
+ // Merge files
110
+ err = yaml .MergeYAMLFiles (filePaths , outFile )
111
+ if err != nil {
112
+ return failure , fmt .Errorf ("could not merge YAML files: %w" , err )
127
113
}
128
114
129
115
// Add a commit message fragment to the step's output.
130
- if commitMsg := y .generateCommitMessage (cfg .OutFile , mergedFiles ); commitMsg != "" {
116
+ if commitMsg := y .generateCommitMessage (cfg .OutFile , filePaths ); commitMsg != "" {
131
117
result .Output = map [string ]any {
132
118
"commitMessage" : commitMsg ,
133
119
}
0 commit comments