Skip to content

Commit

Permalink
Merge pull request #76 from stuartleeks/show-command
Browse files Browse the repository at this point in the history
Add show command
  • Loading branch information
stuartleeks authored May 17, 2023
2 parents b4e7b43 + f03e864 commit 77f121c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
51 changes: 50 additions & 1 deletion cmd/devcontainerx/devcontainer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -61,6 +62,54 @@ func createListCommand() *cobra.Command {
return cmdList
}

func createShowCommand() *cobra.Command {
var argDevcontainerName string
cmd := &cobra.Command{
Use: "show --name <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 {
Expand All @@ -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"}
Expand Down
1 change: 1 addition & 0 deletions cmd/devcontainerx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/devcontainers/dockerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 77f121c

Please sign in to comment.