Skip to content

Commit

Permalink
Added columntype {{.LongPublishedPorts}} (also displays bound IP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescher committed Jul 17, 2023
1 parent 0f9bc42 commit 37582ab
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Available --format keys (extra | do not exist in `docker ps`):
{{.ShortCommand}} Command without arguments
{{.LabelKeys}} All labels assigned to the container (keys only)
{{.ShortPublishedPorts}} Published ports, shorter output than {{.Ports}}
{{.LongPublishedPorts}} Published ports, full output with IP
{{.ExposedPorts}} Exposed ports
{{.PublishedPorts}} Published ports
{{.NotPublishedPorts}} Exposed but not published ports
Expand Down
18 changes: 17 additions & 1 deletion cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

type PSContext struct {
context.Context
Opt Options
Opt Options
Cache map[string]any
}

func (c PSContext) PrintPrimaryOutput(msg string) {
Expand Down Expand Up @@ -156,16 +157,31 @@ func NewContext(opt Options) (*PSContext, error) {
return &PSContext{
Context: context.Background(),
Opt: opt,
Cache: make(map[string]any),
}, nil
}

func NewEarlyContext() *PSContext {
return &PSContext{
Context: context.Background(),
Opt: DefaultCLIOptions(),
Cache: make(map[string]any),
}
}

func (c PSContext) Finish() {
// ...
}

func (c *PSContext) GetIntFromCache(key string, calc func() int) int {
if v1, ok := c.Cache[key]; ok {
if v2, ok := v1.(int); ok {
return v2
}
panic(fmt.Sprintf("Wrong type in cache type(%s) = %T (expected: int)", key, v1))
}

val := calc()
c.Cache[key] = val
return val
}
5 changes: 3 additions & 2 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ 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{{.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{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.LongPublishedPorts}}\\t{{.Networks}}\\t{{.IP}}",
"table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.ShortCommand}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.LongPublishedPorts}}\\t{{.IP}}",
"table {{.ID}}\\t{{.Names}}\\t{{.ImageName}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.State}}\\t{{.Status}}\\t{{.LongPublishedPorts}}\\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}}",
Expand Down
1 change: 1 addition & 0 deletions cmd/dops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func printHelp(ctx *cli.PSContext) {
ctx.PrintPrimaryOutput(" {{.ShortCommand} Command without arguments")
ctx.PrintPrimaryOutput(" {{.LabelKeys} All labels assigned to the container (keys only)")
ctx.PrintPrimaryOutput(" {{.ShortPublishedPorts}} Published ports, shorter output than {{.Ports}}")
ctx.PrintPrimaryOutput(" {{.LongPublishedPorts}} Published ports, full output with IP")
ctx.PrintPrimaryOutput(" {{.ExposedPorts}} Exposed ports")
ctx.PrintPrimaryOutput(" {{.NotPublishedPorts}} Exposed but not published ports")
ctx.PrintPrimaryOutput(" {{.PublishedPorts}} Published ports")
Expand Down
Loading

0 comments on commit 37582ab

Please sign in to comment.