MCPcopy
hub / github.com/wavetermdev/waveterm / BuildShellCommand

Function BuildShellCommand

pkg/genconn/genconn.go:137–155  ·  view source on GitHub ↗
(opts CommandSpec)

Source from the content-addressed store, hash-verified

135}
136
137func BuildShellCommand(opts CommandSpec) (string, error) {
138 // Build environment variables
139 var envVars strings.Builder
140 for key, value := range opts.Env {
141 if !isValidEnvVarName(key) {
142 return "", fmt.Errorf("invalid environment variable name: %q", key)
143 }
144 envVars.WriteString(fmt.Sprintf("%s=%s ", key, shellutil.HardQuote(value)))
145 }
146
147 // Build the command
148 shellCmd := opts.Cmd
149 if opts.Cwd != "" {
150 shellCmd = fmt.Sprintf("cd %s && %s", shellutil.HardQuote(opts.Cwd), shellCmd)
151 }
152
153 // Quote the command for `sh -c`
154 return fmt.Sprintf("sh -c %s", shellutil.HardQuote(envVars.String()+shellCmd)), nil
155}
156
157func isValidEnvVarName(name string) bool {
158 validEnvVarName := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)

Callers 2

MakeWSLProcessControllerFunction · 0.85
StartMethod · 0.85

Calls 4

HardQuoteFunction · 0.92
isValidEnvVarNameFunction · 0.85
WriteStringMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected