MCPcopy Create free account
hub / github.com/github/gh-aw / getBashSingleQuotedArgsCodemod

Function getBashSingleQuotedArgsCodemod

pkg/cli/codemod_bash_single_quoted_args.go:18–92  ·  view source on GitHub ↗

getBashSingleQuotedArgsCodemod rewrites tools.bash entries that contain single-quoted shell arguments into equivalent double-quoted forms so Copilot shell allow-tool generation does not truncate them to a prefix.

()

Source from the content-addressed store, hash-verified

16// single-quoted shell arguments into equivalent double-quoted forms so Copilot
17// shell allow-tool generation does not truncate them to a prefix.
18func getBashSingleQuotedArgsCodemod() Codemod {
19 return Codemod{
20 ID: "bash-single-quoted-args-rewrite",
21 Name: "Rewrite single-quoted bash tool args",
22 Description: "Rewrites tools.bash entries like grep -n 'foo' to grep -n \"foo\" when safe, reducing Copilot shell() truncation warnings.",
23 IntroducedIn: "0.39.0",
24 Apply: func(content string, frontmatter map[string]any) (string, bool, error) {
25 toolsValue, hasTools := frontmatter["tools"]
26 if !hasTools {
27 return content, false, nil
28 }
29
30 toolsMap, ok := toolsValue.(map[string]any)
31 if !ok {
32 return content, false, nil
33 }
34
35 bashValue, hasBash := toolsMap["bash"]
36 if !hasBash {
37 return content, false, nil
38 }
39
40 bashCommands, ok := bashValue.([]any)
41 if !ok {
42 return content, false, nil
43 }
44
45 updated := make([]any, len(bashCommands))
46 copy(updated, bashCommands)
47
48 changed := false
49 var unsafeCommands []string
50 for i, cmd := range bashCommands {
51 cmdStr, ok := cmd.(string)
52 if !ok {
53 continue
54 }
55
56 rewritten, safe, rewrittenChanged := rewriteSingleQuotedBashArgs(cmdStr)
57 if !safe {
58 unsafeCommands = append(unsafeCommands, cmdStr)
59 continue
60 }
61 if rewrittenChanged {
62 updated[i] = rewritten
63 changed = true
64 }
65 }
66
67 for _, cmd := range unsafeCommands {
68 fmt.Fprintln(os.Stderr, console.FormatWarningMessage(
69 fmt.Sprintf("tools.bash entry %q contains an unclosed single-quoted segment and could not be safely rewritten; left unchanged", cmd)))
70 }
71
72 if !changed {
73 return content, false, nil
74 }
75

Calls 6

FormatWarningMessageFunction · 0.92
PrintMethod · 0.80
ErrorfMethod · 0.45