@@ -204,13 +204,9 @@ func (s *Server) editAgentConfig(c echo.Context) error {
204204
205205 // Extract shebang and version lines if they exist
206206 shebang := ""
207- versionLine := ""
208- currentLines := strings .Split (string (currentContent ), "\n " )
209- for i , line := range currentLines {
207+ for i , line := range strings .Split (string (currentContent ), "\n " ) {
210208 if i == 0 && strings .HasPrefix (line , "#!/" ) {
211- shebang = line + "\n "
212- } else if strings .HasPrefix (line , "version:" ) {
213- versionLine = line + "\n "
209+ shebang = line + "\n \n "
214210 break
215211 }
216212 }
@@ -222,12 +218,15 @@ func (s *Server) editAgentConfig(c echo.Context) error {
222218 return c .JSON (http .StatusInternalServerError , map [string ]string {"error" : "failed to generate merged YAML configuration" })
223219 }
224220
225- // Combine shebang, version, and merged YAML content
226- finalContent := shebang + versionLine
227- if shebang != "" || versionLine != "" {
228- finalContent += "\n "
221+ // Combine shebang and merged YAML content
222+ finalContent := shebang + string (yamlData )
223+
224+ // Make sure the content we are about to write is valid YAML
225+ var tmpConfig latest.Config
226+ if err := yaml .UnmarshalWithOptions ([]byte (finalContent ), & tmpConfig , yaml .Strict ()); err != nil {
227+ slog .Error ("Failed to unmarshal final content" , "error" , err )
228+ return c .JSON (http .StatusInternalServerError , map [string ]string {"error" : "failed to validate YAML content" })
229229 }
230- finalContent += string (yamlData )
231230
232231 // Write the updated configuration back to the file
233232 if err := os .WriteFile (path , []byte (finalContent ), 0o644 ); err != nil {
0 commit comments