Skip to content

Commit

Permalink
do not show exposed-only ports by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescher committed Jan 11, 2023
1 parent 65afa9d commit 614332a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 36 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
~~~~~~
12 changes: 6 additions & 6 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand Down
5 changes: 4 additions & 1 deletion cmd/dops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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("")
}
2 changes: 1 addition & 1 deletion consts/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
111 changes: 84 additions & 27 deletions impl/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 614332a

Please sign in to comment.