Skip to content

Commit 3fad895

Browse files
committed
Shorten SHA256 image references (if not --no-trunc is specified)
1 parent 7dbbd2a commit 3fad895

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ _out/*
4040
input.test
4141
input1.test
4242
input2.test
43+
input3.test
4344
input.json
4445
input1.json
4546
input2.json

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Options (default):
134134
--format <fmt> Pretty-print containers using a Go template
135135
--last , -n Show n last created containers (includes all states)
136136
--latest , -l Show the latest created container (includes all states)
137-
--no-trunc Don't truncate output
137+
--no-trunc Don't truncate output (eg ContainerIDs, Sha256 Image references, commandline)
138138
--quiet , -q Only display container IDs
139139
--size , -s Display total file sizes
140140

cmd/dops/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func printHelp(ctx *cli.PSContext) {
9090
ctx.PrintPrimaryOutput(" --format <fmt> Pretty-print containers using a Go template")
9191
ctx.PrintPrimaryOutput(" --last , -n Show n last created containers (includes all states)")
9292
ctx.PrintPrimaryOutput(" --latest , -l Show the latest created container (includes all states)")
93-
ctx.PrintPrimaryOutput(" --no-trunc Don't truncate output")
93+
ctx.PrintPrimaryOutput(" --no-trunc Don't truncate output (eg ContainerIDs, Sha256 Image references, commandline)")
9494
ctx.PrintPrimaryOutput(" --quiet , -q Only display container IDs")
9595
ctx.PrintPrimaryOutput(" --size , -s Display total file sizes")
9696
ctx.PrintPrimaryOutput("")

docker/util.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package docker
22

3-
import "strings"
3+
import (
4+
"better-docker-ps/cli"
5+
"strings"
6+
)
47

58
var registryPrefixList = []string{
69
".com",
@@ -10,7 +13,7 @@ var registryPrefixList = []string{
1013
".org",
1114
}
1215

13-
func SplitDockerImage(img string) (string, string, string) {
16+
func SplitDockerImage(ctx *cli.PSContext, img string) (string, string, string) {
1417

1518
resultRegistry := ""
1619
resultImage := ""
@@ -42,6 +45,11 @@ func SplitDockerImage(img string) (string, string, string) {
4245

4346
resultImage = img
4447

48+
if resultImage == "sha256" && len(resultTag) == 64 && ctx.Opt.Truncate {
49+
resultImage = "(sha256)"
50+
resultTag = resultTag[0:12] + "..."
51+
}
52+
4553
return resultRegistry, resultImage, resultTag
4654

4755
}

impl/columns.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ColRegistry(ctx *cli.PSContext, allData []docker.ContainerSchema, cont *doc
8080
return []string{"REGISTRY"}
8181
}
8282

83-
v, _, _ := docker.SplitDockerImage(cont.Image)
83+
v, _, _ := docker.SplitDockerImage(ctx, cont.Image)
8484

8585
return []string{v}
8686
}
@@ -90,7 +90,7 @@ func ColImage(ctx *cli.PSContext, allData []docker.ContainerSchema, cont *docker
9090
return []string{"IMAGE"}
9191
}
9292

93-
_, v, _ := docker.SplitDockerImage(cont.Image)
93+
_, v, _ := docker.SplitDockerImage(ctx, cont.Image)
9494

9595
return []string{v}
9696
}
@@ -100,7 +100,7 @@ func ColImageTag(ctx *cli.PSContext, allData []docker.ContainerSchema, cont *doc
100100
return []string{"TAG"}
101101
}
102102

103-
_, _, v := docker.SplitDockerImage(cont.Image)
103+
_, _, v := docker.SplitDockerImage(ctx, cont.Image)
104104

105105
return []string{v}
106106
}
@@ -523,22 +523,22 @@ func SortFullImage(ctx *cli.PSContext, v1 *docker.ContainerSchema, v2 *docker.Co
523523
}
524524

525525
func SortRegistry(ctx *cli.PSContext, v1 *docker.ContainerSchema, v2 *docker.ContainerSchema) int {
526-
reg1, _, _ := docker.SplitDockerImage(v1.Image)
527-
reg2, _, _ := docker.SplitDockerImage(v2.Image)
526+
reg1, _, _ := docker.SplitDockerImage(ctx, v1.Image)
527+
reg2, _, _ := docker.SplitDockerImage(ctx, v2.Image)
528528

529529
return langext.Compare(reg1, reg2)
530530
}
531531

532532
func SortImage(ctx *cli.PSContext, v1 *docker.ContainerSchema, v2 *docker.ContainerSchema) int {
533-
_, img1, _ := docker.SplitDockerImage(v1.Image)
534-
_, img2, _ := docker.SplitDockerImage(v2.Image)
533+
_, img1, _ := docker.SplitDockerImage(ctx, v1.Image)
534+
_, img2, _ := docker.SplitDockerImage(ctx, v2.Image)
535535

536536
return langext.Compare(img1, img2)
537537
}
538538

539539
func SortImageTag(ctx *cli.PSContext, v1 *docker.ContainerSchema, v2 *docker.ContainerSchema) int {
540-
_, _, tag1 := docker.SplitDockerImage(v1.Image)
541-
_, _, tag2 := docker.SplitDockerImage(v2.Image)
540+
_, _, tag1 := docker.SplitDockerImage(ctx, v1.Image)
541+
_, _, tag2 := docker.SplitDockerImage(ctx, v2.Image)
542542

543543
return langext.Compare(tag1, tag2)
544544
}

0 commit comments

Comments
 (0)