mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-19 17:26:28 +00:00
cli: Move "build" flag processing into build.go
Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
parent
317e48b9dd
commit
bc5e4c8a14
@ -4,73 +4,42 @@ import (
|
|||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/moby/src/initrd"
|
"github.com/docker/moby/src/initrd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func untarKernel(buf *bytes.Buffer, bzimageName, ktarName string) (*bytes.Buffer, *bytes.Buffer, error) {
|
// Process the build arguments and execute build
|
||||||
tr := tar.NewReader(buf)
|
func build(args []string) {
|
||||||
|
buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
|
||||||
var bzimage, ktar *bytes.Buffer
|
buildCmd.Usage = func() {
|
||||||
|
fmt.Printf("USAGE: %s build [options] [file.yml]\n\n", os.Args[0])
|
||||||
for {
|
fmt.Printf("'file.yml' defaults to 'moby.yml' if not specified.\n\n")
|
||||||
hdr, err := tr.Next()
|
fmt.Printf("Options:\n")
|
||||||
if err == io.EOF {
|
buildCmd.PrintDefaults()
|
||||||
break
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
switch hdr.Name {
|
|
||||||
case bzimageName:
|
|
||||||
bzimage = new(bytes.Buffer)
|
|
||||||
_, err := io.Copy(bzimage, tr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
case ktarName:
|
|
||||||
ktar = new(bytes.Buffer)
|
|
||||||
_, err := io.Copy(bzimage, tr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
buildName := buildCmd.String("name", "", "Name to use for output files")
|
||||||
|
buildPull := buildCmd.Bool("pull", false, "Always pull images")
|
||||||
|
|
||||||
if ktar == nil || bzimage == nil {
|
buildCmd.Parse(args)
|
||||||
return nil, nil, errors.New("did not find bzImage and kernel.tar in tarball")
|
remArgs := buildCmd.Args()
|
||||||
}
|
|
||||||
|
|
||||||
return bzimage, ktar, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func containersInitrd(containers []*bytes.Buffer) (*bytes.Buffer, error) {
|
|
||||||
w := new(bytes.Buffer)
|
|
||||||
iw := initrd.NewWriter(w)
|
|
||||||
defer iw.Close()
|
|
||||||
for _, file := range containers {
|
|
||||||
_, err := initrd.Copy(iw, file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return w, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func build(name string, pull bool, args []string) {
|
|
||||||
conf := "moby.yml"
|
conf := "moby.yml"
|
||||||
if len(args) > 0 {
|
if len(remArgs) > 0 {
|
||||||
conf = args[0]
|
conf = remArgs[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildInternal(*buildName, *buildPull, conf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the actual build process
|
||||||
|
func buildInternal(name string, pull bool, conf string) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = filepath.Base(conf)
|
name = filepath.Base(conf)
|
||||||
ext := filepath.Ext(conf)
|
ext := filepath.Ext(conf)
|
||||||
@ -198,3 +167,55 @@ func build(name string, pull bool, args []string) {
|
|||||||
log.Fatalf("Error writing outputs: %v", err)
|
log.Fatalf("Error writing outputs: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func untarKernel(buf *bytes.Buffer, bzimageName, ktarName string) (*bytes.Buffer, *bytes.Buffer, error) {
|
||||||
|
tr := tar.NewReader(buf)
|
||||||
|
|
||||||
|
var bzimage, ktar *bytes.Buffer
|
||||||
|
|
||||||
|
for {
|
||||||
|
hdr, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
switch hdr.Name {
|
||||||
|
case bzimageName:
|
||||||
|
bzimage = new(bytes.Buffer)
|
||||||
|
_, err := io.Copy(bzimage, tr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
case ktarName:
|
||||||
|
ktar = new(bytes.Buffer)
|
||||||
|
_, err := io.Copy(bzimage, tr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ktar == nil || bzimage == nil {
|
||||||
|
return nil, nil, errors.New("did not find bzImage and kernel.tar in tarball")
|
||||||
|
}
|
||||||
|
|
||||||
|
return bzimage, ktar, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func containersInitrd(containers []*bytes.Buffer) (*bytes.Buffer, error) {
|
||||||
|
w := new(bytes.Buffer)
|
||||||
|
iw := initrd.NewWriter(w)
|
||||||
|
defer iw.Close()
|
||||||
|
for _, file := range containers {
|
||||||
|
_, err := initrd.Copy(iw, file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return w, nil
|
||||||
|
}
|
||||||
|
@ -40,16 +40,6 @@ func main() {
|
|||||||
flagQuiet := flag.Bool("q", false, "Quiet execution")
|
flagQuiet := flag.Bool("q", false, "Quiet execution")
|
||||||
flagVerbose := flag.Bool("v", false, "Verbose execution")
|
flagVerbose := flag.Bool("v", false, "Verbose execution")
|
||||||
|
|
||||||
buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
|
|
||||||
buildCmd.Usage = func() {
|
|
||||||
fmt.Printf("USAGE: %s build [options] [file.yml]\n\n", os.Args[0])
|
|
||||||
fmt.Printf("'file.yml' defaults to 'moby.yml' if not specified.\n\n")
|
|
||||||
fmt.Printf("Options:\n")
|
|
||||||
buildCmd.PrintDefaults()
|
|
||||||
}
|
|
||||||
buildName := buildCmd.String("name", "", "Name to use for output files")
|
|
||||||
buildPull := buildCmd.Bool("pull", false, "Always pull images")
|
|
||||||
|
|
||||||
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
|
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
|
||||||
runCmd.Usage = func() {
|
runCmd.Usage = func() {
|
||||||
fmt.Printf("USAGE: %s run [options] [prefix]\n\n", os.Args[0])
|
fmt.Printf("USAGE: %s run [options] [prefix]\n\n", os.Args[0])
|
||||||
@ -93,8 +83,7 @@ func main() {
|
|||||||
|
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "build":
|
case "build":
|
||||||
buildCmd.Parse(args[1:])
|
build(args[1:])
|
||||||
build(*buildName, *buildPull, buildCmd.Args())
|
|
||||||
case "run":
|
case "run":
|
||||||
runCmd.Parse(args[1:])
|
runCmd.Parse(args[1:])
|
||||||
run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args())
|
run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args())
|
||||||
|
Loading…
Reference in New Issue
Block a user