From dcd9b60cf5f729f47c4c0e072703b585b6a9de17 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Tue, 13 Dec 2022 10:32:54 +0000 Subject: [PATCH] Add support for local default arg overrides --- README.md | 8 ++++++++ pkg/leeway/workspace.go | 23 ++++++++++++++++++++--- pkg/leeway/workspace_test.go | 33 +++++++++++++++++++++++++++++++++ pkg/testutil/testutil.go | 19 +++++++++++++++++-- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 572ae98..e243adb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pkg/leeway/workspace.go b/pkg/leeway/workspace.go index 7833113..f391093 100644 --- a/pkg/leeway/workspace.go +++ b/pkg/leeway/workspace.go @@ -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 { diff --git a/pkg/leeway/workspace_test.go b/pkg/leeway/workspace_test.go index a24a716..ee4bee3 100644 --- a/pkg/leeway/workspace_test.go +++ b/pkg/leeway/workspace_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/gitpod-io/leeway/pkg/leeway" "github.com/gitpod-io/leeway/pkg/testutil" ) @@ -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, diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go index da4f5c0..3e96dbc 100644 --- a/pkg/testutil/testutil.go +++ b/pkg/testutil/testutil.go @@ -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 { @@ -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)