Add condition for os match

Condition: If OS matches and there is no more specific match, the command
will be executed

Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
pull/5176/head
Martin Hickey 7 years ago
parent 16b5a251c6
commit 9231af180c

@ -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 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 // - If no OS/Arch match is found, return nil
func getPlatformCommand(platformCommands []PlatformCommand) []string { func getPlatformCommand(platformCommands []PlatformCommand) []string {
var command []string
for _, platformCommand := range platformCommands { 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) { if strings.EqualFold(platformCommand.OperatingSystem, runtime.GOOS) && strings.EqualFold(platformCommand.Architecture, runtime.GOARCH) {
return strings.Split(os.ExpandEnv(platformCommand.Command), " ") 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: // PrepareCommand takes a Plugin.PlatformCommand.Command, a Plugin.Command and will applying the following processing:

@ -21,17 +21,8 @@ import (
"testing" "testing"
) )
func TestPrepareCommand(t *testing.T) { func checkCommand(p *Plugin, extraArgs []string, osStrCmp string, t *testing.T) {
p := &Plugin{ cmd, args, err := p.PrepareCommand(extraArgs)
Dir: "/tmp", // Unused
Metadata: &Metadata{
Name: "test",
Command: "echo -n foo",
},
}
argv := []string{"--debug", "--foo", "bar"}
cmd, args, err := p.PrepareCommand(argv)
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
@ -43,7 +34,7 @@ func TestPrepareCommand(t *testing.T) {
t.Errorf("expected 5 args, got %d", l) 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++ { for i := 0; i < len(args); i++ {
if expect[i] != args[i] { if expect[i] != args[i] {
t.Errorf("Expected arg=%q, got %q", 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 // Test with IgnoreFlags. This should omit --debug, --foo, bar
p.Metadata.IgnoreFlags = true p.Metadata.IgnoreFlags = true
cmd, args, err = p.PrepareCommand(argv) cmd, args, err = p.PrepareCommand(extraArgs)
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
@ -62,7 +53,7 @@ func TestPrepareCommand(t *testing.T) {
if l := len(args); l != 2 { if l := len(args); l != 2 {
t.Errorf("expected 2 args, got %d", l) t.Errorf("expected 2 args, got %d", l)
} }
expect = []string{"-n", "foo"} expect = []string{"-n", osStrCmp}
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
if expect[i] != args[i] { if expect[i] != args[i] {
t.Errorf("Expected arg=%q, got %q", 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) { func TestPlatformPrepareCommand(t *testing.T) {
p := &Plugin{ p := &Plugin{
Dir: "/tmp", // Unused Dir: "/tmp", // Unused
@ -84,19 +88,6 @@ func TestPlatformPrepareCommand(t *testing.T) {
}, },
} }
argv := []string{"--debug", "--foo", "bar"} 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 var osStrCmp string
os := runtime.GOOS os := runtime.GOOS
arch := runtime.GOARCH arch := runtime.GOARCH
@ -109,31 +100,35 @@ func TestPlatformPrepareCommand(t *testing.T) {
} else { } else {
osStrCmp = "os-arch" 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 checkCommand(p, argv, osStrCmp, t)
p.Metadata.IgnoreFlags = true }
cmd, args, err = p.PrepareCommand(argv)
if err != nil { func TestPartialPlatformPrepareCommand(t *testing.T) {
t.Errorf(err.Error()) p := &Plugin{
} Dir: "/tmp", // Unused
if cmd != "echo" { Metadata: &Metadata{
t.Errorf("Expected echo, got %q", cmd) Name: "test",
} Command: "echo -n os-arch",
if l := len(args); l != 2 { PlatformCommand: []PlatformCommand{
t.Errorf("expected 2 args, got %d", l) {OperatingSystem: "linux", Architecture: "i386", Command: "echo -n linux-i386"},
{OperatingSystem: "windows", Architecture: "amd64", Command: "echo -n win-64"},
},
},
} }
expect = []string{"-n", osStrCmp} argv := []string{"--debug", "--foo", "bar"}
for i := 0; i < len(args); i++ { var osStrCmp string
if expect[i] != args[i] { os := runtime.GOOS
t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) 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) { func TestNoPrepareCommand(t *testing.T) {
@ -157,7 +152,7 @@ func TestNoMatchPrepareCommand(t *testing.T) {
Metadata: &Metadata{ Metadata: &Metadata{
Name: "test", Name: "test",
PlatformCommand: []PlatformCommand{ PlatformCommand: []PlatformCommand{
{OperatingSystem: "linux", Architecture: "no-arch", Command: "echo -n linux-i386"}, {OperatingSystem: "no-os", Architecture: "amd64", Command: "echo -n linux-i386"},
}, },
}, },
} }

Loading…
Cancel
Save