Allow processing of common options from FlagSet
In situations where you don't want/need Cobra climbing behavior nor Cobra at all using FlagSet is the easier sell. Signed-off-by: Andreas Bergmeier <abergmeier@gmx.net>
This commit is contained in:
		
							parent
							
								
									4b22fc5c20
								
							
						
					
					
						commit
						2a32f0bacd
					
				| 
						 | 
					@ -28,6 +28,7 @@ import (
 | 
				
			||||||
	"github.com/pkg/errors"
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
	"github.com/sirupsen/logrus"
 | 
						"github.com/sirupsen/logrus"
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
	"golang.org/x/term"
 | 
						"golang.org/x/term"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,6 +54,11 @@ var (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CommonBuildOptions parses the build options from the bud cli
 | 
					// CommonBuildOptions parses the build options from the bud cli
 | 
				
			||||||
func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
					func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
 | 
						return CommonBuildOptionsFromFlagSet(c.Flags(), c.Flag)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// CommonBuildOptionsFromFlagSet parses the build options from the bud cli
 | 
				
			||||||
 | 
					func CommonBuildOptionsFromFlagSet(flags *pflag.FlagSet, findFlagFunc func(name string) *pflag.Flag) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		memoryLimit int64
 | 
							memoryLimit int64
 | 
				
			||||||
		memorySwap  int64
 | 
							memorySwap  int64
 | 
				
			||||||
| 
						 | 
					@ -60,7 +66,7 @@ func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
		err         error
 | 
							err         error
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memVal, _ := c.Flags().GetString("memory")
 | 
						memVal, _ := flags.GetString("memory")
 | 
				
			||||||
	if memVal != "" {
 | 
						if memVal != "" {
 | 
				
			||||||
		memoryLimit, err = units.RAMInBytes(memVal)
 | 
							memoryLimit, err = units.RAMInBytes(memVal)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -68,7 +74,7 @@ func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memSwapValue, _ := c.Flags().GetString("memory-swap")
 | 
						memSwapValue, _ := flags.GetString("memory-swap")
 | 
				
			||||||
	if memSwapValue != "" {
 | 
						if memSwapValue != "" {
 | 
				
			||||||
		if memSwapValue == "-1" {
 | 
							if memSwapValue == "-1" {
 | 
				
			||||||
			memorySwap = -1
 | 
								memorySwap = -1
 | 
				
			||||||
| 
						 | 
					@ -80,7 +86,7 @@ func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	addHost, _ := c.Flags().GetStringSlice("add-host")
 | 
						addHost, _ := flags.GetStringSlice("add-host")
 | 
				
			||||||
	if len(addHost) > 0 {
 | 
						if len(addHost) > 0 {
 | 
				
			||||||
		for _, host := range addHost {
 | 
							for _, host := range addHost {
 | 
				
			||||||
			if err := validateExtraHost(host); err != nil {
 | 
								if err := validateExtraHost(host); err != nil {
 | 
				
			||||||
| 
						 | 
					@ -91,8 +97,8 @@ func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	noDNS = false
 | 
						noDNS = false
 | 
				
			||||||
	dnsServers := []string{}
 | 
						dnsServers := []string{}
 | 
				
			||||||
	if c.Flag("dns").Changed {
 | 
						if flags.Changed("dns") {
 | 
				
			||||||
		dnsServers, _ = c.Flags().GetStringSlice("dns")
 | 
							dnsServers, _ = flags.GetStringSlice("dns")
 | 
				
			||||||
		for _, server := range dnsServers {
 | 
							for _, server := range dnsServers {
 | 
				
			||||||
			if strings.ToLower(server) == "none" {
 | 
								if strings.ToLower(server) == "none" {
 | 
				
			||||||
				noDNS = true
 | 
									noDNS = true
 | 
				
			||||||
| 
						 | 
					@ -104,62 +110,62 @@ func CommonBuildOptions(c *cobra.Command) (*define.CommonBuildOptions, error) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dnsSearch := []string{}
 | 
						dnsSearch := []string{}
 | 
				
			||||||
	if c.Flag("dns-search").Changed {
 | 
						if flags.Changed("dns-search") {
 | 
				
			||||||
		dnsSearch, _ = c.Flags().GetStringSlice("dns-search")
 | 
							dnsSearch, _ = flags.GetStringSlice("dns-search")
 | 
				
			||||||
		if noDNS && len(dnsSearch) > 0 {
 | 
							if noDNS && len(dnsSearch) > 0 {
 | 
				
			||||||
			return nil, errors.Errorf("invalid --dns-search, --dns-search may not be used with --dns=none")
 | 
								return nil, errors.Errorf("invalid --dns-search, --dns-search may not be used with --dns=none")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dnsOptions := []string{}
 | 
						dnsOptions := []string{}
 | 
				
			||||||
	if c.Flag("dns-option").Changed {
 | 
						if flags.Changed("dns-option") {
 | 
				
			||||||
		dnsOptions, _ = c.Flags().GetStringSlice("dns-option")
 | 
							dnsOptions, _ = flags.GetStringSlice("dns-option")
 | 
				
			||||||
		if noDNS && len(dnsOptions) > 0 {
 | 
							if noDNS && len(dnsOptions) > 0 {
 | 
				
			||||||
			return nil, errors.Errorf("invalid --dns-option, --dns-option may not be used with --dns=none")
 | 
								return nil, errors.Errorf("invalid --dns-option, --dns-option may not be used with --dns=none")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, err := units.FromHumanSize(c.Flag("shm-size").Value.String()); err != nil {
 | 
						if _, err := units.FromHumanSize(findFlagFunc("shm-size").Value.String()); err != nil {
 | 
				
			||||||
		return nil, errors.Wrapf(err, "invalid --shm-size")
 | 
							return nil, errors.Wrapf(err, "invalid --shm-size")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	volumes, _ := c.Flags().GetStringArray("volume")
 | 
						volumes, _ := flags.GetStringArray("volume")
 | 
				
			||||||
	if err := Volumes(volumes); err != nil {
 | 
						if err := Volumes(volumes); err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	cpuPeriod, _ := c.Flags().GetUint64("cpu-period")
 | 
						cpuPeriod, _ := flags.GetUint64("cpu-period")
 | 
				
			||||||
	cpuQuota, _ := c.Flags().GetInt64("cpu-quota")
 | 
						cpuQuota, _ := flags.GetInt64("cpu-quota")
 | 
				
			||||||
	cpuShares, _ := c.Flags().GetUint64("cpu-shares")
 | 
						cpuShares, _ := flags.GetUint64("cpu-shares")
 | 
				
			||||||
	httpProxy, _ := c.Flags().GetBool("http-proxy")
 | 
						httpProxy, _ := flags.GetBool("http-proxy")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ulimit := []string{}
 | 
						ulimit := []string{}
 | 
				
			||||||
	if c.Flag("ulimit").Changed {
 | 
						if flags.Changed("ulimit") {
 | 
				
			||||||
		ulimit, _ = c.Flags().GetStringSlice("ulimit")
 | 
							ulimit, _ = flags.GetStringSlice("ulimit")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	secrets, _ := c.Flags().GetStringArray("secret")
 | 
						secrets, _ := flags.GetStringArray("secret")
 | 
				
			||||||
	sshsources, _ := c.Flags().GetStringArray("ssh")
 | 
						sshsources, _ := flags.GetStringArray("ssh")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	commonOpts := &define.CommonBuildOptions{
 | 
						commonOpts := &define.CommonBuildOptions{
 | 
				
			||||||
		AddHost:      addHost,
 | 
							AddHost:      addHost,
 | 
				
			||||||
		CPUPeriod:    cpuPeriod,
 | 
							CPUPeriod:    cpuPeriod,
 | 
				
			||||||
		CPUQuota:     cpuQuota,
 | 
							CPUQuota:     cpuQuota,
 | 
				
			||||||
		CPUSetCPUs:   c.Flag("cpuset-cpus").Value.String(),
 | 
							CPUSetCPUs:   findFlagFunc("cpuset-cpus").Value.String(),
 | 
				
			||||||
		CPUSetMems:   c.Flag("cpuset-mems").Value.String(),
 | 
							CPUSetMems:   findFlagFunc("cpuset-mems").Value.String(),
 | 
				
			||||||
		CPUShares:    cpuShares,
 | 
							CPUShares:    cpuShares,
 | 
				
			||||||
		CgroupParent: c.Flag("cgroup-parent").Value.String(),
 | 
							CgroupParent: findFlagFunc("cgroup-parent").Value.String(),
 | 
				
			||||||
		DNSOptions:   dnsOptions,
 | 
							DNSOptions:   dnsOptions,
 | 
				
			||||||
		DNSSearch:    dnsSearch,
 | 
							DNSSearch:    dnsSearch,
 | 
				
			||||||
		DNSServers:   dnsServers,
 | 
							DNSServers:   dnsServers,
 | 
				
			||||||
		HTTPProxy:    httpProxy,
 | 
							HTTPProxy:    httpProxy,
 | 
				
			||||||
		Memory:       memoryLimit,
 | 
							Memory:       memoryLimit,
 | 
				
			||||||
		MemorySwap:   memorySwap,
 | 
							MemorySwap:   memorySwap,
 | 
				
			||||||
		ShmSize:      c.Flag("shm-size").Value.String(),
 | 
							ShmSize:      findFlagFunc("shm-size").Value.String(),
 | 
				
			||||||
		Ulimit:       ulimit,
 | 
							Ulimit:       ulimit,
 | 
				
			||||||
		Volumes:      volumes,
 | 
							Volumes:      volumes,
 | 
				
			||||||
		Secrets:      secrets,
 | 
							Secrets:      secrets,
 | 
				
			||||||
		SSHSources:   sshsources,
 | 
							SSHSources:   sshsources,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	securityOpts, _ := c.Flags().GetStringArray("security-opt")
 | 
						securityOpts, _ := flags.GetStringArray("security-opt")
 | 
				
			||||||
	if err := parseSecurityOpts(securityOpts, commonOpts); err != nil {
 | 
						if err := parseSecurityOpts(securityOpts, commonOpts); err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,9 +5,24 @@ import (
 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestCommonBuildOptionsFromFlagSet(t *testing.T) {
 | 
				
			||||||
 | 
						fs := pflag.NewFlagSet("testme", pflag.PanicOnError)
 | 
				
			||||||
 | 
						fs.String("memory", "1GB", "")
 | 
				
			||||||
 | 
						fs.String("shm-size", "5TB", "")
 | 
				
			||||||
 | 
						fs.String("cpuset-cpus", "1", "")
 | 
				
			||||||
 | 
						fs.String("cpuset-mems", "2", "")
 | 
				
			||||||
 | 
						fs.String("cgroup-parent", "none", "")
 | 
				
			||||||
 | 
						err := fs.Parse([]string{"--memory", "2GB"})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						cbo, err := CommonBuildOptionsFromFlagSet(fs, fs.Lookup)
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, cbo.Memory, int64(2147483648))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TestDeviceParser verifies the given device strings is parsed correctly
 | 
					// TestDeviceParser verifies the given device strings is parsed correctly
 | 
				
			||||||
func TestDeviceParser(t *testing.T) {
 | 
					func TestDeviceParser(t *testing.T) {
 | 
				
			||||||
	if runtime.GOOS != "linux" {
 | 
						if runtime.GOOS != "linux" {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue