build: handle --add-host

Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
Tibor Vass
2019-04-17 03:10:34 +00:00
parent dc07613bd2
commit 77ed999572
3 changed files with 30 additions and 5 deletions

View File

@ -3,7 +3,11 @@ package build
import (
"archive/tar"
"bytes"
"net"
"os"
"strings"
"github.com/pkg/errors"
)
// archiveHeaderSize is the number of bytes in an archive header
@ -32,3 +36,20 @@ func isArchive(header []byte) bool {
_, err := r.Next()
return err == nil
}
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
func toBuildkitExtraHosts(inp []string) (string, error) {
if len(inp) == 0 {
return "", nil
}
hosts := make([]string, 0, len(inp))
for _, h := range inp {
parts := strings.Split(h, ":")
if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
return "", errors.Errorf("invalid host %s", h)
}
hosts = append(hosts, parts[0]+"="+parts[1])
}
return strings.Join(hosts, ","), nil
}