Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: subcommand create #867

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions internal/installation/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
pluginName string
platform index.Platform

createSubCommand bool

installDir string
binDir string
}
Expand Down Expand Up @@ -79,8 +81,9 @@
pluginName: plugin.Name,
platform: candidate,

binDir: p.BinPath(),
installDir: p.PluginVersionInstallPath(plugin.Name, plugin.Spec.Version),
createSubCommand: plugin.Spec.CreateSubCommand,
binDir: p.BinPath(),
installDir: p.PluginVersionInstallPath(plugin.Name, plugin.Spec.Version),
}, opts); err != nil {
return errors.Wrap(err, "install failed")
}
Expand Down Expand Up @@ -125,7 +128,7 @@
if _, ok := pathutil.IsSubPath(subPathAbs, pathAbs); !ok {
return errors.Wrapf(err, "the fullPath %q does not extend the sub-fullPath %q", fullPath, op.installDir)
}
err = createOrUpdateLink(op.binDir, fullPath, op.pluginName)
err = createOrUpdateLink(op.binDir, fullPath, op.pluginName, op.createSubCommand)
return errors.Wrap(err, "failed to link installed plugin")
}

Expand Down Expand Up @@ -170,7 +173,9 @@

klog.V(1).Infof("Deleting plugin %s", name)

// Clarify: How can we handle this case? uninstalling a plugin does not have manifest provided.
// Maybe should store manifest used in binpath?
symlinkPath := filepath.Join(p.BinPath(), pluginNameToBin(name, IsWindows()))

Check failure on line 178 in internal/installation/install.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to pluginNameToBin

Check failure on line 178 in internal/installation/install.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to pluginNameToBin

Check failure on line 178 in internal/installation/install.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to pluginNameToBin

Check failure on line 178 in internal/installation/install.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to pluginNameToBin
klog.V(3).Infof("Unlink %q", symlinkPath)
if err := removeLink(symlinkPath); err != nil {
return errors.Wrap(err, "could not uninstall symlink of plugin")
Expand All @@ -187,8 +192,8 @@
return errors.Wrapf(err, "could not remove plugin receipt %q", pluginReceiptPath)
}

func createOrUpdateLink(binDir, binary, plugin string) error {
dst := filepath.Join(binDir, pluginNameToBin(plugin, IsWindows()))
func createOrUpdateLink(binDir, binary, plugin string, createSubCommand bool) error {
dst := filepath.Join(binDir, pluginNameToBin(plugin, IsWindows(), createSubCommand))

if err := removeLink(dst); err != nil {
return errors.Wrap(err, "failed to remove old symlink")
Expand Down Expand Up @@ -238,7 +243,10 @@

// pluginNameToBin creates the name of the symlink file for the plugin name.
// It converts dashes to underscores.
func pluginNameToBin(name string, isWindows bool) string {
func pluginNameToBin(name string, isWindows bool, createSubCommand bool) string {
if createSubCommand && !isWindows {
return name
}
name = strings.ReplaceAll(name, "-", "_")
name = "kubectl-" + name
if isWindows {
Expand Down
28 changes: 15 additions & 13 deletions internal/installation/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ func Test_moveTargets(t *testing.T) {

func Test_createOrUpdateLink(t *testing.T) {
tests := []struct {
name string
pluginName string
binary string
wantErr bool
name string
pluginName string
binary string
createSubCommand bool
wantErr bool
}{
{
name: "normal link",
Expand All @@ -107,7 +108,7 @@ func Test_createOrUpdateLink(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tmpDir := testutil.NewTempDir(t)

if err := createOrUpdateLink(tmpDir.Root(), tt.binary, tt.pluginName); (err != nil) != tt.wantErr {
if err := createOrUpdateLink(tmpDir.Root(), tt.binary, tt.pluginName, tt.createSubCommand); (err != nil) != tt.wantErr {
t.Errorf("createOrUpdateLink() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand All @@ -116,18 +117,19 @@ func Test_createOrUpdateLink(t *testing.T) {

func Test_pluginNameToBin(t *testing.T) {
tests := []struct {
name string
isWindows bool
want string
name string
isWindows bool
createSubCommand bool
want string
}{
{"foo", false, "kubectl-foo"},
{"foo-bar", false, "kubectl-foo_bar"},
{"foo", true, "kubectl-foo.exe"},
{"foo-bar", true, "kubectl-foo_bar.exe"},
{"foo", false, false, "kubectl-foo"},
{"foo-bar", false, false, "kubectl-foo_bar"},
{"foo", true, false, "kubectl-foo.exe"},
{"foo-bar", true, false, "kubectl-foo_bar.exe"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pluginNameToBin(tt.name, tt.isWindows); got != tt.want {
if got := pluginNameToBin(tt.name, tt.isWindows, tt.createSubCommand); got != tt.want {
t.Errorf("pluginNameToBin(%v, %v) = %v; want %v", tt.name, tt.isWindows, got, tt.want)
}
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/index/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type PluginSpec struct {
Caveats string `json:"caveats,omitempty"`
Homepage string `json:"homepage,omitempty"`

// CreateSubCommand specifies whether the plugin should be installed as a
// create subcommand. If true, the plugin will preserve first 'create' word
// in the plugin name.
CreateSubCommand bool `json:"createSubCommand,omitempty"`

Platforms []Platform `json:"platforms,omitempty"`
}

Expand Down
Loading