Skip to content

Commit 5bce5e1

Browse files
Merge pull request #6415 from thaJeztah/plugin_no_regex
cli-plugins/manager: replace pluginNameRe for isValidPluginName utility
2 parents 0640306 + 2351f5b commit 5bce5e1

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

cli-plugins/manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func PluginRunCommand(dockerCli config.Provider, name string, rootcmd *cobra.Com
169169
// have been provided by cobra to our caller. This is because
170170
// they lack e.g. global options which we must propagate here.
171171
args := os.Args[1:]
172-
if !pluginNameRe.MatchString(name) {
172+
if !isValidPluginName(name) {
173173
// We treat this as "not found" so that callers will
174174
// fallback to their "invalid" command path.
175175
return nil, errPluginNotFound(name)

cli-plugins/manager/plugin.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ import (
1212
"strings"
1313

1414
"github.com/docker/cli/cli-plugins/metadata"
15-
"github.com/docker/cli/internal/lazyregexp"
1615
"github.com/spf13/cobra"
1716
)
1817

19-
var pluginNameRe = lazyregexp.New("^[a-z][a-z0-9]*$")
20-
2118
// Plugin represents a potential plugin with all it's metadata.
2219
type Plugin struct {
2320
metadata.Metadata
@@ -85,8 +82,8 @@ func newPlugin(c pluginCandidate, cmds []*cobra.Command) (Plugin, error) {
8582
}
8683

8784
// Now apply the candidate tests, so these update p.Err.
88-
if !pluginNameRe.MatchString(p.Name) {
89-
p.Err = newPluginError("plugin candidate %q did not match %q", p.Name, pluginNameRe.String())
85+
if !isValidPluginName(p.Name) {
86+
p.Err = newPluginError("plugin candidate %q did not match %q", p.Name, pluginNameFormat)
9087
return p, nil
9188
}
9289

@@ -147,3 +144,26 @@ func (p *Plugin) RunHook(ctx context.Context, hookData HookPluginData) ([]byte,
147144

148145
return hookCmdOutput, nil
149146
}
147+
148+
// pluginNameFormat is used as part of errors for invalid plugin-names.
149+
// We should consider making this less technical ("must start with "a-z",
150+
// and only consist of lowercase alphanumeric characters").
151+
const pluginNameFormat = `^[a-z][a-z0-9]*$`
152+
153+
func isValidPluginName(s string) bool {
154+
if len(s) == 0 {
155+
return false
156+
}
157+
// first character must be a-z
158+
if c := s[0]; c < 'a' || c > 'z' {
159+
return false
160+
}
161+
// followed by a-z or 0-9
162+
for i := 1; i < len(s); i++ {
163+
c := s[i]
164+
if (c < 'a' || c > 'z') && (c < '0' || c > '9') {
165+
return false
166+
}
167+
}
168+
return true
169+
}

0 commit comments

Comments
 (0)