Skip to content

Modern Docker Management, Designed for Everyone

refactor: modernize cli and types code

Kyle Mendellbb2ae3483d21
diff --git a/cli/internal/output/output.go b/cli/internal/output/output.go
index ee4305196..0b93ef30f 100644
--- a/cli/internal/output/output.go
+++ b/cli/internal/output/output.go
@@ -46,48 +46,48 @@ var ansiRegexp = regexp.MustCompile("\x1b\\[[0-9;]*[a-zA-Z]")
// Success prints a success message in green.
// The message is prefixed with a newline for visual separation.
// Format specifiers and arguments work like fmt.Printf.
-func Success(format string, a ...interface{}) {
+func Success(format string, a ...any) {
fmt.Printf("\n%s\n", successStyle.Render(fmt.Sprintf(format, a...)))
}
// Error prints an error message in red.
// The message is prefixed with a newline for visual separation.
// Format specifiers and arguments work like fmt.Printf.
-func Error(format string, a ...interface{}) {
+func Error(format string, a ...any) {
fmt.Printf("\n%s\n", errorStyle.Render(fmt.Sprintf(format, a...)))
}
// Warning prints a warning message in yellow.
// The message is prefixed with a newline for visual separation.
// Format specifiers and arguments work like fmt.Printf.
-func Warning(format string, a ...interface{}) {
+func Warning(format string, a ...any) {
fmt.Printf("\n%s\n", warnStyle.Render(fmt.Sprintf(format, a...)))
}
// Info prints an info message in cyan.
// The message is prefixed with a newline for visual separation.
// Format specifiers and arguments work like fmt.Printf.
-func Info(format string, a ...interface{}) {
+func Info(format string, a ...any) {
fmt.Printf("\n%s\n", infoStyle.Render(fmt.Sprintf(format, a...)))
}
// Header prints a header message in bold white.
// Use this to introduce sections of output. The message is prefixed
// with a newline for visual separation.
-func Header(format string, a ...interface{}) {
+func Header(format string, a ...any) {
fmt.Printf("\n%s\n", headerStyle.Render(fmt.Sprintf(format, a...)))
}
// Print prints a standard message without color formatting.
// Use this for regular output that doesn't need status indication.
-func Print(format string, a ...interface{}) {
+func Print(format string, a ...any) {
fmt.Printf(format+"\n", a...)
}
// KeyValue prints a key-value pair with the key in bold and value in blue.
// This is useful for displaying structured information like image details
// or configuration values.
-func KeyValue(key string, value interface{}) {
+func KeyValue(key string, value any) {
fmt.Printf("%s: %v\n", keyStyle.Render(key), valueStyle.Render(fmt.Sprint(value)))
}
@@ -234,13 +234,13 @@ func computeWidths(headers []string, rows [][]string) []int {
widths[i] = runewidth.StringWidth(stripAnsi(h))
}
for _, row := range rows {
- for i := 0; i < n; i++ {
+ for i := range n {
var cell string
if i < len(row) {
cell = row[i]
}
- lines := strings.Split(cell, "\n")
- for _, ln := range lines {
+ lines := strings.SplitSeq(cell, "\n")
+ for ln := range lines {
w := runewidth.StringWidth(stripAnsi(ln))
if w > widths[i] {
widths[i] = w
@@ -257,10 +257,7 @@ func printHeader(headers []string, widths []int) {
for i, h := range headers {
visible := stripAnsi(h)
colored := columnHeader.Render(h)
- padLen := widths[i] - runewidth.StringWidth(visible)
- if padLen < 0 {
- padLen = 0
- }
+ padLen := max(widths[i]-runewidth.StringWidth(visible), 0)
if i < n-1 {
fmt.Print(colored + strings.Repeat(" ", padLen) + sep)
} else {
@@ -276,7 +273,7 @@ func printRow(row []string, widths []int, n int) {
// Prepare lines per column
cellLines := make([][]string, n)
maxLines := 1
- for i := 0; i < n; i++ {
+ for i := range n {
var cell string
if i < len(row) {
cell = row[i]
@@ -289,7 +286,7 @@ func printRow(row []string, widths []int, n int) {
}
for li := 0; li < maxLines; li++ {
- for i := 0; i < n; i++ {
+ for i := range n {
var val string
if li < len(cellLines[i]) {
val = cellLines[i][li]
@@ -302,10 +299,7 @@ func printRow(row []string, widths []int, n int) {
rendered = fmt.Sprint(val)
}
- padLen := widths[i] - runewidth.StringWidth(stripAnsi(val))
- if padLen < 0 {
- padLen = 0
- }
+ padLen := max(widths[i]-runewidth.StringWidth(stripAnsi(val)), 0)
if i < n-1 {
fmt.Print(rendered + strings.Repeat(" ", padLen) + sep)
diff --git a/cli/internal/prompt/prompt.go b/cli/internal/prompt/prompt.go
index f0a3affed..681234b79 100644
--- a/cli/internal/prompt/prompt.go
+++ b/cli/internal/prompt/prompt.go
@@ -81,10 +81,7 @@ func (m selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
width := msg.Width
- height := msg.Height - 4
- if height < 6 {
- height = 6
- }
+ height := max(msg.Height-4, 6)
if width < 20 {
width = 20
}
diff --git a/cli/pkg/admin/apikeys/cmd.go b/cli/pkg/admin/apikeys/cmd.go
index 38cbbcd57..410fd13bf 100644
--- a/cli/pkg/admin/apikeys/cmd.go
+++ b/cli/pkg/admin/apikeys/cmd.go
@@ -183,7 +183,7 @@ var deleteCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
diff --git a/cli/pkg/auth/cmd.go b/cli/pkg/auth/cmd.go
index 20eff0ff0..90753dd24 100644
--- a/cli/pkg/auth/cmd.go
+++ b/cli/pkg/auth/cmd.go
@@ -184,7 +184,7 @@ var logoutCmd = &cobra.Command{
}
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err == nil {
if resultBytes, err := json.MarshalIndent(result.Data, "", " "); err == nil {
fmt.Println(string(resultBytes))
@@ -215,7 +215,7 @@ var meCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
@@ -289,7 +289,7 @@ var passwordCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err == nil {
if resultBytes, err := json.MarshalIndent(result.Data, "", " "); err == nil {
fmt.Println(string(resultBytes))
diff --git a/cli/pkg/environments/cmd.go b/cli/pkg/environments/cmd.go
index 2062aa3cd..830140b99 100644
--- a/cli/pkg/environments/cmd.go
+++ b/cli/pkg/environments/cmd.go
@@ -190,7 +190,7 @@ var testCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err == nil {
if resultBytes, err := json.MarshalIndent(result.Data, "", " "); err == nil {
fmt.Println(string(resultBytes))
diff --git a/cli/pkg/generate/secret_test.go b/cli/pkg/generate/secret_test.go
index 4b91dc940..37cf2b509 100644
--- a/cli/pkg/generate/secret_test.go
+++ b/cli/pkg/generate/secret_test.go
@@ -58,12 +58,12 @@ func TestSecretDefaultBase64(t *testing.T) {
// find ENCRYPTION_KEY= and JWT_SECRET= lines and ensure they are valid base64
var encVal, jwtVal string
- for _, line := range strings.Split(out, "\n") {
- if strings.HasPrefix(line, "ENCRYPTION_KEY=") {
- encVal = strings.TrimPrefix(line, "ENCRYPTION_KEY=")
+ for line := range strings.SplitSeq(out, "\n") {
+ if after, ok := strings.CutPrefix(line, "ENCRYPTION_KEY="); ok {
+ encVal = after
}
- if strings.HasPrefix(line, "JWT_SECRET=") {
- jwtVal = strings.TrimPrefix(line, "JWT_SECRET=")
+ if after, ok := strings.CutPrefix(line, "JWT_SECRET="); ok {
+ jwtVal = after
}
}
if encVal == "" || jwtVal == "" {
@@ -112,17 +112,17 @@ func TestSecretAllFormatContainsSections(t *testing.T) {
// verify hex values decode to 32 bytes
var hexEnc, hexJwt string
- for _, line := range strings.Split(out, "\n") {
- if strings.HasPrefix(line, "ENCRYPTION_KEY=") {
- v := strings.TrimPrefix(line, "ENCRYPTION_KEY=")
+ for line := range strings.SplitSeq(out, "\n") {
+ if after, ok := strings.CutPrefix(line, "ENCRYPTION_KEY="); ok {
+ v := after
// prefer hex if line length looks like hex (64 chars)
if len(v) >= 64 {
hexEnc = v
continue
}
}
- if strings.HasPrefix(line, "JWT_SECRET=") {
- v := strings.TrimPrefix(line, "JWT_SECRET=")
+ if after, ok := strings.CutPrefix(line, "JWT_SECRET="); ok {
+ v := after
if len(v) >= 64 {
hexJwt = v
continue
diff --git a/cli/pkg/images/cmd.go b/cli/pkg/images/cmd.go
index 09f6838e0..0ee34b49c 100644
--- a/cli/pkg/images/cmd.go
+++ b/cli/pkg/images/cmd.go
@@ -356,7 +356,7 @@ var imagesPullCmd = &cobra.Command{
log.Debugf("Pulling image from: %s", path)
- requestBody := map[string]interface{}{
+ requestBody := map[string]any{
"imageName": imageName,
}
@@ -468,7 +468,7 @@ var imagesPruneCmd = &cobra.Command{
log.Debugf("Pruning images from: %s", path)
- requestBody := map[string]interface{}{
+ requestBody := map[string]any{
"dangling": pruneDangling,
}
diff --git a/cli/pkg/networks/cmd.go b/cli/pkg/networks/cmd.go
index 4387dc460..a9451bd14 100644
--- a/cli/pkg/networks/cmd.go
+++ b/cli/pkg/networks/cmd.go
@@ -185,7 +185,7 @@ var deleteCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
diff --git a/cli/pkg/projects/cmd.go b/cli/pkg/projects/cmd.go
index 7d8c3b5f3..a49376b9d 100644
--- a/cli/pkg/projects/cmd.go
+++ b/cli/pkg/projects/cmd.go
@@ -341,7 +341,7 @@ var countsCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[map[string]interface{}]
+ var result base.ApiResponse[map[string]any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
diff --git a/cli/pkg/registries/cmd.go b/cli/pkg/registries/cmd.go
index 7909fec12..dd36ec632 100644
--- a/cli/pkg/registries/cmd.go
+++ b/cli/pkg/registries/cmd.go
@@ -104,7 +104,7 @@ var syncCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err == nil {
if resultBytes, err := json.MarshalIndent(result.Data, "", " "); err == nil {
fmt.Println(string(resultBytes))
@@ -136,7 +136,7 @@ var testCmd = &cobra.Command{
defer func() { _ = resp.Body.Close() }()
if jsonOutput {
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err == nil {
if resultBytes, err := json.MarshalIndent(result.Data, "", " "); err == nil {
fmt.Println(string(resultBytes))
diff --git a/cli/pkg/volumes/cmd.go b/cli/pkg/volumes/cmd.go
index 8b0fe3cb7..3706b7654 100644
--- a/cli/pkg/volumes/cmd.go
+++ b/cli/pkg/volumes/cmd.go
@@ -184,7 +184,7 @@ var countsCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
@@ -237,7 +237,7 @@ var pruneCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
@@ -272,7 +272,7 @@ var sizesCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
@@ -319,7 +319,7 @@ var usageCmd = &cobra.Command{
}
defer func() { _ = resp.Body.Close() }()
- var result base.ApiResponse[interface{}]
+ var result base.ApiResponse[any]
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
diff --git a/types/base/json.go b/types/base/json.go
index 4e515bb8d..4e7ceb27a 100644
--- a/types/base/json.go
+++ b/types/base/json.go
@@ -10,7 +10,7 @@ import (
// seamless database integration.
//
// nolint:recvcheck
-type JsonObject map[string]interface{}
+type JsonObject map[string]any
// Value implements the driver.Valuer interface for database storage.
// It marshals the JsonObject to JSON bytes for database insertion.
@@ -26,7 +26,7 @@ func (j JsonObject) Value() (driver.Value, error) {
// It unmarshals JSON data from the database into the JsonObject.
// Supports scanning from []byte or string values.
// If the value is nil, the JsonObject is set to nil.
-func (j *JsonObject) Scan(value interface{}) error {
+func (j *JsonObject) Scan(value any) error {
if value == nil {
*j = nil
return nil
diff --git a/types/container/container.go b/types/container/container.go
index 3a9585cb7..9e812a3f1 100644
--- a/types/container/container.go
+++ b/types/container/container.go
@@ -1,6 +1,7 @@
package container
import (
+ "maps"
"strconv"
"strings"
@@ -856,9 +857,7 @@ func NewDetails(c *container.InspectResponse) Details {
}
imageName = c.Config.Image
if c.Config.Labels != nil {
- for k, v := range c.Config.Labels {
- labels[k] = v
- }
+ maps.Copy(labels, c.Config.Labels)
}
}
diff --git a/types/event/event.go b/types/event/event.go
index 4897ed34e..280e353df 100644
--- a/types/event/event.go
+++ b/types/event/event.go
@@ -62,7 +62,7 @@ type Event struct {
// Metadata contains additional key-value data associated with the event.
//
// Required: false
- Metadata map[string]interface{} `json:"metadata,omitempty"`
+ Metadata map[string]any `json:"metadata,omitempty"`
// Timestamp is when the event occurred.
//
@@ -134,7 +134,7 @@ type CreateEvent struct {
// Metadata contains additional key-value data associated with the event.
//
// Required: false
- Metadata map[string]interface{} `json:"metadata,omitempty"`
+ Metadata map[string]any `json:"metadata,omitempty"`
}
type ListReponse struct {
diff --git a/types/image/image.go b/types/image/image.go
index 01634b739..69c0d8a43 100644
--- a/types/image/image.go
+++ b/types/image/image.go
@@ -128,7 +128,7 @@ type Summary struct {
// Labels contains user-defined metadata for the image.
//
// Required: true
- Labels map[string]interface{} `json:"labels"`
+ Labels map[string]any `json:"labels"`
// InUse indicates if the image is currently in use by a container.
//
@@ -297,7 +297,7 @@ type DetailSummary struct {
// Required: true
GraphDriver struct {
// Data contains driver-specific data.
- Data interface{} `json:"data"`
+ Data any `json:"data"`
// Name is the name of the graph driver.
Name string `json:"name"`
} `json:"graphDriver"`
diff --git a/types/updater/updater.go b/types/updater/updater.go
index 9ca73c8aa..4064dc6ca 100644
--- a/types/updater/updater.go
+++ b/types/updater/updater.go
@@ -64,7 +64,7 @@ type ResourceResult struct {
// Details contains additional details about the update operation.
//
// Required: false
- Details map[string]interface{} `json:"details,omitempty"`
+ Details map[string]any `json:"details,omitempty"`
}
// Result represents the complete result of an update operation.