devcontainerEnv is a helper function that inspects the container labels to find the required environment variables for running a command in the container.
(ctx context.Context, execer agentexec.Execer, container string)
| 152 | // devcontainerEnv is a helper function that inspects the container labels to |
| 153 | // find the required environment variables for running a command in the container. |
| 154 | func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container string) ([]string, error) { |
| 155 | stdout, stderr, err := runDockerInspect(ctx, execer, container) |
| 156 | if err != nil { |
| 157 | return nil, xerrors.Errorf("inspect container: %w: %q", err, stderr) |
| 158 | } |
| 159 | |
| 160 | ins, _, err := convertDockerInspect(stdout) |
| 161 | if err != nil { |
| 162 | return nil, xerrors.Errorf("inspect container: %w", err) |
| 163 | } |
| 164 | |
| 165 | if len(ins) != 1 { |
| 166 | return nil, xerrors.Errorf("inspect container: expected 1 container, got %d", len(ins)) |
| 167 | } |
| 168 | |
| 169 | in := ins[0] |
| 170 | if in.Labels == nil { |
| 171 | return nil, nil |
| 172 | } |
| 173 | |
| 174 | // We want to look for the devcontainer metadata, which is in the |
| 175 | // value of the label `devcontainer.metadata`. |
| 176 | rawMeta, ok := in.Labels["devcontainer.metadata"] |
| 177 | if !ok { |
| 178 | return nil, nil |
| 179 | } |
| 180 | |
| 181 | meta := make([]dcspec.DevContainer, 0) |
| 182 | if err := json.Unmarshal([]byte(rawMeta), &meta); err != nil { |
| 183 | return nil, xerrors.Errorf("unmarshal devcontainer.metadata: %w", err) |
| 184 | } |
| 185 | |
| 186 | // The environment variables are stored in the `remoteEnv` key. |
| 187 | env := make([]string, 0) |
| 188 | for _, m := range meta { |
| 189 | for k, v := range m.RemoteEnv { |
| 190 | if v == nil { // *string per spec |
| 191 | // devcontainer-cli will set this to the string "null" if the value is |
| 192 | // not set. Explicitly setting to an empty string here as this would be |
| 193 | // more expected here. |
| 194 | v = ptr.Ref("") |
| 195 | } |
| 196 | env = append(env, fmt.Sprintf("%s=%s", k, *v)) |
| 197 | } |
| 198 | } |
| 199 | slices.Sort(env) |
| 200 | return env, nil |
| 201 | } |
| 202 | |
| 203 | // wrapDockerExec is a helper function that wraps the given command and arguments |
| 204 | // with a docker exec command that runs as the given user in the given |
no test coverage detected