MCPcopy
hub / github.com/keploy/keploy / CreateConfig

Method CreateConfig

pkg/service/tools/tools.go:274–336  ·  view source on GitHub ↗
(_ context.Context, filePath string, configData string)

Source from the content-addressed store, hash-verified

272}
273
274func (t *Tools) CreateConfig(_ context.Context, filePath string, configData string) error {
275 var node yamlLib.Node
276 var data []byte
277 var err error
278
279 if configData != "" {
280 data = []byte(configData)
281 } else {
282 configData, err = config.Merge(config.InternalConfig, config.GetDefaultConfig())
283 if err != nil {
284 utils.LogError(t.logger, err, "failed to create default config string")
285 return nil
286 }
287 data = []byte(configData)
288 }
289
290 if err := yamlLib.Unmarshal(data, &node); err != nil {
291 utils.LogError(t.logger, err, "failed to unmarshal the config")
292 return nil
293 }
294
295 if len(node.Content) > 0 { // we don't need agent config in the config file. All the config of the agent will be managed internally
296 rootContent := node.Content[0].Content
297 for i := 0; i < len(rootContent)-1; i += 2 {
298 keyNode := rootContent[i]
299 if keyNode.Value == "agent" {
300 node.Content[0].Content = append(rootContent[:i], rootContent[i+2:]...)
301 break
302 }
303 }
304 }
305 results, err := yamlLib.Marshal(node.Content[0])
306 if err != nil {
307 utils.LogError(t.logger, err, "failed to marshal the config")
308 return nil
309 }
310
311 finalOutput := append(results, []byte(utils.ConfigGuide)...)
312 finalOutput = append([]byte(utils.GetVersionAsComment()), finalOutput...)
313
314 err = os.WriteFile(filePath, finalOutput, fs.ModePerm)
315 if err != nil {
316 // Return the error so callers (cli/config.go handler,
317 // CmdConfigurator.CreateConfigFile) don't falsely claim
318 // "Config file generated successfully" — they each check
319 // the error already. Prior behavior of returning nil
320 // here was a latent lie that let CI scripts see a
321 // "success" line plus an ERROR line for the same op.
322 utils.LogError(t.logger, err, "failed to write config file",
323 zap.String("path", filePath),
324 zap.String("next_step", "verify the directory exists and the user running keploy has write permission; remove any read-only keploy.yml left over from a prior run (e.g. via sudo chown or rm) before re-invoking `keploy config --generate`"),
325 )
326 return err
327 }
328
329 err = os.Chmod(filePath, 0777) // Set permissions to 777
330 if err != nil {
331 utils.LogError(t.logger, err, "failed to set the permission of config file")

Callers 1

CreateConfigFileMethod · 0.95

Calls 7

MergeFunction · 0.92
GetDefaultConfigFunction · 0.92
LogErrorFunction · 0.92
GetVersionAsCommentFunction · 0.92
UnmarshalMethod · 0.80
MarshalMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected