2017-06-26 04:10:02 +00:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/shlex"
|
|
|
|
)
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type contextKeyT string
|
2017-06-26 04:10:02 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
var (
|
|
|
|
keyArgs = contextKeyT("llb.exec.args")
|
|
|
|
keyDir = contextKeyT("llb.exec.dir")
|
|
|
|
keyEnv = contextKeyT("llb.exec.env")
|
|
|
|
)
|
2017-07-09 23:58:29 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func addEnv(key, value string) StateOption {
|
2017-07-09 23:58:29 +00:00
|
|
|
return addEnvf(key, value)
|
2017-06-26 18:51:27 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
func addEnvf(key, value string, v ...interface{}) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyEnv, getEnv(s).AddOrReplace(key, fmt.Sprintf(value, v...)))
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
func clearEnv() StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyEnv, EnvList{})
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
func delEnv(key string) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyEnv, getEnv(s).Delete(key))
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func dir(str string) StateOption {
|
|
|
|
return dirf(str)
|
|
|
|
}
|
|
|
|
func dirf(str string, v ...interface{}) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyDir, fmt.Sprintf(str, v...))
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func reset(s_ State) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
s = NewState(s.Output())
|
|
|
|
s.ctx = s_.ctx
|
|
|
|
return s
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func getEnv(s State) EnvList {
|
|
|
|
v := s.Value(keyEnv)
|
|
|
|
if v != nil {
|
|
|
|
return v.(EnvList)
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
return EnvList{}
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func getDir(s State) string {
|
|
|
|
v := s.Value(keyDir)
|
|
|
|
if v != nil {
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
return ""
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func getArgs(s State) []string {
|
|
|
|
v := s.Value(keyArgs)
|
|
|
|
if v != nil {
|
|
|
|
return v.([]string)
|
|
|
|
}
|
|
|
|
return nil
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func args(args ...string) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyArgs, args)
|
|
|
|
}
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func shlexf(str string, v ...interface{}) StateOption {
|
|
|
|
return func(s State) State {
|
2017-07-09 23:58:29 +00:00
|
|
|
arg, err := shlex.Split(fmt.Sprintf(str, v...))
|
2017-06-26 04:10:02 +00:00
|
|
|
if err != nil {
|
2017-07-23 17:33:19 +00:00
|
|
|
// TODO: handle error
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
return args(arg...)(s)
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type EnvList []KeyValue
|
2017-06-26 04:10:02 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type KeyValue struct {
|
2017-06-26 04:10:02 +00:00
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (e EnvList) AddOrReplace(k, v string) EnvList {
|
2017-06-26 04:10:02 +00:00
|
|
|
e = e.Delete(k)
|
2017-07-23 17:33:19 +00:00
|
|
|
e = append(e, KeyValue{key: k, value: v})
|
2017-06-26 04:10:02 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (e EnvList) Delete(k string) EnvList {
|
|
|
|
e = append([]KeyValue(nil), e...)
|
|
|
|
if i, ok := e.Index(k); ok {
|
2017-06-26 04:10:02 +00:00
|
|
|
return append(e[:i], e[i+1:]...)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (e EnvList) Get(k string) (string, bool) {
|
|
|
|
if index, ok := e.Index(k); ok {
|
2017-06-26 04:10:02 +00:00
|
|
|
return e[index].value, true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (e EnvList) Index(k string) (int, bool) {
|
2017-06-26 04:10:02 +00:00
|
|
|
for i, kv := range e {
|
|
|
|
if kv.key == k {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (e EnvList) ToArray() []string {
|
2017-06-26 04:10:02 +00:00
|
|
|
out := make([]string, 0, len(e))
|
|
|
|
for _, kv := range e {
|
|
|
|
out = append(out, kv.key+"="+kv.value)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|