Skip to content

Commit

Permalink
POC: subcommand create
Browse files Browse the repository at this point in the history
  • Loading branch information
mjudeikis committed Nov 10, 2024
1 parent 9f83688 commit 211d9e0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
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 @@ type installOperation struct {
pluginName string
platform index.Platform

createSubCommand bool

installDir string
binDir string
}
Expand Down Expand Up @@ -79,8 +81,9 @@ func Install(p environment.Paths, plugin index.Plugin, indexName string, opts In
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 @@ func install(op installOperation, opts InstallOpts) error {
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,6 +173,8 @@ func Uninstall(p environment.Paths, name string) error {

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 {
Expand All @@ -187,8 +192,8 @@ func Uninstall(p environment.Paths, name string) error {
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 @@ func IsWindows() bool {

// 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

0 comments on commit 211d9e0

Please sign in to comment.