From 3825d37923a2b9dc156c762998438df34a474d3c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 31 Oct 2024 13:03:33 +0100 Subject: [PATCH] cli/command: define some consts for repeated values Signed-off-by: Sebastiaan van Stijn --- cli/command/container/formatter_stats.go | 14 ++++++++------ cli/command/node/formatter.go | 3 ++- cli/command/service/formatter.go | 6 ++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index dafcb4303b..b7fdf47333 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -22,6 +22,8 @@ const ( winMemUseHeader = "PRIV WORKING SET" // Used only on Windows memUseHeader = "MEM USAGE / LIMIT" // Used only on Linux pidsHeader = "PIDS" // Used only on Linux + + noValue = "--" ) // StatsEntry represents the statistics data collected from a container @@ -169,7 +171,7 @@ func (c *statsContext) Name() string { if len(c.s.Name) > 1 { return c.s.Name[1:] } - return "--" + return noValue } func (c *statsContext) ID() string { @@ -181,7 +183,7 @@ func (c *statsContext) ID() string { func (c *statsContext) CPUPerc() string { if c.s.IsInvalid { - return "--" + return noValue } return formatPercentage(c.s.CPUPercentage) } @@ -198,28 +200,28 @@ func (c *statsContext) MemUsage() string { func (c *statsContext) MemPerc() string { if c.s.IsInvalid || c.os == winOSType { - return "--" + return noValue } return formatPercentage(c.s.MemoryPercentage) } func (c *statsContext) NetIO() string { if c.s.IsInvalid { - return "--" + return noValue } return units.HumanSizeWithPrecision(c.s.NetworkRx, 3) + " / " + units.HumanSizeWithPrecision(c.s.NetworkTx, 3) } func (c *statsContext) BlockIO() string { if c.s.IsInvalid { - return "--" + return noValue } return units.HumanSizeWithPrecision(c.s.BlockRead, 3) + " / " + units.HumanSizeWithPrecision(c.s.BlockWrite, 3) } func (c *statsContext) PIDs() string { if c.s.IsInvalid || c.os == winOSType { - return "--" + return noValue } return strconv.FormatUint(c.s.PidsCurrent, 10) } diff --git a/cli/command/node/formatter.go b/cli/command/node/formatter.go index cc82e6d037..d625b485e1 100644 --- a/cli/command/node/formatter.go +++ b/cli/command/node/formatter.go @@ -281,7 +281,8 @@ func (ctx *nodeInspectContext) ResourceNanoCPUs() int { if ctx.Node.Description.Resources.NanoCPUs == 0 { return int(0) } - return int(ctx.Node.Description.Resources.NanoCPUs) / 1e9 + const nano = 1e9 + return int(ctx.Node.Description.Resources.NanoCPUs) / nano } func (ctx *nodeInspectContext) ResourceMemory() string { diff --git a/cli/command/service/formatter.go b/cli/command/service/formatter.go index 9e043713be..499217ec11 100644 --- a/cli/command/service/formatter.go +++ b/cli/command/service/formatter.go @@ -503,7 +503,8 @@ func (ctx *serviceInspectContext) ResourceReservationNanoCPUs() float64 { if ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs == 0 { return float64(0) } - return float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs) / 1e9 + const nano = 1e9 + return float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs) / nano } func (ctx *serviceInspectContext) ResourceReservationMemory() string { @@ -521,7 +522,8 @@ func (ctx *serviceInspectContext) HasResourceLimits() bool { } func (ctx *serviceInspectContext) ResourceLimitsNanoCPUs() float64 { - return float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs) / 1e9 + const nano = 1e9 + return float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs) / nano } func (ctx *serviceInspectContext) ResourceLimitMemory() string {