Skip to content

Commit 77f121c

Browse files
authored
Merge pull request #76 from stuartleeks/show-command
Add show command
2 parents b4e7b43 + f03e864 commit 77f121c

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

cmd/devcontainerx/devcontainer.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"sort"
@@ -61,6 +62,54 @@ func createListCommand() *cobra.Command {
6162
return cmdList
6263
}
6364

65+
func createShowCommand() *cobra.Command {
66+
var argDevcontainerName string
67+
cmd := &cobra.Command{
68+
Use: "show --name <name>",
69+
Short: "Show devcontainer info",
70+
Long: "Show information about a running dev container",
71+
RunE: func(cmd *cobra.Command, args []string) error {
72+
devcontainers, err := devcontainers.ListDevcontainers()
73+
if err != nil {
74+
return err
75+
}
76+
containerIDOrName := argDevcontainerName
77+
78+
// Get container ID
79+
for _, devcontainer := range devcontainers {
80+
if devcontainer.ContainerName == containerIDOrName ||
81+
devcontainer.DevcontainerName == containerIDOrName ||
82+
devcontainer.ContainerID == containerIDOrName {
83+
output, err := json.MarshalIndent(devcontainer, "", "\t")
84+
if err != nil {
85+
return fmt.Errorf("Failed to serialise devcontainer info: %s", err)
86+
}
87+
fmt.Printf("%s\n", output)
88+
return nil
89+
}
90+
}
91+
92+
return fmt.Errorf("Failed to find a matching (running) dev container for %q", containerIDOrName)
93+
},
94+
}
95+
cmd.Flags().StringVarP(&argDevcontainerName, "name", "n", "", "name of dev container to exec into")
96+
97+
_ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
98+
devcontainers, err := devcontainers.ListDevcontainers()
99+
if err != nil {
100+
os.Exit(1)
101+
}
102+
names := []string{}
103+
for _, devcontainer := range devcontainers {
104+
names = append(names, devcontainer.DevcontainerName)
105+
}
106+
sort.Strings(names)
107+
return names, cobra.ShellCompDirectiveNoFileComp
108+
109+
})
110+
return cmd
111+
}
112+
64113
func countBooleans(values ...bool) int {
65114
count := 0
66115
for _, v := range values {
@@ -82,7 +131,7 @@ func createExecCommand() *cobra.Command {
82131
Short: "Execute a command in a devcontainer",
83132
Long: "Execute a command in a devcontainer, similar to `docker exec`",
84133
RunE: func(cmd *cobra.Command, args []string) error {
85-
134+
fmt.Printf("*** %q\n\n\n", argDevcontainerName)
86135
// Default to executing /bin/bash
87136
if len(args) == 0 {
88137
args = []string{"/bin/bash"}

cmd/devcontainerx/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func main() {
2727
rootCmd.AddCommand(createConfigCommand())
2828
rootCmd.AddCommand(createExecCommand())
2929
rootCmd.AddCommand(createListCommand())
30+
rootCmd.AddCommand(createShowCommand())
3031
rootCmd.AddCommand(createTemplateCommand())
3132
if config.GetExperimentalFeaturesEnabled() {
3233
rootCmd.AddCommand(createSnippetCommand())

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ require (
66
github.com/blang/semver v3.5.1+incompatible
77
github.com/bradford-hamilton/dora v0.1.1
88
github.com/kyoh86/richgo v0.3.12 // indirect
9+
github.com/mattn/go-isatty v0.0.18 // indirect
910
github.com/rhysd/go-github-selfupdate v1.2.2
1011
github.com/spf13/cobra v1.0.0
1112
github.com/spf13/viper v1.4.0
1213
github.com/stretchr/testify v1.8.2
13-
golang.org/x/sys v0.6.0 // indirect
14+
golang.org/x/sys v0.8.0 // indirect
1415
gopkg.in/yaml.v2 v2.4.0 // indirect
1516
)
1617

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK
8080
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8181
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
8282
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
83+
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
84+
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8385
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
8486
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
8587
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
@@ -190,6 +192,8 @@ golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBc
190192
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
191193
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
192194
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
195+
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
196+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
193197
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
194198
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
195199
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/pkg/devcontainers/dockerutils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424

2525
// DevcontainerInfo holds details about a devcontainer
2626
type DevcontainerInfo struct {
27-
ContainerID string
28-
ContainerName string
29-
DevcontainerName string
30-
LocalFolderPath string
27+
ContainerID string `json:"containerID"`
28+
ContainerName string `json:"containerName"`
29+
DevcontainerName string `json:"devcontainerName"`
30+
LocalFolderPath string `json:"localFolderPath"`
3131
}
3232

3333
const (

0 commit comments

Comments
 (0)