ecCommandIsRunWith launches the ec command line with provided parameters. If parameters contain references to variables in ${...} syntax those will be substituted with the values appropriate for this scenario execution
(ctx context.Context, parameters string)
| 143 | // If parameters contain references to variables in ${...} syntax those will |
| 144 | // be substituted with the values appropriate for this scenario execution |
| 145 | func ecCommandIsRunWith(ctx context.Context, parameters string) (context.Context, error) { |
| 146 | // path to the ec* binary given specific operating system and archive as built by |
| 147 | // make build |
| 148 | ec := path.Join("dist", fmt.Sprintf("ec_%s_%s", runtime.GOOS, runtime.GOARCH)) |
| 149 | info, err := os.Stat(ec) |
| 150 | if err != nil { |
| 151 | if errors.Is(err, os.ErrNotExist) { |
| 152 | return ctx, fmt.Errorf("%s does not exist, run a build (`make build`) first", ec) |
| 153 | } |
| 154 | |
| 155 | return ctx, err |
| 156 | } |
| 157 | |
| 158 | if !info.Mode().IsRegular() { |
| 159 | return ctx, fmt.Errorf("%s is a not a regular file", ec) |
| 160 | } |
| 161 | |
| 162 | ctx, environment, vars, err := variables(ctx) |
| 163 | if err != nil { |
| 164 | return ctx, err |
| 165 | } |
| 166 | |
| 167 | // performs the actual substitution of ${...} with the |
| 168 | // values from `vars`` |
| 169 | args := os.Expand(parameters, func(key string) string { |
| 170 | return vars[key] |
| 171 | }) |
| 172 | |
| 173 | cmd := exec.Command(ec) |
| 174 | // note, argument at 0 is the path to ec command line |
| 175 | cmd.Args = append([]string{ec}, strings.Split(args, " ")...) |
| 176 | cmd.Env = environment |
| 177 | |
| 178 | // capture stdout and stderr |
| 179 | var stdout bytes.Buffer |
| 180 | var stderr bytes.Buffer |
| 181 | cmd.Stdout = &stdout |
| 182 | cmd.Stderr = &stderr |
| 183 | |
| 184 | sts := status{Cmd: cmd, vars: vars, err: err, stdout: &stdout, stderr: &stderr} |
| 185 | |
| 186 | err = cmd.Run() |
| 187 | if err != nil { |
| 188 | // we're not asserting here, so let's log the error |
| 189 | // depending on how we assert the error might or |
| 190 | // might not surface again |
| 191 | var logger log.Logger |
| 192 | logger, ctx = log.LoggerFor(ctx) |
| 193 | logger.Log(err) |
| 194 | } |
| 195 | |
| 196 | // store the outcome in the Context |
| 197 | return context.WithValue(ctx, processStatusKey, &sts), nil |
| 198 | } |
| 199 | |
| 200 | func setupKeys(ctx context.Context, vars map[string]string, environment []string) ([]string, map[string]string, error) { |
| 201 | // there could be several key pairs created, for testing |