diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index cf5c72f74..546f66744 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -109,12 +109,16 @@ type Plugin struct { // - If OS matches and there is no more specific match, the command will be prepared for execution // - If no OS/Arch match is found, return nil func getPlatformCommand(platformCommands []PlatformCommand) []string { + var command []string for _, platformCommand := range platformCommands { + if strings.EqualFold(platformCommand.OperatingSystem, runtime.GOOS) { + command = strings.Split(os.ExpandEnv(platformCommand.Command), " ") + } if strings.EqualFold(platformCommand.OperatingSystem, runtime.GOOS) && strings.EqualFold(platformCommand.Architecture, runtime.GOARCH) { return strings.Split(os.ExpandEnv(platformCommand.Command), " ") } } - return nil + return command } // PrepareCommand takes a Plugin.PlatformCommand.Command, a Plugin.Command and will applying the following processing: diff --git a/pkg/plugin/plugin_test.go b/pkg/plugin/plugin_test.go index 9885ffd3c..de707c281 100644 --- a/pkg/plugin/plugin_test.go +++ b/pkg/plugin/plugin_test.go @@ -21,17 +21,8 @@ import ( "testing" ) -func TestPrepareCommand(t *testing.T) { - p := &Plugin{ - Dir: "/tmp", // Unused - Metadata: &Metadata{ - Name: "test", - Command: "echo -n foo", - }, - } - argv := []string{"--debug", "--foo", "bar"} - - cmd, args, err := p.PrepareCommand(argv) +func checkCommand(p *Plugin, extraArgs []string, osStrCmp string, t *testing.T) { + cmd, args, err := p.PrepareCommand(extraArgs) if err != nil { t.Errorf(err.Error()) } @@ -43,7 +34,7 @@ func TestPrepareCommand(t *testing.T) { t.Errorf("expected 5 args, got %d", l) } - expect := []string{"-n", "foo", "--debug", "--foo", "bar"} + expect := []string{"-n", osStrCmp, "--debug", "--foo", "bar"} for i := 0; i < len(args); i++ { if expect[i] != args[i] { t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) @@ -52,7 +43,7 @@ func TestPrepareCommand(t *testing.T) { // Test with IgnoreFlags. This should omit --debug, --foo, bar p.Metadata.IgnoreFlags = true - cmd, args, err = p.PrepareCommand(argv) + cmd, args, err = p.PrepareCommand(extraArgs) if err != nil { t.Errorf(err.Error()) } @@ -62,7 +53,7 @@ func TestPrepareCommand(t *testing.T) { if l := len(args); l != 2 { t.Errorf("expected 2 args, got %d", l) } - expect = []string{"-n", "foo"} + expect = []string{"-n", osStrCmp} for i := 0; i < len(args); i++ { if expect[i] != args[i] { t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) @@ -70,6 +61,19 @@ func TestPrepareCommand(t *testing.T) { } } +func TestPrepareCommand(t *testing.T) { + p := &Plugin{ + Dir: "/tmp", // Unused + Metadata: &Metadata{ + Name: "test", + Command: "echo -n foo", + }, + } + argv := []string{"--debug", "--foo", "bar"} + + checkCommand(p, argv, "foo", t) +} + func TestPlatformPrepareCommand(t *testing.T) { p := &Plugin{ Dir: "/tmp", // Unused @@ -84,19 +88,6 @@ func TestPlatformPrepareCommand(t *testing.T) { }, } argv := []string{"--debug", "--foo", "bar"} - - cmd, args, err := p.PrepareCommand(argv) - if err != nil { - t.Errorf(err.Error()) - } - if cmd != "echo" { - t.Errorf("Expected echo, got %q", cmd) - } - - if l := len(args); l != 5 { - t.Errorf("expected 5 args, got %d", l) - } - var osStrCmp string os := runtime.GOOS arch := runtime.GOARCH @@ -109,31 +100,35 @@ func TestPlatformPrepareCommand(t *testing.T) { } else { osStrCmp = "os-arch" } - expect := []string{"-n", osStrCmp, "--debug", "--foo", "bar"} - for i := 0; i < len(args); i++ { - if expect[i] != args[i] { - t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) - } - } - // Test with IgnoreFlags. This should omit --debug, --foo, bar - p.Metadata.IgnoreFlags = true - cmd, args, err = p.PrepareCommand(argv) - if err != nil { - t.Errorf(err.Error()) - } - if cmd != "echo" { - t.Errorf("Expected echo, got %q", cmd) - } - if l := len(args); l != 2 { - t.Errorf("expected 2 args, got %d", l) + checkCommand(p, argv, osStrCmp, t) +} + +func TestPartialPlatformPrepareCommand(t *testing.T) { + p := &Plugin{ + Dir: "/tmp", // Unused + Metadata: &Metadata{ + Name: "test", + Command: "echo -n os-arch", + PlatformCommand: []PlatformCommand{ + {OperatingSystem: "linux", Architecture: "i386", Command: "echo -n linux-i386"}, + {OperatingSystem: "windows", Architecture: "amd64", Command: "echo -n win-64"}, + }, + }, } - expect = []string{"-n", osStrCmp} - for i := 0; i < len(args); i++ { - if expect[i] != args[i] { - t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) - } + argv := []string{"--debug", "--foo", "bar"} + var osStrCmp string + os := runtime.GOOS + arch := runtime.GOARCH + if os == "linux" { + osStrCmp = "linux-i386" + } else if os == "windows" && arch == "amd64" { + osStrCmp = "win-64" + } else { + osStrCmp = "os-arch" } + + checkCommand(p, argv, osStrCmp, t) } func TestNoPrepareCommand(t *testing.T) { @@ -157,7 +152,7 @@ func TestNoMatchPrepareCommand(t *testing.T) { Metadata: &Metadata{ Name: "test", PlatformCommand: []PlatformCommand{ - {OperatingSystem: "linux", Architecture: "no-arch", Command: "echo -n linux-i386"}, + {OperatingSystem: "no-os", Architecture: "amd64", Command: "echo -n linux-i386"}, }, }, }