46 lines
868 B
Go
46 lines
868 B
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/client"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var dockerCmd = &cobra.Command{
|
|
Use: "docker",
|
|
Short: "Access to docker client",
|
|
}
|
|
|
|
var dockerListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "list out all docker images",
|
|
Long: "List out all docker images available to us",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
dockerList()
|
|
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(dockerCmd)
|
|
dockerCmd.AddCommand(dockerListCmd)
|
|
}
|
|
|
|
func dockerList() {
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, container := range containers {
|
|
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
|
}
|
|
}
|