(opts CommandSpec)
| 135 | } |
| 136 | |
| 137 | func 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 | |
| 157 | func isValidEnvVarName(name string) bool { |
| 158 | validEnvVarName := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`) |
no test coverage detected