Skip to content

Commit b0d5fa4

Browse files
authored
Merge pull request #16 from stuartleeks/sl/dc-prompt
Add option to be prompted for devcontainer on `devcontainer exec`
2 parents 912f614 + 40cd830 commit b0d5fa4

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
// Add the IDs of extensions you want installed when the container is created.
5050
"extensions": [
51-
"ms-vscode.go"
51+
"golang.go"
5252
]
5353

5454
// Use 'forwardPorts' to make a list of ports inside the container available locally.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ devcontainer exec vscode-remote-test-dockerfile bash
5050
devcontainer exec vscode-remote-test-dockercompose_devcontainer/mongo ls -a /workspaces/vscode-remote-test-dockerfile
5151
```
5252

53+
You can pass `?` as the devcontainer name and the CLI will prompt you to pick a devcontainer to run the `exec` command against, e.g.:
54+
55+
```bash
56+
$ ./devcontainer exec ? bash
57+
Specify the devcontainer to use:
58+
0: devcontainer-cli (festive_saha)
59+
1: vscode-remote-test-dockerfile (fervent_gopher)
60+
0
61+
```
62+
63+
5364
### Working with devcontainer templates
5465

5566
To work with devcontainer templates `devcontainer` needs to know where you have the templates stored.

cmd/devcontainer/devcontainer.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func createExecCommand() *cobra.Command {
6666
cmd := &cobra.Command{
6767
Use: "exec DEVCONTAINER_NAME COMMAND [args...]",
6868
Short: "Execute a command in a devcontainer",
69-
Long: "Execute a command in a devcontainer, similar to `docker exec`.",
69+
Long: "Execute a command in a devcontainer, similar to `docker exec`. Pass `?` as DEVCONTAINER_NAME to be prompted.",
7070
RunE: func(cmd *cobra.Command, args []string) error {
7171

7272
if len(args) < 2 {
@@ -80,14 +80,28 @@ func createExecCommand() *cobra.Command {
8080
}
8181

8282
containerID := ""
83-
for _, devcontainer := range devcontainers {
84-
if devcontainer.ContainerName == devcontainerName || devcontainer.DevcontainerName == devcontainerName {
85-
containerID = devcontainer.ContainerID
86-
break
83+
if devcontainerName == "?" {
84+
// prompt user
85+
fmt.Println("Specify the devcontainer to use:")
86+
for index, devcontainer := range devcontainers {
87+
fmt.Printf("%4d: %s (%s)\n", index, devcontainer.DevcontainerName, devcontainer.ContainerName)
88+
}
89+
selection := -1
90+
_, _ = fmt.Scanf("%d", &selection)
91+
if selection < 0 || selection >= len(devcontainers) {
92+
return fmt.Errorf("Invalid option")
93+
}
94+
containerID = devcontainers[selection].ContainerID
95+
} else {
96+
for _, devcontainer := range devcontainers {
97+
if devcontainer.ContainerName == devcontainerName || devcontainer.DevcontainerName == devcontainerName {
98+
containerID = devcontainer.ContainerID
99+
break
100+
}
101+
}
102+
if containerID == "" {
103+
return cmd.Usage()
87104
}
88-
}
89-
if containerID == "" {
90-
return cmd.Usage()
91105
}
92106

93107
dockerArgs := []string{"exec", "-it", containerID}

0 commit comments

Comments
 (0)