From 7567e990d24481eeafc02b81de57a784d3851806 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 24 Apr 2024 12:42:36 +0100 Subject: [PATCH] feat: interpret current arg to get current platform Signed-off-by: Justin Chadwell --- cmd/dagger/flags.go | 35 ++++++++++++++++++++++++++++ cmd/dagger/functions.go | 1 + core/integration/module_call_test.go | 4 ++++ 3 files changed, 40 insertions(+) diff --git a/cmd/dagger/flags.go b/cmd/dagger/flags.go index a8d9e903d87..dd9b4408a4b 100644 --- a/cmd/dagger/flags.go +++ b/cmd/dagger/flags.go @@ -17,6 +17,7 @@ import ( "strconv" "strings" + "github.com/containerd/containerd/platforms" "github.com/moby/buildkit/util/gitutil" "github.com/spf13/pflag" @@ -44,6 +45,8 @@ func GetCustomFlagValue(name string) DaggerValue { return &moduleSourceValue{} case Module: return &moduleValue{} + case Platform: + return &platformValue{} } return nil } @@ -69,6 +72,8 @@ func GetCustomFlagValueSlice(name string) DaggerValue { return &sliceValue[*moduleSourceValue]{} case Module: return &sliceValue[*moduleValue]{} + case Platform: + return &sliceValue[*platformValue]{} } return nil } @@ -592,6 +597,36 @@ func (v *moduleSourceValue) Get(ctx context.Context, dag *dagger.Client, _ *dagg return modConf.Source, nil } +type platformValue struct { + platform string +} + +func (v *platformValue) Type() string { + return Module +} + +func (v *platformValue) Set(s string) error { + if s == "" { + return fmt.Errorf("platform cannot be empty") + } + if s == "current" { + s = platforms.DefaultString() + } + v.platform = s + return nil +} + +func (v *platformValue) String() string { + return v.platform +} + +func (v *platformValue) Get(ctx context.Context, dag *dagger.Client, _ *dagger.ModuleSource) (any, error) { + if v.platform == "" { + return nil, fmt.Errorf("platform cannot be empty") + } + return v.platform, nil +} + // AddFlag adds a flag appropriate for the argument type. Should return a // pointer to the value. func (r *modFunctionArg) AddFlag(flags *pflag.FlagSet, dag *dagger.Client) (any, error) { diff --git a/cmd/dagger/functions.go b/cmd/dagger/functions.go index f2e422b6e57..e5b9dc1208d 100644 --- a/cmd/dagger/functions.go +++ b/cmd/dagger/functions.go @@ -30,6 +30,7 @@ const ( CacheVolume string = "CacheVolume" ModuleSource string = "ModuleSource" Module string = "Module" + Platform string = "Platform" ) var funcGroup = &cobra.Group{ diff --git a/core/integration/module_call_test.go b/core/integration/module_call_test.go index 4a5003ffa85..4bc46f909f5 100644 --- a/core/integration/module_call_test.go +++ b/core/integration/module_call_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + "github.com/containerd/containerd/platforms" "github.com/moby/buildkit/identity" "github.com/stretchr/testify/require" @@ -526,6 +527,9 @@ func (m *Test) ToPlatform(platform string) Platform { out, err := modGen.With(daggerCall("from-platform", "--platform", "linux/amd64")).Stdout(ctx) require.NoError(t, err) require.Equal(t, "linux/amd64", out) + out, err = modGen.With(daggerCall("from-platform", "--platform", "current")).Stdout(ctx) + require.NoError(t, err) + require.Equal(t, platforms.DefaultString(), out) _, err = modGen.With(daggerCall("from-platform", "--platform", "invalid")).Stdout(ctx) require.ErrorContains(t, err, "unknown operating system or architecture")