Worker: better way to get array command parameters
This commit is contained in:
parent
ef2bbd2845
commit
e5e466931b
@ -41,19 +41,26 @@ type BlenderSettings struct {
|
||||
// cmdBlender executes the "blender-render" command.
|
||||
func (ce *CommandExecutor) cmdBlenderRender(ctx context.Context, logger zerolog.Logger, taskID string, cmd api.Command) error {
|
||||
settings := BlenderSettings{
|
||||
exe: cmd.Parameters["exe"].(string),
|
||||
argsBefore: cmd.Parameters["argsBefore"].([]string),
|
||||
blendfile: cmd.Parameters["blendfile"].(string),
|
||||
args: cmd.Parameters["args"].([]string),
|
||||
exe: cmd.Parameters["exe"].(string),
|
||||
blendfile: cmd.Parameters["blendfile"].(string),
|
||||
}
|
||||
var ok bool
|
||||
if settings.exe == "" {
|
||||
logger.Warn().Interface("command", cmd).Msg("missing 'exe' setting")
|
||||
return fmt.Errorf("missing 'exe' setting: %+v", cmd)
|
||||
}
|
||||
if settings.argsBefore, ok = cmdSettingAsStrings(cmd, "argsBefore"); !ok {
|
||||
logger.Warn().Interface("command", cmd).Msg("invalid 'argsBefore' setting")
|
||||
return fmt.Errorf("invalid 'argsBefore' setting: %+v", cmd)
|
||||
}
|
||||
if settings.blendfile == "" {
|
||||
logger.Warn().Interface("command", cmd).Msg("missing 'blendfile' setting")
|
||||
return fmt.Errorf("missing 'blendfile' setting: %+v", cmd)
|
||||
}
|
||||
if settings.args, ok = cmdSettingAsStrings(cmd, "args"); !ok {
|
||||
logger.Warn().Interface("command", cmd).Msg("invalid 'args' setting")
|
||||
return fmt.Errorf("invalid 'args' setting: %+v", cmd)
|
||||
}
|
||||
|
||||
cliArgs := make([]string, 0)
|
||||
cliArgs = append(cliArgs, settings.argsBefore...)
|
||||
|
@ -87,3 +87,33 @@ func (ce *CommandExecutor) Run(ctx context.Context, taskID string, cmd api.Comma
|
||||
|
||||
return runner(ctx, logger, taskID, cmd)
|
||||
}
|
||||
|
||||
// cmdSettingAsStrings converts an array setting ([]interface{}) to a []string slice.
|
||||
func cmdSettingAsStrings(cmd api.Command, key string) ([]string, bool) {
|
||||
setting, found := cmd.Parameters[key]
|
||||
if !found {
|
||||
return []string{}, false
|
||||
}
|
||||
|
||||
if asStrSlice, ok := setting.([]string); ok {
|
||||
return asStrSlice, true
|
||||
}
|
||||
|
||||
interfSlice, ok := setting.([]interface{})
|
||||
if !ok {
|
||||
return []string{}, false
|
||||
}
|
||||
|
||||
strSlice := make([]string, len(interfSlice))
|
||||
for idx := range interfSlice {
|
||||
switch v := interfSlice[idx].(type) {
|
||||
case string:
|
||||
strSlice[idx] = v
|
||||
case fmt.Stringer:
|
||||
strSlice[idx] = v.String()
|
||||
default:
|
||||
strSlice[idx] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
return strSlice, true
|
||||
}
|
||||
|
65
internal/worker/command_exe_test.go
Normal file
65
internal/worker/command_exe_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package worker
|
||||
|
||||
/* ***** BEGIN GPL LICENSE BLOCK *****
|
||||
*
|
||||
* Original Code Copyright (C) 2022 Blender Foundation.
|
||||
*
|
||||
* This file is part of Flamenco.
|
||||
*
|
||||
* Flamenco is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* Flamenco is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* Flamenco. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* ***** END GPL LICENSE BLOCK ***** */
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/blender/flamenco-ng-poc/pkg/api"
|
||||
)
|
||||
|
||||
func TestCmdSettingAsStrings(t *testing.T) {
|
||||
cmd := api.Command{
|
||||
Name: "test",
|
||||
Parameters: map[string]interface{}{
|
||||
"strings": []string{"a", "b"},
|
||||
"ints": []int{3, 4},
|
||||
"floats": []float64{0.47, 0.327},
|
||||
"mixed": []interface{}{"a", 47, 0.327},
|
||||
},
|
||||
}
|
||||
|
||||
{
|
||||
slice, ok := cmdSettingAsStrings(cmd, "strings")
|
||||
if ok {
|
||||
assert.Equal(t, []string{"a", "b"}, slice)
|
||||
} else {
|
||||
t.Error("not ok")
|
||||
}
|
||||
}
|
||||
{
|
||||
_, ok := cmdSettingAsStrings(cmd, "ints")
|
||||
assert.False(t, ok, "only []string or []interface{} are expected to work")
|
||||
}
|
||||
{
|
||||
_, ok := cmdSettingAsStrings(cmd, "floats")
|
||||
assert.False(t, ok, "only []string or []interface{} are expected to work")
|
||||
}
|
||||
{
|
||||
slice, ok := cmdSettingAsStrings(cmd, "mixed")
|
||||
if ok {
|
||||
assert.Equal(t, []string{"a", "47", "0.327"}, slice)
|
||||
} else {
|
||||
t.Error("not ok")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user