New registry plugin func GetPluginName. Re-use regsitry.reference

Signed-off-by: Scott Rigby <scott@r6by.com>
pull/10364/merge
Scott Rigby 1 week ago
parent 7d22bb25fa
commit fd41fdd9c9

@ -24,7 +24,6 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"
"helm.sh/helm/v4/internal/plugin/cache"
"helm.sh/helm/v4/internal/third_party/dep/fs"
@ -45,21 +44,10 @@ type OCIInstaller struct {
// NewOCIInstaller creates a new OCIInstaller with optional getter options
func NewOCIInstaller(source string, options ...getter.Option) (*OCIInstaller, error) {
ref := strings.TrimPrefix(source, fmt.Sprintf("%s://", registry.OCIScheme))
// Extract plugin name from OCI reference
// e.g., "ghcr.io/user/plugin-name:v1.0.0" -> "plugin-name"
parts := strings.Split(ref, "/")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid OCI reference: %s", source)
}
lastPart := parts[len(parts)-1]
pluginName := lastPart
if idx := strings.LastIndex(lastPart, ":"); idx > 0 {
pluginName = lastPart[:idx]
}
if idx := strings.LastIndex(lastPart, "@"); idx > 0 {
pluginName = lastPart[:idx]
// Extract plugin name from OCI reference using robust registry parsing
pluginName, err := registry.GetPluginName(source)
if err != nil {
return nil, err
}
key, err := cache.Key(source)

@ -174,3 +174,28 @@ func PluginPullOptWithPluginName(name string) PluginPullOption {
operation.pluginName = name
}
}
// GetPluginName extracts the plugin name from an OCI reference using proper reference parsing
func GetPluginName(source string) (string, error) {
ref, err := newReference(source)
if err != nil {
return "", fmt.Errorf("invalid OCI reference: %w", err)
}
// Extract plugin name from the repository path
// e.g., "ghcr.io/user/plugin-name:v1.0.0" -> Repository: "user/plugin-name"
repository := ref.Repository
if repository == "" {
return "", fmt.Errorf("invalid OCI reference: missing repository")
}
// Get the last part of the repository path as the plugin name
parts := strings.Split(repository, "/")
pluginName := parts[len(parts)-1]
if pluginName == "" {
return "", fmt.Errorf("invalid OCI reference: cannot determine plugin name from repository %s", repository)
}
return pluginName, nil
}

@ -0,0 +1,93 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package registry
import (
"testing"
)
func TestGetPluginName(t *testing.T) {
tests := []struct {
name string
source string
expected string
expectErr bool
}{
{
name: "valid OCI reference with tag",
source: "oci://ghcr.io/user/plugin-name:v1.0.0",
expected: "plugin-name",
},
{
name: "valid OCI reference with digest",
source: "oci://ghcr.io/user/plugin-name@sha256:1234567890abcdef",
expected: "plugin-name",
},
{
name: "valid OCI reference without tag",
source: "oci://ghcr.io/user/plugin-name",
expected: "plugin-name",
},
{
name: "valid OCI reference with multiple path segments",
source: "oci://registry.example.com/org/team/plugin-name:latest",
expected: "plugin-name",
},
{
name: "valid OCI reference with plus signs in tag",
source: "oci://registry.example.com/user/plugin-name:v1.0.0+build.1",
expected: "plugin-name",
},
{
name: "valid OCI reference - single path segment",
source: "oci://registry.example.com/plugin",
expected: "plugin",
},
{
name: "invalid OCI reference - no repository",
source: "oci://registry.example.com",
expectErr: true,
},
{
name: "invalid OCI reference - malformed",
source: "not-an-oci-reference",
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pluginName, err := GetPluginName(tt.source)
if tt.expectErr {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if pluginName != tt.expected {
t.Errorf("expected plugin name %q, got %q", tt.expected, pluginName)
}
})
}
}
Loading…
Cancel
Save