mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-20 09:39:08 +00:00
Merge pull request #187 from justincormack/build-improvements
Make easier to use as a library
This commit is contained in:
commit
ad2fda5769
@ -9,7 +9,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/moby/tool/src/moby"
|
"github.com/moby/tool/src/moby"
|
||||||
@ -32,27 +31,6 @@ func (f *formatList) Set(value string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a string which is either a number in MB, or a number with
|
|
||||||
// either M (for Megabytes) or G (for GigaBytes) as a suffix and
|
|
||||||
// returns the number in MB. Return 0 if string is empty.
|
|
||||||
func getDiskSizeMB(s string) (int, error) {
|
|
||||||
if s == "" {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
sz := len(s)
|
|
||||||
if strings.HasSuffix(s, "G") {
|
|
||||||
i, err := strconv.Atoi(s[:sz-1])
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return i * 1024, nil
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(s, "M") {
|
|
||||||
s = s[:sz-1]
|
|
||||||
}
|
|
||||||
return strconv.Atoi(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process the build arguments and execute build
|
// Process the build arguments and execute build
|
||||||
func build(args []string) {
|
func build(args []string) {
|
||||||
var buildFormats formatList
|
var buildFormats formatList
|
||||||
|
29
cmd/moby/util.go
Normal file
29
cmd/moby/util.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This function parses the "size" parameter of a disk specification
|
||||||
|
// and returns the size in MB. The "size" parameter defaults to GB, but
|
||||||
|
// the unit can be explicitly set with either a G (for GB) or M (for
|
||||||
|
// MB). It returns the disk size in MB.
|
||||||
|
func getDiskSizeMB(s string) (int, error) {
|
||||||
|
if s == "" {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
sz := len(s)
|
||||||
|
if strings.HasSuffix(s, "M") {
|
||||||
|
return strconv.Atoi(s[:sz-1])
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(s, "G") {
|
||||||
|
s = s[:sz-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
i, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return 1024 * i, nil
|
||||||
|
}
|
@ -142,7 +142,7 @@ func outputImage(image *Image, section string, prefix string, m Moby, idMap map[
|
|||||||
// Build performs the actual build process
|
// Build performs the actual build process
|
||||||
func Build(m Moby, w io.Writer, pull bool, tp string) error {
|
func Build(m Moby, w io.Writer, pull bool, tp string) error {
|
||||||
if MobyDir == "" {
|
if MobyDir == "" {
|
||||||
return fmt.Errorf("MobyDir for temporary storage not set")
|
MobyDir = defaultMobyConfigDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
iw := tar.NewWriter(w)
|
iw := tar.NewWriter(w)
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
package moby
|
package moby
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// MobyDir is the location of the cache directory which should be set by the caller
|
// MobyDir is the location of the cache directory, defaults to ~/.moby
|
||||||
MobyDir string
|
MobyDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func defaultMobyConfigDir() string {
|
||||||
|
mobyDefaultDir := ".moby"
|
||||||
|
home := homeDir()
|
||||||
|
return filepath.Join(home, mobyDefaultDir)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user