Skip to content

Commit

Permalink
Merge pull request #1289 from weaveworks/move-listvar
Browse files Browse the repository at this point in the history
Move ListVar into common, so it can be reused elsewhere.
  • Loading branch information
tomwilkie committed Aug 12, 2015
2 parents 7656752 + 45293fd commit cdab6a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
31 changes: 31 additions & 0 deletions common/mflagext/listvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mflagext

import (
"fmt"

"github.com/docker/docker/pkg/mflag"
)

type listOpts struct {
value *[]string
hasBeenSet bool
}

func ListVar(p *[]string, names []string, value []string, usage string) {
*p = value
mflag.Var(&listOpts{p, false}, names, usage)
}

func (opts *listOpts) Set(value string) error {
if opts.hasBeenSet {
(*opts.value) = append((*opts.value), value)
} else {
(*opts.value) = []string{value}
opts.hasBeenSet = true
}
return nil
}

func (opts *listOpts) String() string {
return fmt.Sprintf("%v", []string(*opts.value))
}
27 changes: 2 additions & 25 deletions prog/weaveproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,14 @@ import (

"github.com/docker/docker/pkg/mflag"
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/mflagext"
"github.com/weaveworks/weave/proxy"
)

var (
version = "(unreleased version)"
)

type listOpts struct {
value *[]string
hasBeenSet bool
}

func ListVar(p *[]string, names []string, value []string, usage string) {
*p = value
mflag.Var(&listOpts{p, false}, names, usage)
}

func (opts *listOpts) Set(value string) error {
if opts.hasBeenSet {
(*opts.value) = append((*opts.value), value)
} else {
(*opts.value) = []string{value}
opts.hasBeenSet = true
}
return nil
}

func (opts *listOpts) String() string {
return fmt.Sprintf("%v", []string(*opts.value))
}

func main() {
var (
justVersion bool
Expand All @@ -49,7 +26,7 @@ func main() {

mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit")
mflag.StringVar(&logLevel, []string{"-log-level"}, "info", "logging level (debug, info, warning, error)")
ListVar(&c.ListenAddrs, []string{"H"}, nil, "addresses on which to listen")
mflagext.ListVar(&c.ListenAddrs, []string{"H"}, nil, "addresses on which to listen")
mflag.StringVar(&c.HostnameMatch, []string{"-hostname-match"}, "(.*)", "Regexp pattern to apply on container names (e.g. '^aws-[0-9]+-(.*)$')")
mflag.StringVar(&c.HostnameReplacement, []string{"-hostname-replacement"}, "$1", "Expression to generate hostnames based on matches from --hostname-match (e.g. 'my-app-$1')")
mflag.BoolVar(&c.NoDefaultIPAM, []string{"#-no-default-ipam", "-no-default-ipalloc"}, false, "do not automatically allocate addresses for containers without a WEAVE_CIDR")
Expand Down

0 comments on commit cdab6a7

Please sign in to comment.