-
Notifications
You must be signed in to change notification settings - Fork 386
/
features.go
235 lines (209 loc) · 8.96 KB
/
features.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package features
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
goflags "github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"github.com/earthly/earthly/analytics"
"github.com/earthly/earthly/ast/spec"
"github.com/earthly/earthly/util/flagutil"
)
// Features is used to denote which features to flip on or off; this is for use in maintaining
// backwards compatibility
type Features struct {
ReferencedSaveOnly bool `long:"referenced-save-only" description:"only save artifacts that are directly referenced"`
UseCopyIncludePatterns bool `long:"use-copy-include-patterns" description:"specify an include pattern to buildkit when performing copies"`
ForIn bool `long:"for-in" description:"allow the use of the FOR command"`
TryFinally bool `long:"try" description:"allow the use of the TRY/FINALLY commands"`
RequireForceForUnsafeSaves bool `long:"require-force-for-unsafe-saves" description:"require the --force flag when saving to path outside of current path"`
NoImplicitIgnore bool `long:"no-implicit-ignore" description:"disable implicit ignore rules to exclude .tmp-earthly-out/, build.earth, Earthfile, .earthignore and .earthlyignore when resolving local context"`
CheckDuplicateImages bool `long:"check-duplicate-images" description:"check for duplicate images during output"`
EarthlyVersionArg bool `long:"earthly-version-arg" description:"includes EARTHLY_VERSION and EARTHLY_BUILD_SHA ARGs"`
EarthlyLocallyArg bool `long:"earthly-locally-arg" description:"includes EARTHLY_LOCALLY ARG"`
EarthlyGitAuthorArgs bool `long:"earthly-git-author-args" description:"includes EARTHLY_GIT_AUTHOR and EARTHLY_GIT_CO_AUTHORS ARGs"`
ExplicitGlobal bool `long:"explicit-global" description:"require base target args to have explicit settings to be considered global args"`
UseCacheCommand bool `long:"use-cache-command" description:"allow use of CACHE command in Earthfiles"`
UseHostCommand bool `long:"use-host-command" description:"allow use of HOST command in Earthfiles"`
ExecAfterParallel bool `long:"exec-after-parallel" description:"force execution after parallel conversion"`
UseCopyLink bool `long:"use-copy-link" description:"use the equivalent of COPY --link for all copy-like operations"`
ParallelLoad bool `long:"parallel-load" description:"perform parallel loading of images into WITH DOCKER"`
NoTarBuildOutput bool `long:"no-tar-build-output" description:"do not print output when creating a tarball to load into WITH DOCKER"`
ShellOutAnywhere bool `long:"shell-out-anywhere" description:"allow shelling-out in the middle of ARGs, or any other command"`
NewPlatform bool `long:"new-platform" description:"enable new platform behavior"`
UseNoManifestList bool `long:"use-no-manifest-list" description:"enable the SAVE IMAGE --no-manifest-list option"`
UseChmod bool `long:"use-chmod" description:"enable the SAVE IMAGE --no-manifest-list option"`
UseRegistryForWithDocker bool `long:"use-registry-for-with-docker" description:"use embedded Docker registry for WITH DOCKER load operations"`
WaitBlock bool `long:"wait-block" description:"enable WITH/END feature, also allows RUN --push mixed with non-push commands"`
UseProjectSecrets bool `long:"use-project-secrets" description:"enable project-based secret resolution"`
UsePipelines bool `long:"use-pipelines" description:"enable the PIPELINE and TRIGGER commands"`
NoUseRegistryForWithDocker bool `long:"no-use-registry-for-with-docker" description:"disable use-registry-for-with-docker"`
Major int
Minor int
}
// Version returns the current version
func (f *Features) Version() string {
return fmt.Sprintf("%d.%d", f.Major, f.Minor)
}
func parseFlagOverrides(env string) map[string]string {
env = strings.TrimSpace(env)
m := map[string]string{}
if env != "" {
for _, flag := range strings.Split(env, ",") {
flagNameAndValue := strings.SplitN(flag, "=", 2)
var flagValue string
flagName := strings.TrimSpace(flagNameAndValue[0])
flagName = strings.TrimPrefix(flagName, "--")
if len(flagNameAndValue) > 1 {
flagValue = strings.TrimSpace(flagNameAndValue[1])
}
m[flagName] = flagValue
}
}
return m
}
// String returns a string representation of the version and set flags
func (f *Features) String() string {
if f == nil {
return "<nil>"
}
v := reflect.ValueOf(*f)
typeOf := v.Type()
flags := []string{}
for i := 0; i < typeOf.NumField(); i++ {
tag := typeOf.Field(i).Tag
if flagName, ok := tag.Lookup("long"); ok {
ifaceVal := v.Field(i).Interface()
if boolVal, ok := ifaceVal.(bool); ok && boolVal {
flags = append(flags, fmt.Sprintf("--%v", flagName))
}
}
}
sort.Strings(flags)
args := []string{"VERSION"}
if len(flags) > 0 {
args = append(args, strings.Join(flags, " "))
}
args = append(args, fmt.Sprintf("%d.%d", f.Major, f.Minor))
return strings.Join(args, " ")
}
// ApplyFlagOverrides parses a comma separated list of feature flag overrides (without the -- flag name prefix)
// and sets them in the referenced features.
func ApplyFlagOverrides(ftrs *Features, envOverrides string) error {
overrides := parseFlagOverrides(envOverrides)
fieldIndices := map[string]int{}
typeOf := reflect.ValueOf(*ftrs).Type()
for i := 0; i < typeOf.NumField(); i++ {
f := typeOf.Field(i)
tag := f.Tag
if flagName, ok := tag.Lookup("long"); ok {
fieldIndices[flagName] = i
}
}
ftrsStruct := reflect.ValueOf(ftrs).Elem()
for key := range overrides {
analytics.Count("override-feature-flags", key)
i, ok := fieldIndices[key]
if !ok {
return fmt.Errorf("unable to set %s: invalid flag", key)
}
fv := ftrsStruct.Field(i)
if fv.IsValid() && fv.CanSet() {
fv.SetBool(true)
} else {
return fmt.Errorf("unable to set %s: field is invalid or cant be set", key)
}
ifaceVal := fv.Interface()
if _, ok := ifaceVal.(bool); ok {
fv.SetBool(true)
} else {
return fmt.Errorf("unable to set %s: only boolean fields are currently supported", key)
}
}
processNegativeFlags(ftrs)
return nil
}
var errUnexpectedArgs = fmt.Errorf("unexpected VERSION arguments; should be VERSION [flags] <major-version>.<minor-version>")
func instrumentVersion(_ string, opt *goflags.Option, s *string) (*string, error) {
analytics.Count("version-feature-flags", opt.LongName)
return s, nil // don't modify the flag, just pass it back.
}
// GetFeatures returns a features struct for a particular version
func GetFeatures(version *spec.Version) (*Features, bool, error) {
var ftrs Features
hasVersion := (version != nil)
if !hasVersion {
// If no version is specified, we default to 0.5 (the Earthly version
// before the VERSION command was introduced).
version = &spec.Version{
Args: []string{"0.5"},
}
}
if version.Args == nil {
return nil, false, errUnexpectedArgs
}
parsedArgs, err := flagutil.ParseArgsWithValueModifierAndOptions("VERSION", &ftrs, version.Args, instrumentVersion, goflags.PassDoubleDash|goflags.PassAfterNonOption|goflags.AllowBoolValues)
if err != nil {
return nil, false, err
}
if len(parsedArgs) != 1 {
return nil, false, errUnexpectedArgs
}
versionValueStr := parsedArgs[0]
majorAndMinor := strings.Split(versionValueStr, ".")
if len(majorAndMinor) != 2 {
return nil, false, errUnexpectedArgs
}
ftrs.Major, err = strconv.Atoi(majorAndMinor[0])
if err != nil {
return nil, false, errors.Wrapf(err, "failed to parse major version %q", majorAndMinor[0])
}
ftrs.Minor, err = strconv.Atoi(majorAndMinor[1])
if err != nil {
return nil, false, errors.Wrapf(err, "failed to parse minor version %q", majorAndMinor[1])
}
if hasVersion {
analytics.Count("version", versionValueStr)
} else {
analytics.Count("version", "missing")
}
// Enable version-specific features.
if versionAtLeast(ftrs, 0, 5) {
ftrs.ExecAfterParallel = true
ftrs.ParallelLoad = true
ftrs.UseRegistryForWithDocker = true
}
if versionAtLeast(ftrs, 0, 6) {
ftrs.ReferencedSaveOnly = true
ftrs.UseCopyIncludePatterns = true
ftrs.ForIn = true
ftrs.RequireForceForUnsafeSaves = true
ftrs.NoImplicitIgnore = true
}
if versionAtLeast(ftrs, 0, 7) {
ftrs.ExplicitGlobal = true
ftrs.CheckDuplicateImages = true
ftrs.EarthlyVersionArg = true
ftrs.UseCacheCommand = true
ftrs.UseHostCommand = true
ftrs.UseCopyLink = true
ftrs.NewPlatform = true
ftrs.NoTarBuildOutput = true
ftrs.UseNoManifestList = true
ftrs.UseChmod = true
}
processNegativeFlags(&ftrs)
return &ftrs, hasVersion, nil
}
// versionAtLeast returns true if the version configured in `ftrs`
// are greater than or equal to the provided major and minor versions.
func versionAtLeast(ftrs Features, majorVersion, minorVersion int) bool {
return (ftrs.Major > majorVersion) || (ftrs.Major == majorVersion && ftrs.Minor >= minorVersion)
}
func processNegativeFlags(ftrs *Features) {
if ftrs.NoUseRegistryForWithDocker {
ftrs.UseRegistryForWithDocker = false
}
}