-
Notifications
You must be signed in to change notification settings - Fork 250
/
providerworkerconfig.go
59 lines (50 loc) · 1.92 KB
/
providerworkerconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cfg
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/taskcluster/taskcluster/v33/tools/worker-runner/files"
)
// ProviderWorkerConfig handles the configuration format provided from
// worker-manager providers: `{<workerName>: {config, files}}`, including some
// compatibility translations described in the README.
type ProviderWorkerConfig struct {
Config *WorkerConfig `json:"config,omitempty"`
Files []files.File `json:"files,omitempty"`
}
// ParseProviderWorkerConfig takes a RawMessage representing the `workerConfig`
// field received from worker-manager, and returns the config and files it
// contains. This requires the runnerConfig in order to determine the worker
// implementation name.
func ParseProviderWorkerConfig(runnercfg *RunnerConfig, body *json.RawMessage) (ProviderWorkerConfig, error) {
var pwc ProviderWorkerConfig
if body == nil {
return pwc, nil
}
// calculate the camel-cased worker name
implSnakeCase := runnercfg.WorkerImplementation.Implementation
workerImpl := regexp.MustCompile("-[a-z]").ReplaceAllStringFunc(implSnakeCase, func(snake string) string { return strings.ToUpper(snake[1:]) })
// the "correct" format, then, is a single-key dictionary with that key
var bodyMap map[string]json.RawMessage
err := json.Unmarshal(*body, &bodyMap)
if err != nil {
return pwc, fmt.Errorf("While parsing workerConfig from worker-manager: %s", err)
}
if len(bodyMap) == 1 {
if inner, ok := bodyMap[workerImpl]; ok {
err = json.Unmarshal(inner, &pwc)
if err != nil {
return pwc, fmt.Errorf("While parsing workerConfig from worker-manager: %s", err)
}
return pwc, nil
}
}
// failing that, assume this is the deprecated "flat" form with configuration values at the top level
pwc.Config = NewWorkerConfig()
err = json.Unmarshal(*body, pwc.Config)
if err != nil {
return pwc, fmt.Errorf("While parsing workerConfig from worker-manager: %s", err)
}
return pwc, nil
}