From d2bc823fe8154ef9cf76ecb1a0b29bb4609885fa Mon Sep 17 00:00:00 2001 From: yanxuean Date: Fri, 3 Aug 2018 11:53:48 +0800 Subject: [PATCH] add getTruncatedId function Signed-off-by: yanxuean --- cmd/crictl/container.go | 5 +---- cmd/crictl/image.go | 5 +---- cmd/crictl/sandbox.go | 5 +---- cmd/crictl/stats.go | 6 +----- cmd/crictl/util.go | 8 ++++++++ 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index bf48c61e9b..5e7677930d 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -626,10 +626,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { if !opts.verbose { id := c.Id if !opts.noTrunc { - id = strings.TrimPrefix(c.Id, "") - if len(id) > truncatedIDLen { - id = id[:truncatedIDLen] - } + id = getTruncatedID(id, "") } fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n", id, c.Image.Image, ctm, convertContainerState(c.State), c.Metadata.Name, c.Metadata.Attempt) diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index a7a0ef5d7d..004d27aaab 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -156,10 +156,7 @@ var listImageCommand = cli.Command{ size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3) id := image.Id if !noTrunc { - id = strings.TrimPrefix(image.Id, "sha256:") - if len(id) > truncatedIDLen { - id = id[:truncatedIDLen] - } + id = getTruncatedID(id, "sha256:") } for _, repoTagPair := range repoTagPairs { if showDigest { diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index b2e157df1d..85832f2e45 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -453,10 +453,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago" id := pod.Id if !opts.noTrunc { - id = strings.TrimPrefix(pod.Id, "") - if len(id) > truncatedIDLen { - id = id[:truncatedIDLen] - } + id = getTruncatedID(id, "") } fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n", id, ctm, convertPodState(pod.State), pod.Metadata.Name, pod.Metadata.Namespace, pod.Metadata.Attempt) diff --git a/cmd/crictl/stats.go b/cmd/crictl/stats.go index 9f37c9a995..f1fe7bad70 100644 --- a/cmd/crictl/stats.go +++ b/cmd/crictl/stats.go @@ -20,7 +20,6 @@ import ( "fmt" "os" "sort" - "strings" "text/tabwriter" "time" @@ -163,10 +162,7 @@ func ContainerStats(client pb.RuntimeServiceClient, opts statsOptions) error { // Use `+` to work around go vet bug fmt.Fprintln(w, "CONTAINER\tCPU %"+"\tMEM\tDISK\tINODES") for _, s := range r.GetStats() { - id := strings.TrimPrefix(s.Attributes.Id, "") - if len(id) > truncatedIDLen { - id = id[:truncatedIDLen] - } + id := getTruncatedID(s.Attributes.Id, "") cpu := s.GetCpu().GetUsageCoreNanoSeconds().GetValue() mem := s.GetMemory().GetWorkingSetBytes().GetValue() disk := s.GetWritableLayer().GetUsedBytes().GetValue() diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index a58685cfa1..bea06d4c04 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -294,3 +294,11 @@ func jsonFieldFromTag(tag reflect.StructTag) string { } return field } + +func getTruncatedID(id, prefix string) string { + id = strings.TrimPrefix(id, prefix) + if len(id) > truncatedIDLen { + id = id[:truncatedIDLen] + } + return id +}