Documentation
¶
Overview ¶
cmd/product/codemod/codemod.go
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var CodemodCmd = &cobra.Command{ Use: "codemod [--script <file.json>]", Example: ` contextvibes product codemod # Looks for codemod.json contextvibes product codemod --script ./my_refactor_script.json`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr()) scriptToLoad := codemodScriptPath if scriptToLoad == "" { scriptToLoad = "codemod.json" } scriptData, err := os.ReadFile(scriptToLoad) if err != nil { return fmt.Errorf("failed to read codemod script '%s': %w", scriptToLoad, err) } var script codemod.ChangeScript if err := json.Unmarshal(scriptData, &script); err != nil { return fmt.Errorf("failed to parse codemod script JSON: %w", err) } for _, fileChangeSet := range script { presenter.Header("Processing target: %s", fileChangeSet.FilePath) contentBytes, err := os.ReadFile(fileChangeSet.FilePath) if err != nil && !os.IsNotExist(err) { return err } currentContent := string(contentBytes) for _, op := range fileChangeSet.Operations { switch op.Type { case "regex_replace": re, err := regexp.Compile(op.FindRegex) if err != nil { return err } currentContent = re.ReplaceAllString(currentContent, op.ReplaceWith) } } if !globals.AssumeYes { confirmed, err := presenter.PromptForConfirmation( fmt.Sprintf("Write changes to %s?", fileChangeSet.FilePath), ) if err != nil || !confirmed { continue } } if err := os.WriteFile(fileChangeSet.FilePath, []byte(currentContent), 0o600); err != nil { return err } globals.AppLogger.Info("Applied codemod", "file", fileChangeSet.FilePath) } return nil }, }
CodemodCmd represents the codemod command.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.