Migrate to cobra CLI

We intend to migrate to the cobra cli from urfave/cli because the
project is more well maintained.  There are also some technical reasons
as well which extend into our remote client work.

Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
baude
2019-01-31 13:20:04 -06:00
parent 962850c6e0
commit 25a3923b61
149 changed files with 11444 additions and 4448 deletions

View File

@ -13,263 +13,183 @@ import (
"github.com/containers/buildah/util"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/spf13/pflag"
)
var (
usernsFlags = []cli.Flag{
cli.StringFlag{
Name: "userns",
Usage: "'container', `path` of user namespace to join, or 'host'",
},
cli.StringSliceFlag{
Name: "userns-uid-map",
Usage: "`containerID:hostID:length` UID mapping to use in user namespace",
},
cli.StringSliceFlag{
Name: "userns-gid-map",
Usage: "`containerID:hostID:length` GID mapping to use in user namespace",
},
cli.StringFlag{
Name: "userns-uid-map-user",
Usage: "`name` of entries from /etc/subuid to use to set user namespace UID mapping",
},
cli.StringFlag{
Name: "userns-gid-map-group",
Usage: "`name` of entries from /etc/subgid to use to set user namespace GID mapping",
},
}
// LayerResults represents the results of the layer flags
type LayerResults struct {
ForceRm bool
Layers bool
}
NamespaceFlags = []cli.Flag{
cli.StringFlag{
Name: string(specs.IPCNamespace),
Usage: "'container', `path` of IPC namespace to join, or 'host'",
},
cli.StringFlag{
Name: string(specs.NetworkNamespace) + ", net",
Usage: "'container', `path` of network namespace to join, or 'host'",
},
cli.StringFlag{
Name: "cni-config-dir",
Usage: "`directory` of CNI configuration files",
Value: util.DefaultCNIConfigDir,
},
cli.StringFlag{
Name: "cni-plugin-path",
Usage: "`path` of CNI network plugins",
Value: util.DefaultCNIPluginPath,
},
cli.StringFlag{
Name: string(specs.PIDNamespace),
Usage: "'container', `path` of PID namespace to join, or 'host'",
},
cli.StringFlag{
Name: string(specs.UTSNamespace),
Usage: "'container', `path` of UTS namespace to join, or 'host'",
},
}
// UserNSResults represents the results for the UserNS flags
type UserNSResults struct {
UserNS string
UserNSUIDMap []string
UserNSGIDMap []string
UserNSUIDMapUser string
UserNSGIDMapGroup string
}
LayerFlags = []cli.Flag{
cli.BoolFlag{
Name: "force-rm",
Usage: "Always remove intermediate containers after a build, even if the build is unsuccessful.",
},
cli.BoolFlag{
Name: "layers",
Usage: fmt.Sprintf("cache intermediate layers during build. Use BUILDAH_LAYERS environment variable to override. (default %t)", UseLayers()),
},
}
// NameSpaceResults represents the results for Namespace flags
type NameSpaceResults struct {
IPC string
Network string
CNIConfigDir string
CNIPlugInPath string
PID string
UTS string
}
BudFlags = []cli.Flag{
cli.StringSliceFlag{
Name: "annotation",
Usage: "Set metadata for an image (default [])",
},
cli.StringFlag{
Name: "authfile",
Usage: "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json",
},
cli.StringSliceFlag{
Name: "build-arg",
Usage: "`argument=value` to supply to the builder",
},
cli.StringFlag{
Name: "cache-from",
Usage: "Images to utilise as potential cache sources. The build process does not currently support caching so this is a NOOP.",
},
cli.StringFlag{
Name: "cert-dir",
Value: "",
Usage: "use certificates at the specified path to access the registry",
},
cli.BoolFlag{
Name: "compress",
Usage: "This is legacy option, which has no effect on the image",
},
cli.StringFlag{
Name: "creds",
Value: "",
Usage: "use `[username[:password]]` for accessing the registry",
},
cli.BoolFlag{
Name: "disable-compression, D",
Usage: "don't compress layers by default",
},
cli.BoolFlag{
Name: "disable-content-trust",
Usage: "This is a Docker specific option and is a NOOP",
},
cli.StringSliceFlag{
Name: "file, f",
Usage: "`pathname or URL` of a Dockerfile",
},
cli.StringFlag{
Name: "format",
Usage: "`format` of the built image's manifest and metadata. Use BUILDAH_FORMAT environment variable to override.",
Value: DefaultFormat(),
},
cli.StringFlag{
Name: "iidfile",
Usage: "`file` to write the image ID to",
},
cli.StringSliceFlag{
Name: "label",
Usage: "Set metadata for an image (default [])",
},
cli.BoolFlag{
Name: "no-cache",
Usage: "Do not use existing cached images for the container build. Build from the start with a new set of cached layers.",
},
cli.StringFlag{
Name: "logfile",
Usage: "log to `file` instead of stdout/stderr",
},
cli.IntFlag{
Name: "loglevel",
Usage: "adjust logging level (range from -2 to 3)",
},
cli.StringFlag{
Name: "platform",
Usage: "CLI compatibility: no action or effect",
},
cli.BoolTFlag{
Name: "pull",
Usage: "pull the image if not present",
},
cli.BoolFlag{
Name: "pull-always",
Usage: "pull the image, even if a version is present",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "refrain from announcing build instructions and image read/write progress",
},
cli.BoolTFlag{
Name: "rm",
Usage: "Remove intermediate containers after a successful build (default true)",
},
cli.StringFlag{
Name: "runtime",
Usage: "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.",
Value: util.Runtime(),
},
cli.StringSliceFlag{
Name: "runtime-flag",
Usage: "add global flags for the container runtime",
},
cli.StringFlag{
Name: "signature-policy",
Usage: "`pathname` of signature policy file (not usually used)",
},
cli.BoolFlag{
Name: "squash",
Usage: "Squash newly built layers into a single new layer. The build process does not currently support caching so this is a NOOP.",
},
cli.StringSliceFlag{
Name: "tag, t",
Usage: "tagged `name` to apply to the built image",
},
cli.BoolTFlag{
Name: "tls-verify",
Usage: "require HTTPS and verify certificates when accessing the registry",
},
}
// BudResults represents the results for Bud flags
type BudResults struct {
Annotation []string
Authfile string
BuildArg []string
CacheFrom string
CertDir string
Compress bool
Creds string
DisableCompression bool
DisableContentTrust bool
File []string
Format string
Iidfile string
NoCache bool
Label []string
Logfile string
Loglevel int
Platform string
Pull bool
PullAlways bool
Quiet bool
Rm bool
Runtime string
RuntimeOpts []string
SignaturePolicy string
Squash bool
Tag []string
TlsVerify bool
}
FromAndBudFlags = append(append([]cli.Flag{
cli.StringSliceFlag{
Name: "add-host",
Usage: "add a custom host-to-IP mapping (`host:ip`) (default [])",
},
cli.StringFlag{
Name: "blob-cache",
Value: "",
Usage: "assume image blobs in the specified directory will be available for pushing",
Hidden: true, // this is here mainly so that we can test the API during integration tests
},
cli.StringSliceFlag{
Name: "cap-add",
Usage: "add the specified capability when running (default [])",
},
cli.StringSliceFlag{
Name: "cap-drop",
Usage: "drop the specified capability when running (default [])",
},
cli.StringFlag{
Name: "cgroup-parent",
Usage: "optional parent cgroup for the container",
},
cli.Uint64Flag{
Name: "cpu-period",
Usage: "limit the CPU CFS (Completely Fair Scheduler) period",
},
cli.Int64Flag{
Name: "cpu-quota",
Usage: "limit the CPU CFS (Completely Fair Scheduler) quota",
},
cli.Uint64Flag{
Name: "cpu-shares, c",
Usage: "CPU shares (relative weight)",
},
cli.StringFlag{
Name: "cpuset-cpus",
Usage: "CPUs in which to allow execution (0-3, 0,1)",
},
cli.StringFlag{
Name: "cpuset-mems",
Usage: "memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.",
},
cli.StringFlag{
Name: "isolation",
Usage: "`type` of process isolation to use. Use BUILDAH_ISOLATION environment variable to override.",
Value: DefaultIsolation(),
},
cli.StringFlag{
Name: "memory, m",
Usage: "memory limit (format: <number>[<unit>], where unit = b, k, m or g)",
},
cli.StringFlag{
Name: "memory-swap",
Usage: "swap limit equal to memory plus swap: '-1' to enable unlimited swap",
},
cli.StringSliceFlag{
Name: "security-opt",
Usage: "security options (default [])",
},
cli.StringFlag{
Name: "shm-size",
Usage: "size of '/dev/shm'. The format is `<number><unit>`.",
Value: "65536k",
},
cli.StringSliceFlag{
Name: "ulimit",
Usage: "ulimit options (default [])",
},
cli.StringSliceFlag{
Name: "volume, v",
Usage: "bind mount a volume into the container (default [])",
},
}, usernsFlags...), NamespaceFlags...)
)
// FromAndBugResults represents the results for common flags
// in bud and from
type FromAndBudResults struct {
AddHost []string
BlobCache string
CapAdd []string
CapDrop []string
CgroupParent string
CPUPeriod uint64
CPUQuota int64
CPUSetCPUs string
CPUSetMems string
CPUShares uint64
Isolation string
Memory string
MemorySwap string
SecurityOpt []string
ShmSize string
Ulimit []string
Volume []string
}
// GetUserNSFlags returns the common flags for usernamespace
func GetUserNSFlags(flags *UserNSResults) pflag.FlagSet {
usernsFlags := pflag.FlagSet{}
usernsFlags.StringVar(&flags.UserNS, "userns", "", "'container', `path` of user namespace to join, or 'host'")
usernsFlags.StringSliceVar(&flags.UserNSUIDMap, "userns-uid-map", []string{}, "`containerID:hostID:length` UID mapping to use in user namespace")
usernsFlags.StringSliceVar(&flags.UserNSGIDMap, "userns-gid-map", []string{}, "`containerID:hostID:length` GID mapping to use in user namespace")
usernsFlags.StringVar(&flags.UserNSUIDMapUser, "userns-uid-map-user", "", "`name` of entries from /etc/subuid to use to set user namespace UID mapping")
usernsFlags.StringVar(&flags.UserNSGIDMapGroup, "userns-gid-map-group", "", "`name` of entries from /etc/subgid to use to set user namespace GID mapping")
return usernsFlags
}
// GetNameSpaceFlags returns the common flags for a namespace menu
func GetNameSpaceFlags(flags *NameSpaceResults) pflag.FlagSet {
fs := pflag.FlagSet{}
fs.StringVar(&flags.IPC, string(specs.IPCNamespace), "", "'container', `path` of IPC namespace to join, or 'host'")
fs.StringVar(&flags.Network, string(specs.NetworkNamespace), "", "'container', `path` of network namespace to join, or 'host'")
// TODO How do we alias net and network?
fs.StringVar(&flags.Network, "net", "", "'container', `path` of network namespace to join, or 'host'")
fs.MarkHidden("net")
fs.StringVar(&flags.CNIConfigDir, "cni-config-dir", util.DefaultCNIConfigDir, "`directory` of CNI configuration files")
fs.StringVar(&flags.CNIPlugInPath, "cni-plugin-path", util.DefaultCNIPluginPath, "`path` of CNI network plugins")
fs.StringVar(&flags.PID, string(specs.PIDNamespace), "", "container, `path` of PID namespace to join, or 'host'")
fs.StringVar(&flags.UTS, string(specs.UTSNamespace), "", "container, :`path` of UTS namespace to join, or 'host'")
return fs
}
// GetLayerFlags returns the common flags for layers
func GetLayerFlags(flags *LayerResults) pflag.FlagSet {
fs := pflag.FlagSet{}
fs.BoolVar(&flags.ForceRm, "force-rm", false, "Always remove intermediate containers after a build, even if the build is unsuccessful.")
fs.BoolVar(&flags.Layers, "layers", false, fmt.Sprintf("cache intermediate layers during build. Use BUILDAH_LAYERS environment variable to override. (default %t)", UseLayers()))
return fs
}
// GetBudFlags returns common bud flags
func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs := pflag.FlagSet{}
fs.StringSliceVar(&flags.Annotation, "annotation", []string{}, "Set metadata for an image (default [])")
fs.StringVar(&flags.Authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json")
fs.StringSliceVar(&flags.BuildArg, "build-arg", []string{}, "`argument=value` to supply to the builder")
fs.StringVar(&flags.CacheFrom, "cache-from", "", "Images to utilise as potential cache sources. The build process does not currently support caching so this is a NOOP.")
fs.StringVar(&flags.CertDir, "cert-dir", "", "use certificates at the specified path to access the registry")
fs.BoolVar(&flags.Compress, "compress", false, "This is legacy option, which has no effect on the image")
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
fs.BoolVarP(&flags.DisableCompression, "disable-compression", "D", false, "don't compress layers by default")
fs.BoolVar(&flags.DisableContentTrust, "disable-content-trust", false, "This is a Docker specific option and is a NOOP")
fs.StringSliceVarP(&flags.File, "file", "f", []string{}, "`pathname or URL` of a Dockerfile")
fs.StringVar(&flags.Format, "format", DefaultFormat(), "`format` of the built image's manifest and metadata. Use BUILDAH_FORMAT environment variable to override.")
fs.StringVar(&flags.Iidfile, "iidfile", "", "`file` to write the image ID to")
fs.StringSliceVar(&flags.Label, "label", []string{}, "Set metadata for an image (default [])")
fs.BoolVar(&flags.NoCache, "no-cache", false, "Do not use existing cached images for the container build. Build from the start with a new set of cached layers.")
fs.StringVar(&flags.Logfile, "logfile", "", "log to `file` instead of stdout/stderr")
fs.IntVar(&flags.Loglevel, "loglevel", 0, "adjust logging level (range from -2 to 3)")
fs.StringVar(&flags.Platform, "platform", "", "CLI compatibility: no action or effect")
fs.BoolVar(&flags.Pull, "pull", true, "pull the image if not present")
fs.BoolVar(&flags.PullAlways, "pull-always", false, "pull the image, even if a version is present")
fs.BoolVarP(&flags.Quiet, "quiet", "q", false, "refrain from announcing build instructions and image read/write progress")
fs.BoolVar(&flags.Rm, "rm", true, "Remove intermediate containers after a successful build (default true)")
fs.StringVar(&flags.Runtime, "runtime", util.Runtime(), "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.")
fs.StringSliceVar(&flags.RuntimeOpts, "runtime-flag", []string{}, "add global flags for the container runtime")
fs.StringVar(&flags.SignaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)")
fs.BoolVar(&flags.Squash, "squash", false, "Squash newly built layers into a single new layer. The build process does not currently support caching so this is a NOOP.")
fs.StringSliceVarP(&flags.Tag, "tag", "t", []string{}, "tagged `name` to apply to the built image")
fs.BoolVar(&flags.TlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing the registry")
return fs
}
func GetFromAndBudFlags(flags *FromAndBudResults, usernsResults *UserNSResults, namespaceResults *NameSpaceResults) pflag.FlagSet {
fs := pflag.FlagSet{}
fs.StringSliceVar(&flags.AddHost, "add-host", []string{}, "add a custom host-to-IP mapping (`host:ip`) (default [])")
fs.StringVar(&flags.BlobCache, "blob-cache", "", "assume image blobs in the specified directory will be available for pushing")
fs.MarkHidden("blob-cache")
fs.StringSliceVar(&flags.CapAdd, "cap-add", []string{}, "add the specified capability when running (default [])")
fs.StringSliceVar(&flags.CapDrop, "cap-drop", []string{}, "drop the specified capability when running (default [])")
fs.StringVar(&flags.CgroupParent, "cgroup-parent", "", "optional parent cgroup for the container")
fs.Uint64Var(&flags.CPUPeriod, "cpu-period", 0, "limit the CPU CFS (Completely Fair Scheduler) period")
fs.Int64Var(&flags.CPUQuota, "cpu-quota", 0, "limit the CPU CFS (Completely Fair Scheduler) quota")
fs.Uint64VarP(&flags.CPUShares, "cpu-shares", "c", 0, "CPU shares (relative weight)")
fs.StringVar(&flags.CPUSetCPUs, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
fs.StringVar(&flags.CPUSetMems, "cpuset-mems", "", "memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.")
fs.StringVar(&flags.Isolation, "isolation", DefaultIsolation(), "`type` of process isolation to use. Use BUILDAH_ISOLATION environment variable to override.")
fs.StringVarP(&flags.Memory, "memory", "m", "", "memory limit (format: <number>[<unit>], where unit = b, k, m or g)")
fs.StringVar(&flags.MemorySwap, "memory-swap", "", "swap limit equal to memory plus swap: '-1' to enable unlimited swap")
fs.StringSliceVar(&flags.SecurityOpt, "security-opt", []string{}, "security options (default [])")
fs.StringVar(&flags.ShmSize, "shm-size", "65536k", "size of '/dev/shm'. The format is `<number><unit>`.")
fs.StringSliceVar(&flags.Ulimit, "ulimit", []string{}, "ulimit options (default [])")
fs.StringSliceVarP(&flags.Volume, "volume", "v", []string{}, "bind mount a volume into the container (default [])")
// Add in the usernamespace and namespaceflags
usernsFlags := GetUserNSFlags(usernsResults)
namespaceFlags := GetNameSpaceFlags(namespaceResults)
fs.AddFlagSet(&usernsFlags)
fs.AddFlagSet(&namespaceFlags)
return fs
}
// UseLayers returns true if BUILDAH_LAYERS is set to "1" or "true"
// otherwise it returns false
@ -299,6 +219,15 @@ func DefaultIsolation() string {
return buildah.OCI
}
// DefaultHistory returns the default add-history setting
func DefaultHistory() bool {
history := os.Getenv("BUILDAH_HISTORY")
if strings.ToLower(history) == "true" || history == "1" {
return true
}
return false
}
func VerifyFlagsArgsOrder(args []string) error {
for _, arg := range args {
if strings.HasPrefix(arg, "-") {