Automatically set target platform based on client architecture

Set the default platform based on the client's OS and architecture. This
function is the same one that buildkit uses (https://github.com/moby/buildkit/blob/master/frontend/dockerfile/builder/build.go#L100-L102) to set the default build target platform

Signed-off-by: Marcos Lilljedahl <marcosnils@gmail.com>
This commit is contained in:
Marcos Lilljedahl
2022-04-05 12:28:53 -03:00
parent cc79934c18
commit 34c7a2ff12
4 changed files with 46 additions and 17 deletions

View File

@@ -17,9 +17,7 @@ type Context struct {
func New() *Context {
return &Context{
Platform: &platformContext{
platform: defaultPlatform,
},
Platform: &platformContext{},
FS: &fsContext{
store: make(map[string]*FS),
},

View File

@@ -5,28 +5,27 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1"
)
var (
// Default platform.
// FIXME: This should be auto detected using buildkit
defaultPlatform = specs.Platform{
OS: "linux",
Architecture: "amd64",
}
)
type platformContext struct {
platform specs.Platform
platform *specs.Platform
}
func (c *platformContext) Get() specs.Platform {
return c.platform
return *c.platform
}
func (c *platformContext) Set(platform string) error {
func (c *platformContext) SetString(platform string) error {
p, err := platforms.Parse(platform)
if err != nil {
return err
}
c.platform = p
c.platform = &p
return nil
}
func (c *platformContext) Set(p specs.Platform) {
c.platform = &p
}
func (c *platformContext) IsSet() bool {
return c.platform != nil
}