Skip to content

Commit

Permalink
Add support for local default arg overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Dec 13, 2022
1 parent 7858d75 commit dcd9b60
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ defaultArgs:
key: value
```
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.
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`.

```YAML
foo: bar
key: value
```

## Component
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:
```YAML
Expand Down
23 changes: 20 additions & 3 deletions pkg/leeway/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,33 @@ func loadWorkspace(ctx context.Context, path string, args Arguments, variant str
workspace.ignores = ignores
log.WithField("ignores", workspace.ignores).Debug("computed workspace ignores")

if workspace.ArgumentDefaults == nil {
workspace.ArgumentDefaults = make(map[string]string)
}
if len(opts.ArgumentDefaults) > 0 {
if workspace.ArgumentDefaults == nil {
workspace.ArgumentDefaults = make(map[string]string)
}
for k, v := range opts.ArgumentDefaults {
workspace.ArgumentDefaults[k] = v
}
log.WithField("rootDefaultArgs", opts.ArgumentDefaults).Debug("installed root workspace defaults")
}

defaultArgsFN := filepath.Join(path, "WORKSPACE.args.yaml")
if fc, err := os.ReadFile(defaultArgsFN); err == nil {
defargs := make(map[string]string)
err = yaml.Unmarshal(fc, &defargs)
if err != nil {
return Workspace{}, xerrors.Errorf("cannot unmarshal %s: %w", defaultArgsFN, err)
}
for k, v := range defargs {
workspace.ArgumentDefaults[k] = v
}
log.WithField("content", defargs).WithField("filename", defaultArgsFN).Debug("applied workspace default args file")
} else if os.IsNotExist(err) {
// ignore
} else {
return Workspace{}, xerrors.Errorf("cannot read %s: %w", defaultArgsFN, err)
}

log.WithField("defaultArgs", workspace.ArgumentDefaults).Debug("applying workspace defaults")
for key, val := range workspace.ArgumentDefaults {
if args == nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/leeway/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/gitpod-io/leeway/pkg/leeway"
"github.com/gitpod-io/leeway/pkg/testutil"
)

Expand All @@ -33,6 +34,38 @@ func TestFixtureLoadWorkspace(t *testing.T) {
StdoutSubs: []string{"deeper/pkg0\nwsa\nwsa/pkg0\nwsa/pkg1"},
FixturePath: "fixtures/load-workspace.yaml",
},
{
Name: "workspace args file",
T: t,
Args: []string{"describe", "comp:pkg"},
NoNestedWorkspace: true,
ExitCode: 0,
StdoutSubs: []string{"foobar"},
Fixture: &testutil.Setup{
Files: map[string]string{"WORKSPACE.args.yaml": "msg: foobar"},
Workspace: leeway.Workspace{
ArgumentDefaults: map[string]string{
"msg": "blabla",
},
},
Components: []testutil.Component{
{
Location: "comp",
Packages: []leeway.Package{
{
PackageInternal: leeway.PackageInternal{
Name: "pkg",
Type: leeway.GenericPackage,
},
Config: leeway.GenericPkgConfig{
Commands: [][]string{{"echo", "${msg}"}},
},
},
},
},
},
},
},
{
Name: "environment manifest",
T: t,
Expand Down
19 changes: 17 additions & 2 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

type Setup struct {
Workspace leeway.Workspace `yaml:"workspace"`
Components []Component `yaml:"components"`
Workspace leeway.Workspace `yaml:"workspace"`
Components []Component `yaml:"components"`
Files map[string]string `yaml:"files"`
}

type Component struct {
Expand Down Expand Up @@ -60,6 +61,20 @@ func (s Setup) Materialize() (workspaceRoot string, err error) {
if err != nil {
return
}
for fn, content := range s.Files {
fn = filepath.Join(workspaceRoot, fn)
err = os.MkdirAll(filepath.Dir(fn), 0755)
if errors.Is(err, os.ErrExist) {
err = nil
}
if err != nil {
return
}
err = os.WriteFile(fn, []byte(content), 0644)
if err != nil {
return
}
}

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

0 comments on commit dcd9b60

Please sign in to comment.