Merge pull request #694 from ijc/apply-config-to-state

client/llb: factor out `State.WithConfig` from `Image`.
docker-18.09
Tõnis Tiigi 2018-10-18 13:26:16 -07:00 committed by GitHub
commit 54cf2b136d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 22 deletions

View File

@ -126,30 +126,11 @@ func Image(ref string, opts ...ImageOption) State {
if err != nil { if err != nil {
src.err = err src.err = err
} else { } else {
var img struct { st, err := NewState(src.Output()).WithImageConfig(dt)
Config struct { if err == nil {
Env []string `json:"Env,omitempty"`
WorkingDir string `json:"WorkingDir,omitempty"`
User string `json:"User,omitempty"`
} `json:"config,omitempty"`
}
if err := json.Unmarshal(dt, &img); err != nil {
src.err = err
} else {
st := NewState(src.Output())
for _, env := range img.Config.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts[0]) > 0 {
var v string
if len(parts) > 1 {
v = parts[1]
}
st = st.AddEnv(parts[0], v)
}
}
st = st.Dir(img.Config.WorkingDir)
return st return st
} }
src.err = err
} }
} }
return NewState(src.Output()) return NewState(src.Output())

View File

@ -2,8 +2,10 @@ package llb
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net" "net"
"strings"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/identity" "github.com/moby/buildkit/identity"
@ -171,6 +173,31 @@ func (s State) WithOutput(o Output) State {
return s return s
} }
func (s State) WithImageConfig(c []byte) (State, error) {
var img struct {
Config struct {
Env []string `json:"Env,omitempty"`
WorkingDir string `json:"WorkingDir,omitempty"`
User string `json:"User,omitempty"`
} `json:"config,omitempty"`
}
if err := json.Unmarshal(c, &img); err != nil {
return State{}, err
}
for _, env := range img.Config.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts[0]) > 0 {
var v string
if len(parts) > 1 {
v = parts[1]
}
s = s.AddEnv(parts[0], v)
}
}
s = s.Dir(img.Config.WorkingDir)
return s, nil
}
func (s State) Run(ro ...RunOption) ExecState { func (s State) Run(ro ...RunOption) ExecState {
ei := &ExecInfo{State: s} ei := &ExecInfo{State: s}
if p := s.GetPlatform(); p != nil { if p := s.GetPlatform(); p != nil {