-
Notifications
You must be signed in to change notification settings - Fork 18
/
helpers.go
116 lines (96 loc) · 2.85 KB
/
helpers.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package helpers
import (
"context"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/pkg/larker"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/local"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"strings"
)
var ErrConfigurationReadFailed = errors.New("failed to read configuration")
func ConsumeSubCommands(cmd *cobra.Command, subCommands []*cobra.Command) *cobra.Command {
var hasValidSubcommands bool
for _, subCommand := range subCommands {
if cmd == nil {
continue
}
cmd.AddCommand(subCommand)
hasValidSubcommands = true
}
if hasValidSubcommands {
return cmd
}
return nil
}
// envArgsToMap parses and expands environment arguments like "A=B" (set operation)
// and "A" (pass-through operation) into a map suitable for use across the codebase.
func EnvArgsToMap(arguments []string) map[string]string {
result := make(map[string]string)
const (
keyPart = iota
valuePart
maxParts
)
for _, argument := range arguments {
parts := strings.SplitN(argument, "=", maxParts)
if len(parts) == maxParts {
// Set mode: simply assign the provided value to key
result[parts[keyPart]] = parts[valuePart]
} else {
// Pass-through mode: resolve the value for the provided key and assign it (if any)
resolvedValue, found := os.LookupEnv(parts[keyPart])
if !found {
break
}
result[parts[keyPart]] = resolvedValue
}
}
return result
}
func ReadYAMLConfig(path string) (string, error) {
yamlConfig, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(yamlConfig), nil
}
func EvaluateStarlarkConfig(ctx context.Context, path string, env map[string]string) (string, error) {
starlarkSource, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
lrk := larker.New(larker.WithFileSystem(local.New(".")), larker.WithEnvironment(env))
return lrk.Main(ctx, string(starlarkSource))
}
func ReadCombinedConfig(ctx context.Context, env map[string]string) (string, error) {
// Here we read the .cirrus.yaml first so that if the error would arise
// and will be inspected it would indicate the preferable extension
yamlConfig, yamlErr := ReadYAMLConfig(".cirrus.yaml")
if yamlErr != nil {
if !os.IsNotExist(yamlErr) {
return "", yamlErr
}
yamlConfig, yamlErr = ReadYAMLConfig(".cirrus.yml")
if yamlErr != nil && !os.IsNotExist(yamlErr) {
return "", yamlErr
}
}
starlarkConfig, starlarkErr := EvaluateStarlarkConfig(ctx, ".cirrus.star", env)
if starlarkErr != nil && !os.IsNotExist(starlarkErr) {
return "", starlarkErr
}
switch {
case yamlErr == nil && starlarkErr == nil:
return yamlConfig + "\n" + starlarkConfig, nil
case yamlErr == nil:
return yamlConfig, nil
case starlarkErr == nil:
return starlarkConfig, nil
default:
return "", fmt.Errorf("%w: neither .cirrus.yml (%s) nor .cirrus.star were accessible (%s)",
ErrConfigurationReadFailed, yamlErr, starlarkErr)
}
}