From 614332a185b3a027c7c7ca8aa87d863a845fea57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 12 Jan 2023 00:09:23 +0100 Subject: [PATCH] do not show exposed-only ports by default --- README.md | 5 ++- cli/options.go | 12 ++--- cmd/dops/main.go | 5 ++- consts/version.go | 2 +- impl/columns.go | 111 +++++++++++++++++++++++++++++++++++----------- 5 files changed, 99 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 0198d1e..413896f 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ Available --format keys (default): {{.Command}} Quoted command {{.CreatedAt}} Time when the container was created. {{.RunningFor}} Elapsed time since the container was started. - {{.Ports}} Exposed ports. + {{.Ports}} Published ports. ([!] differs from docker CLI, these are only the published ports) {{.State}} Container status {{.Status}} Container status with details {{.Size}} Container disk size. @@ -165,5 +165,8 @@ Available --format keys (extra | do not exist in `docker ps`): {{.ImageRegistry}}, {{.Registry}} Image Registry {{.ShortCommand}} Command without arguments {{.LabelKeys}} All labels assigned to the container (keys only) + {{.ExposedPorts}} Exposed ports + {{.PublishedPorts}} Published ports + {{.NotPublishedPorts}} Exposed but not published ports {{.IP}} Internal IP Address ~~~~~~ diff --git a/cli/options.go b/cli/options.go index 4198fb2..5ffd917 100644 --- a/cli/options.go +++ b/cli/options.go @@ -44,12 +44,12 @@ func DefaultCLIOptions() Options { Limit: -1, DefaultFormat: true, Format: []string{ - "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}\\t{{.Networks}}\\t{{.IP}}", - "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}\\t{{.IP}}", - "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}\\t{{.IP}}", - "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}", - "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}", - "table {{.ID}}\\t{{.Names}}\\t{{.Tag}}\\t{{.State}}\\t{{.Status}}\\t{{.Ports}}", + "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}\\t{{.Networks}}\\t{{.IP}}", + "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}\\t{{.IP}}", + "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}\\t{{.IP}}", + "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}", + "table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}", + "table {{.ID}}\\t{{.Names}}\\t{{.Tag}}\\t{{.State}}\\t{{.Status}}\\t{{.PublishedPorts}}", "table {{.ID}}\\t{{.Names}}\\t{{.Tag}}\\t{{.State}}\\t{{.Status}}", "table {{.ID}}\\t{{.Names}}\\t{{.State}}\\t{{.Status}}", "table {{.ID}}\\t{{.Names}}\\t{{.State}}", diff --git a/cmd/dops/main.go b/cmd/dops/main.go index b1f5af0..d02e56e 100644 --- a/cmd/dops/main.go +++ b/cmd/dops/main.go @@ -94,7 +94,7 @@ func printHelp(ctx *cli.PSContext) { ctx.PrintPrimaryOutput(" {{.Command}} Quoted command") ctx.PrintPrimaryOutput(" {{.CreatedAt}} Time when the container was created.") ctx.PrintPrimaryOutput(" {{.RunningFor}} Elapsed time since the container was started.") - ctx.PrintPrimaryOutput(" {{.Ports}} Exposed ports.") + ctx.PrintPrimaryOutput(" {{.Ports}} Published ports. ([!] differs from docker CLI, these are only the published ports)") ctx.PrintPrimaryOutput(" {{.State}} Container status") ctx.PrintPrimaryOutput(" {{.Status}} Container status with details") ctx.PrintPrimaryOutput(" {{.Size}} Container disk size.") @@ -110,6 +110,9 @@ func printHelp(ctx *cli.PSContext) { ctx.PrintPrimaryOutput(" {{.ImageRegistry}, {{.Registry} Image Registry") ctx.PrintPrimaryOutput(" {{.ShortCommand} Command without arguments") ctx.PrintPrimaryOutput(" {{.LabelKeys} All labels assigned to the container (keys only)") + ctx.PrintPrimaryOutput(" {{.ExposedPorts}} Exposed ports") + ctx.PrintPrimaryOutput(" {{.NotPublishedPorts}} Exposed but not published ports") + ctx.PrintPrimaryOutput(" {{.PublishedPorts}} Published ports") ctx.PrintPrimaryOutput(" {{.IP} Internal IP Address") ctx.PrintPrimaryOutput("") } diff --git a/consts/version.go b/consts/version.go index f162e76..a859c46 100644 --- a/consts/version.go +++ b/consts/version.go @@ -2,4 +2,4 @@ package consts //go:generate /bin/bash version.sh -const BETTER_DOCKER_PS_VERSION = "1.6" +const BETTER_DOCKER_PS_VERSION = "1.7" diff --git a/impl/columns.go b/impl/columns.go index 1ad8c8f..5096a93 100644 --- a/impl/columns.go +++ b/impl/columns.go @@ -14,27 +14,30 @@ import ( ) var ColumnMap = map[string]printer.ColFun{ - "ID": ColContainerID, - "Image": ColFullImage, - "ImageName": ColImage, - "ImageTag": ColImageTag, - "Registry": ColRegistry, - "ImageRegistry": ColRegistry, - "Tag": ColImageTag, - "Command": ColCommand, - "ShortCommand": ColShortCommand, - "CreatedAt": ColCreatedAt, - "RunningFor": ColRunningFor, - "Ports": ColPorts, - "State": ColState, - "Status": ColStatus, - "Size": ColSize, - "Names": ColName, - "Labels": ColLabels, - "LabelKeys": ColLabelKeys, - "Mounts": ColMounts, - "Networks": ColNetworks, - "IP": ColIP, + "ID": ColContainerID, + "Image": ColFullImage, + "ImageName": ColImage, + "ImageTag": ColImageTag, + "Registry": ColRegistry, + "ImageRegistry": ColRegistry, + "Tag": ColImageTag, + "Command": ColCommand, + "ShortCommand": ColShortCommand, + "CreatedAt": ColCreatedAt, + "RunningFor": ColRunningFor, + "Ports": ColPortsPublished, + "PublishedPorts": ColPortsPublished, + "ExposedPorts": ColPortsExposed, + "NotPublishedPorts": ColPortsNotPublished, + "State": ColState, + "Status": ColStatus, + "Size": ColSize, + "Names": ColName, + "Labels": ColLabels, + "LabelKeys": ColLabelKeys, + "Mounts": ColMounts, + "Networks": ColNetworks, + "IP": ColIP, } func ColContainerID(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { @@ -171,24 +174,78 @@ func ColStatus(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { return []string{statusColor(cont.Status, cont.Status)} } -func ColPorts(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { +func ColPortsExposed(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { if cont == nil { - return []string{"PORTS"} + return []string{"EXPOSED PORTS"} } - r := make(map[string]bool) + m := make(map[string]bool) + r := make([]string, 0) for _, port := range cont.Ports { p1 := langext.StrPadLeft(strconv.Itoa(port.PublicPort), " ", 5) p2 := langext.StrPadLeft(strconv.Itoa(port.PrivatePort), " ", 5) if port.PublicPort == 0 { - r[fmt.Sprintf(" %s / %s", p2, port.Type)] = true + str := fmt.Sprintf(" %s / %s", p2, port.Type) + if _, ok := m[str]; !ok { + m[str] = true + r = append(r, str) + } } else { - r[fmt.Sprintf("%s -> %s / %s", p1, p2, port.Type)] = true + str := fmt.Sprintf("%s -> %s / %s", p1, p2, port.Type) + if _, ok := m[str]; !ok { + m[str] = true + r = append(r, str) + } + } + } + + return r +} + +func ColPortsPublished(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { + if cont == nil { + return []string{"PUBLISHED PORTS"} + } + + m := make(map[string]bool) + r := make([]string, 0) + for _, port := range cont.Ports { + p1 := langext.StrPadLeft(strconv.Itoa(port.PublicPort), " ", 5) + p2 := langext.StrPadLeft(strconv.Itoa(port.PrivatePort), " ", 5) + + if port.PublicPort != 0 { + str := fmt.Sprintf("%s -> %s / %s", p1, p2, port.Type) + if _, ok := m[str]; !ok { + m[str] = true + r = append(r, str) + } } } - return langext.MapKeyArr(r) + return r +} + +func ColPortsNotPublished(ctx *cli.PSContext, cont *docker.ContainerSchema) []string { + if cont == nil { + return []string{"NOT PUBLISHED PORTS"} + } + + m := make(map[string]bool) + r := make([]string, 0) + for _, port := range cont.Ports { + p2 := langext.StrPadLeft(strconv.Itoa(port.PrivatePort), " ", 5) + + if port.PublicPort == 0 { + str := fmt.Sprintf("%s / %s", p2, port.Type) + if _, ok := m[str]; !ok { + m[str] = true + r = append(r, str) + } + } + } + + return r } func ColName(ctx *cli.PSContext, cont *docker.ContainerSchema) []string {