Skip to content

Commit 40e605a

Browse files
authored
Merge pull request #6334 from thaJeztah/commands_nolock
internal/commands: remove mutexes / synchronisation and copy
2 parents 942a6c4 + 570a17b commit 40e605a

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

internal/commands/commands.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,38 @@ package commands
22

33
import (
44
"os"
5-
"slices"
6-
"sync"
75

86
"github.com/docker/cli/cli/command"
97
"github.com/spf13/cobra"
108
)
119

12-
var (
13-
commands []func(command.Cli) *cobra.Command
14-
l sync.RWMutex
15-
)
10+
var commands []func(command.Cli) *cobra.Command
1611

1712
// Register pushes the passed in command into an internal queue which can
18-
// be retrieved using the [Commands] function.
13+
// be retrieved using the [Commands] function. It is designed to be called
14+
// in an init function and is not safe for concurrent use.
1915
func Register(f func(command.Cli) *cobra.Command) {
20-
l.Lock()
21-
defer l.Unlock()
2216
commands = append(commands, f)
2317
}
2418

2519
// RegisterLegacy functions similarly to [Register], but it checks the
26-
// `DOCKER_HIDE_LEGACY_COMMANDS` environment variable and if
27-
// it has been set and is non-empty, the command will be hidden.
20+
// "DOCKER_HIDE_LEGACY_COMMANDS" environment variable and if it has been
21+
// set and is non-empty, the command will be hidden. It is designed to be called
22+
// in an init function and is not safe for concurrent use.
2823
func RegisterLegacy(f func(command.Cli) *cobra.Command) {
29-
l.Lock()
30-
defer l.Unlock()
3124
commands = append(commands, func(c command.Cli) *cobra.Command {
32-
cmd := f(c)
3325
if os.Getenv("DOCKER_HIDE_LEGACY_COMMANDS") == "" {
34-
return cmd
26+
return f(c)
3527
}
36-
cmdCopy := *cmd
37-
cmdCopy.Hidden = true
38-
cmdCopy.Aliases = []string{}
39-
return &cmdCopy
28+
cmd := f(c)
29+
cmd.Hidden = true
30+
cmd.Aliases = []string{}
31+
return cmd
4032
})
4133
}
4234

43-
// Commands returns a copy of the internal queue holding registered commands
44-
// added via [Register] or [RegisterLegacy].
35+
// Commands returns the internal queue holding registered commands added
36+
// via [Register] and [RegisterLegacy].
4537
func Commands() []func(command.Cli) *cobra.Command {
46-
l.RLock()
47-
defer l.RUnlock()
48-
return slices.Clone(commands)
38+
return commands
4939
}

0 commit comments

Comments
 (0)