Skip to content

Commit

Permalink
feat: interpret current arg to get current platform
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed Apr 30, 2024
1 parent b3cb647 commit 5a3d325
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/dagger/flags.go
Expand Up @@ -17,6 +17,7 @@ import (
"strconv"
"strings"

"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/util/gitutil"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -44,6 +45,8 @@ func GetCustomFlagValue(name string) DaggerValue {
return &moduleSourceValue{}
case Module:
return &moduleValue{}
case Platform:
return &platformValue{}
}
return nil
}
Expand All @@ -69,6 +72,8 @@ func GetCustomFlagValueSlice(name string) DaggerValue {
return &sliceValue[*moduleSourceValue]{}
case Module:
return &sliceValue[*moduleValue]{}
case Platform:
return &sliceValue[*platformValue]{}
}
return nil
}
Expand Down Expand Up @@ -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 Platform
}

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) {
Expand Down
1 change: 1 addition & 0 deletions cmd/dagger/functions.go
Expand Up @@ -30,6 +30,7 @@ const (
CacheVolume string = "CacheVolume"
ModuleSource string = "ModuleSource"
Module string = "Module"
Platform string = "Platform"
)

var funcGroup = &cobra.Group{
Expand Down
4 changes: 4 additions & 0 deletions core/integration/module_call_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/identity"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 5a3d325

Please sign in to comment.