From 6520dc2d66a1f183dd2fe334bdf0022f3f4824db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 10 Jan 2022 15:34:05 +0100 Subject: [PATCH] Fix some linter warnings No functional changes. --- internal/manager/api_impl/job_compiler.go | 9 ++--- internal/manager/api_impl/workers.go | 4 +-- internal/manager/job_compilers/author.go | 12 +++++-- .../manager/job_compilers/job_compilers.go | 23 ++++++------ internal/manager/job_compilers/js_globals.go | 36 +++++++++++++++++++ internal/manager/job_compilers/path.go | 17 ++++++--- internal/manager/job_compilers/process.go | 13 +++++-- 7 files changed, 87 insertions(+), 27 deletions(-) create mode 100644 internal/manager/job_compilers/js_globals.go diff --git a/internal/manager/api_impl/job_compiler.go b/internal/manager/api_impl/job_compiler.go index ce4cb9c3..2c3f4d7f 100644 --- a/internal/manager/api_impl/job_compiler.go +++ b/internal/manager/api_impl/job_compiler.go @@ -30,18 +30,15 @@ import ( func (f *Flamenco) GetJobTypes(e echo.Context) error { // Some helper functions because Go doesn't allow taking the address of a literal. defaultString := func(s string) *interface{} { - var iValue interface{} - iValue = s + var iValue interface{} = s return &iValue } defaultInt32 := func(i int32) *interface{} { - var iValue interface{} - iValue = i + var iValue interface{} = i return &iValue } defaultBool := func(b bool) *interface{} { - var iValue interface{} - iValue = b + var iValue interface{} = b return &iValue } boolPtr := func(b bool) *bool { diff --git a/internal/manager/api_impl/workers.go b/internal/manager/api_impl/workers.go index 2d1dbff7..28c8fd7a 100644 --- a/internal/manager/api_impl/workers.go +++ b/internal/manager/api_impl/workers.go @@ -58,8 +58,8 @@ func (f *Flamenco) ScheduleTask(e echo.Context) error { return e.JSON(http.StatusOK, &api.AssignedTask{ Id: uuid.New().String(), Commands: []api.Command{ - {"echo", echo.Map{"payload": "Simon says \"Shaders!\""}}, - {"blender", echo.Map{"blender_cmd": "/shared/bin/blender"}}, + {Name: "echo", Settings: echo.Map{"payload": "Simon says \"Shaders!\""}}, + {Name: "blender", Settings: echo.Map{"blender_cmd": "/shared/bin/blender"}}, }, Job: uuid.New().String(), JobPriority: 50, diff --git a/internal/manager/job_compilers/author.go b/internal/manager/job_compilers/author.go index cf1d9026..e88c5727 100644 --- a/internal/manager/job_compilers/author.go +++ b/internal/manager/job_compilers/author.go @@ -76,13 +76,21 @@ func (a *Author) Command(cmdType string, parameters map[string]string) (*Authore return &ac, nil } +// AuthorModule exports the Author module members to Goja. func AuthorModule(r *goja.Runtime, module *goja.Object) { a := &Author{ runtime: r, } obj := module.Get("exports").(*goja.Object) - obj.Set("Task", a.Task) - obj.Set("Command", a.Command) + mustExport := func(name string, value interface{}) { + err := obj.Set(name, value) + if err != nil { + log.Panic().Err(err).Msgf("unable to register '%s' in Goja 'author' module", name) + } + } + + mustExport("Task", a.Task) + mustExport("Command", a.Command) } func (aj *AuthoredJob) AddTask(at *AuthoredTask) { diff --git a/internal/manager/job_compilers/job_compilers.go b/internal/manager/job_compilers/job_compilers.go index 2aaac930..0e3600ac 100644 --- a/internal/manager/job_compilers/job_compilers.go +++ b/internal/manager/job_compilers/job_compilers.go @@ -75,21 +75,22 @@ func (c *GojaJobCompiler) newGojaVM() *goja.Runtime { vm := goja.New() vm.SetFieldNameMapper(goja.UncapFieldNameMapper()) + mustSet := func(name string, value interface{}) { + err := vm.Set(name, value) + if err != nil { + log.Panic().Err(err).Msgf("unable to register '%s' in Goja VM", name) + } + } + // Set some global functions for script debugging purposes. - vm.Set("print", func(call goja.FunctionCall) goja.Value { - log.Info().Interface("args", call.Arguments).Msg("print") - return goja.Undefined() - }) - vm.Set("alert", func(call goja.FunctionCall) goja.Value { - log.Warn().Interface("args", call.Arguments).Msg("alert") - return goja.Undefined() - }) + mustSet("print", jsPrint) + mustSet("alert", jsAlert) // Pre-import some useful modules. c.registry.Enable(vm) - vm.Set("author", require.Require(vm, "author")) - vm.Set("path", require.Require(vm, "path")) - vm.Set("process", require.Require(vm, "process")) + mustSet("author", require.Require(vm, "author")) + mustSet("path", require.Require(vm, "path")) + mustSet("process", require.Require(vm, "process")) return vm } diff --git a/internal/manager/job_compilers/js_globals.go b/internal/manager/job_compilers/js_globals.go new file mode 100644 index 00000000..834425d2 --- /dev/null +++ b/internal/manager/job_compilers/js_globals.go @@ -0,0 +1,36 @@ +package job_compilers + +/* ***** 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 . + * + * ***** END GPL LICENSE BLOCK ***** */ + +import ( + "github.com/dop251/goja" + "github.com/rs/zerolog/log" +) + +func jsPrint(call goja.FunctionCall) goja.Value { + log.Info().Interface("args", call.Arguments).Msg("print") + return goja.Undefined() +} + +func jsAlert(call goja.FunctionCall) goja.Value { + log.Warn().Interface("args", call.Arguments).Msg("alert") + return goja.Undefined() +} diff --git a/internal/manager/job_compilers/path.go b/internal/manager/job_compilers/path.go index ea8e10ea..2d11a730 100644 --- a/internal/manager/job_compilers/path.go +++ b/internal/manager/job_compilers/path.go @@ -24,15 +24,24 @@ import ( "path/filepath" "github.com/dop251/goja" + "github.com/rs/zerolog/log" ) // PathModule provides file path manipulation functions by wrapping Go's `path`. func PathModule(r *goja.Runtime, module *goja.Object) { obj := module.Get("exports").(*goja.Object) - obj.Set("basename", filepath.Base) - obj.Set("dirname", filepath.Dir) - obj.Set("join", filepath.Join) - obj.Set("stem", Stem) + + mustExport := func(name string, value interface{}) { + err := obj.Set(name, value) + if err != nil { + log.Panic().Err(err).Msgf("unable to register '%s' in Goja 'path' module", name) + } + } + + mustExport("basename", filepath.Base) + mustExport("dirname", filepath.Dir) + mustExport("join", filepath.Join) + mustExport("stem", Stem) } func Stem(fpath string) string { diff --git a/internal/manager/job_compilers/process.go b/internal/manager/job_compilers/process.go index d16f4d78..329d90cc 100644 --- a/internal/manager/job_compilers/process.go +++ b/internal/manager/job_compilers/process.go @@ -25,6 +25,7 @@ import ( "runtime" "github.com/dop251/goja" + "github.com/rs/zerolog/log" ) // Process implements a subset of the built-in NodeJS process object. @@ -43,10 +44,18 @@ func ProcessModule(r *goja.Runtime, module *goja.Object) { runtime: r, } obj := module.Get("exports").(*goja.Object) - obj.Set("cwd", p.cwd) + + mustExport := func(name string, value interface{}) { + err := obj.Set(name, value) + if err != nil { + log.Panic().Err(err).Msgf("unable to register '%s' in Goja 'process' module", name) + } + } + + mustExport("cwd", p.cwd) // To get a list of possible values of runtime.GOOS, run `go tool dist list`. // The NodeJS values are documented on https://nodejs.org/api/process.html#processplatform // Both lists are equal enough to just use runtime.GOOS here. - obj.Set("platform", runtime.GOOS) + mustExport("platform", runtime.GOOS) }