Skip to content

Commit dcd9b60

Browse files
committed
Add support for local default arg overrides
1 parent 7858d75 commit dcd9b60

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ defaultArgs:
3737
key: value
3838
```
3939
40+
Users can override, and provide additional default arguments using a `WORKSPACE.args.yaml` file in the workspace root. This is useful for providing local overrides which you might not want to commit to Git.
41+
The `WORKSPACE.args.yaml` takes key value pairs which become available as build arguments. The values herein take precedence over the default arguments in the `WORKSPACE.yaml`.
42+
43+
```YAML
44+
foo: bar
45+
key: value
46+
```
47+
4048
## Component
4149
Place a `BUILD.yaml` in a folder somewhere in the workspace to make that folder a component. A `BUILD.yaml` primarily contains the packages of that components, but can also contain constant values (think of them as metadata). For example:
4250
```YAML

pkg/leeway/workspace.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,33 @@ func loadWorkspace(ctx context.Context, path string, args Arguments, variant str
227227
workspace.ignores = ignores
228228
log.WithField("ignores", workspace.ignores).Debug("computed workspace ignores")
229229

230+
if workspace.ArgumentDefaults == nil {
231+
workspace.ArgumentDefaults = make(map[string]string)
232+
}
230233
if len(opts.ArgumentDefaults) > 0 {
231-
if workspace.ArgumentDefaults == nil {
232-
workspace.ArgumentDefaults = make(map[string]string)
233-
}
234234
for k, v := range opts.ArgumentDefaults {
235235
workspace.ArgumentDefaults[k] = v
236236
}
237237
log.WithField("rootDefaultArgs", opts.ArgumentDefaults).Debug("installed root workspace defaults")
238238
}
239239

240+
defaultArgsFN := filepath.Join(path, "WORKSPACE.args.yaml")
241+
if fc, err := os.ReadFile(defaultArgsFN); err == nil {
242+
defargs := make(map[string]string)
243+
err = yaml.Unmarshal(fc, &defargs)
244+
if err != nil {
245+
return Workspace{}, xerrors.Errorf("cannot unmarshal %s: %w", defaultArgsFN, err)
246+
}
247+
for k, v := range defargs {
248+
workspace.ArgumentDefaults[k] = v
249+
}
250+
log.WithField("content", defargs).WithField("filename", defaultArgsFN).Debug("applied workspace default args file")
251+
} else if os.IsNotExist(err) {
252+
// ignore
253+
} else {
254+
return Workspace{}, xerrors.Errorf("cannot read %s: %w", defaultArgsFN, err)
255+
}
256+
240257
log.WithField("defaultArgs", workspace.ArgumentDefaults).Debug("applying workspace defaults")
241258
for key, val := range workspace.ArgumentDefaults {
242259
if args == nil {

pkg/leeway/workspace_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/gitpod-io/leeway/pkg/leeway"
1112
"github.com/gitpod-io/leeway/pkg/testutil"
1213
)
1314

@@ -33,6 +34,38 @@ func TestFixtureLoadWorkspace(t *testing.T) {
3334
StdoutSubs: []string{"deeper/pkg0\nwsa\nwsa/pkg0\nwsa/pkg1"},
3435
FixturePath: "fixtures/load-workspace.yaml",
3536
},
37+
{
38+
Name: "workspace args file",
39+
T: t,
40+
Args: []string{"describe", "comp:pkg"},
41+
NoNestedWorkspace: true,
42+
ExitCode: 0,
43+
StdoutSubs: []string{"foobar"},
44+
Fixture: &testutil.Setup{
45+
Files: map[string]string{"WORKSPACE.args.yaml": "msg: foobar"},
46+
Workspace: leeway.Workspace{
47+
ArgumentDefaults: map[string]string{
48+
"msg": "blabla",
49+
},
50+
},
51+
Components: []testutil.Component{
52+
{
53+
Location: "comp",
54+
Packages: []leeway.Package{
55+
{
56+
PackageInternal: leeway.PackageInternal{
57+
Name: "pkg",
58+
Type: leeway.GenericPackage,
59+
},
60+
Config: leeway.GenericPkgConfig{
61+
Commands: [][]string{{"echo", "${msg}"}},
62+
},
63+
},
64+
},
65+
},
66+
},
67+
},
68+
},
3669
{
3770
Name: "environment manifest",
3871
T: t,

pkg/testutil/testutil.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
type Setup struct {
20-
Workspace leeway.Workspace `yaml:"workspace"`
21-
Components []Component `yaml:"components"`
20+
Workspace leeway.Workspace `yaml:"workspace"`
21+
Components []Component `yaml:"components"`
22+
Files map[string]string `yaml:"files"`
2223
}
2324

2425
type Component struct {
@@ -60,6 +61,20 @@ func (s Setup) Materialize() (workspaceRoot string, err error) {
6061
if err != nil {
6162
return
6263
}
64+
for fn, content := range s.Files {
65+
fn = filepath.Join(workspaceRoot, fn)
66+
err = os.MkdirAll(filepath.Dir(fn), 0755)
67+
if errors.Is(err, os.ErrExist) {
68+
err = nil
69+
}
70+
if err != nil {
71+
return
72+
}
73+
err = os.WriteFile(fn, []byte(content), 0644)
74+
if err != nil {
75+
return
76+
}
77+
}
6378

6479
for _, comp := range s.Components {
6580
err = os.MkdirAll(filepath.Join(workspaceRoot, comp.Location), 0755)

0 commit comments

Comments
 (0)