diff --git a/cmd/devcontainerx/devcontainer.go b/cmd/devcontainerx/devcontainer.go index b9f2356..6259991 100644 --- a/cmd/devcontainerx/devcontainer.go +++ b/cmd/devcontainerx/devcontainer.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "os" "sort" @@ -61,6 +62,54 @@ func createListCommand() *cobra.Command { return cmdList } +func createShowCommand() *cobra.Command { + var argDevcontainerName string + cmd := &cobra.Command{ + Use: "show --name ", + Short: "Show devcontainer info", + Long: "Show information about a running dev container", + RunE: func(cmd *cobra.Command, args []string) error { + devcontainers, err := devcontainers.ListDevcontainers() + if err != nil { + return err + } + containerIDOrName := argDevcontainerName + + // Get container ID + for _, devcontainer := range devcontainers { + if devcontainer.ContainerName == containerIDOrName || + devcontainer.DevcontainerName == containerIDOrName || + devcontainer.ContainerID == containerIDOrName { + output, err := json.MarshalIndent(devcontainer, "", "\t") + if err != nil { + return fmt.Errorf("Failed to serialise devcontainer info: %s", err) + } + fmt.Printf("%s\n", output) + return nil + } + } + + return fmt.Errorf("Failed to find a matching (running) dev container for %q", containerIDOrName) + }, + } + cmd.Flags().StringVarP(&argDevcontainerName, "name", "n", "", "name of dev container to exec into") + + _ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + devcontainers, err := devcontainers.ListDevcontainers() + if err != nil { + os.Exit(1) + } + names := []string{} + for _, devcontainer := range devcontainers { + names = append(names, devcontainer.DevcontainerName) + } + sort.Strings(names) + return names, cobra.ShellCompDirectiveNoFileComp + + }) + return cmd +} + func countBooleans(values ...bool) int { count := 0 for _, v := range values { @@ -82,7 +131,7 @@ func createExecCommand() *cobra.Command { Short: "Execute a command in a devcontainer", Long: "Execute a command in a devcontainer, similar to `docker exec`", RunE: func(cmd *cobra.Command, args []string) error { - + fmt.Printf("*** %q\n\n\n", argDevcontainerName) // Default to executing /bin/bash if len(args) == 0 { args = []string{"/bin/bash"} diff --git a/cmd/devcontainerx/main.go b/cmd/devcontainerx/main.go index c865f4c..7bf43fd 100644 --- a/cmd/devcontainerx/main.go +++ b/cmd/devcontainerx/main.go @@ -27,6 +27,7 @@ func main() { rootCmd.AddCommand(createConfigCommand()) rootCmd.AddCommand(createExecCommand()) rootCmd.AddCommand(createListCommand()) + rootCmd.AddCommand(createShowCommand()) rootCmd.AddCommand(createTemplateCommand()) if config.GetExperimentalFeaturesEnabled() { rootCmd.AddCommand(createSnippetCommand()) diff --git a/go.mod b/go.mod index 4f6dcf6..1aed690 100644 --- a/go.mod +++ b/go.mod @@ -6,11 +6,12 @@ require ( github.com/blang/semver v3.5.1+incompatible github.com/bradford-hamilton/dora v0.1.1 github.com/kyoh86/richgo v0.3.12 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect github.com/rhysd/go-github-selfupdate v1.2.2 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.4.0 github.com/stretchr/testify v1.8.2 - golang.org/x/sys v0.6.0 // indirect + golang.org/x/sys v0.8.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index cecd6e8..5856423 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,8 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 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 golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/internal/pkg/devcontainers/dockerutils.go b/internal/pkg/devcontainers/dockerutils.go index 1e17b68..6c4ce20 100644 --- a/internal/pkg/devcontainers/dockerutils.go +++ b/internal/pkg/devcontainers/dockerutils.go @@ -24,10 +24,10 @@ import ( // DevcontainerInfo holds details about a devcontainer type DevcontainerInfo struct { - ContainerID string - ContainerName string - DevcontainerName string - LocalFolderPath string + ContainerID string `json:"containerID"` + ContainerName string `json:"containerName"` + DevcontainerName string `json:"devcontainerName"` + LocalFolderPath string `json:"localFolderPath"` } const (