ScriptLines returns shell syntax for executing the commands in the LifecycleScript. TODO: Technically the commands could be executed in parallel, but that would add a bit of complexity to do portably.
()
| 145 | // TODO: Technically the commands could be executed in parallel, but that would |
| 146 | // add a bit of complexity to do portably. |
| 147 | func (s *LifecycleScript) ScriptLines() string { |
| 148 | var lines string |
| 149 | for _, command := range s.shellCommands { |
| 150 | lines += command + "\n" |
| 151 | } |
| 152 | for _, commandAndArgs := range s.nonShellCommands { |
| 153 | // Quote the command arguments to prevent shell interpretation. |
| 154 | quotedCommandAndArgs := make([]string, len(commandAndArgs)) |
| 155 | for i := range commandAndArgs { |
| 156 | // Surround each argument with single quotes. If the |
| 157 | // argument contains any single quotes, they are escaped |
| 158 | // by replacing them with the sequence '"'"'. This |
| 159 | // sequence ends the current single-quoted string, |
| 160 | // starts and immediately ends a double-quoted string |
| 161 | // containing a single quote, and then restarts the |
| 162 | // single-quoted string. This approach works because in |
| 163 | // shell syntax, adjacent strings are concatenated, so |
| 164 | // 'arg'"'"'arg' is interpreted as arg'arg. |
| 165 | quotedCommandAndArgs[i] = "'" + strings.ReplaceAll(commandAndArgs[i], "'", "'\"'\"'") + "'" |
| 166 | } |
| 167 | lines += strings.Join(quotedCommandAndArgs, " ") + "\n" |
| 168 | } |
| 169 | return lines |
| 170 | } |
no test coverage detected