Skip to content

Commit

Permalink
Merge pull request #18 from StarOfService/custom-parser
Browse files Browse the repository at this point in the history
Custom parser without colon
  • Loading branch information
linki authored Nov 7, 2018
2 parents 4ccd6c6 + da09091 commit daaebb8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/cloudformation-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alecthomas/kingpin"
"github.com/sirupsen/logrus"

"github.com/linki/cloudformation-operator/pkg/argparser"
stub "github.com/linki/cloudformation-operator/pkg/stub"
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
sdkVersion "github.com/operator-framework/operator-sdk/version"
Expand All @@ -19,7 +20,7 @@ import (
var (
namespace string
region string
tags = map[string]string{}
tags = new(map[string]string)
dryRun bool
debug bool
version = "0.3.0+git"
Expand All @@ -28,9 +29,10 @@ var (
func init() {
kingpin.Flag("namespace", "The Kubernetes namespace to watch").Default("default").Envar("WATCH_NAMESPACE").StringVar(&namespace)
kingpin.Flag("region", "The AWS region to use").Envar("AWS_REGION").StringVar(&region)
kingpin.Flag("tag", "Tags to apply to all Stacks by default. Specify multiple times for multiple tags.").Envar("AWS_TAGS").StringMapVar(&tags)
kingpin.Flag("dry-run", "If true, don't actually do anything.").Envar("DRY_RUN").BoolVar(&dryRun)
kingpin.Flag("debug", "Enable debug logging.").Envar("DEBUG").BoolVar(&debug)

tags = argparser.StringMap(kingpin.Flag("tag", "Tags to apply to all Stacks by default. Specify multiple times for multiple tags.").Envar("AWS_TAGS"))
}

func printVersion() {
Expand Down Expand Up @@ -59,6 +61,6 @@ func main() {
})

sdk.Watch("cloudformation.linki.space/v1alpha1", "Stack", namespace, 0)
sdk.Handle(stub.NewHandler(client, tags, dryRun))
sdk.Handle(stub.NewHandler(client, *tags, dryRun))
sdk.Run(context.TODO())
}
44 changes: 44 additions & 0 deletions pkg/argparser/stringmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package argparser

import (
"fmt"
"regexp"

"github.com/alecthomas/kingpin"
)

// -- map[string]string Value
type stringMapValue map[string]string

func newStringMapValue(p *map[string]string) *stringMapValue {
return (*stringMapValue)(p)
}

var stringMapRegex = regexp.MustCompile("[=]")

func (s *stringMapValue) Set(value string) error {
parts := stringMapRegex.Split(value, 2)
if len(parts) != 2 {
return fmt.Errorf("expected KEY=VALUE got '%s'", value)
}
(*s)[parts[0]] = parts[1]
return nil
}

func (s *stringMapValue) Get() interface{} {
return (map[string]string)(*s)
}

func (s *stringMapValue) String() string {
return fmt.Sprintf("%s", map[string]string(*s))
}

func (s *stringMapValue) IsCumulative() bool {
return true
}

func StringMap(s kingpin.Settings) (target *map[string]string) {
target = &map[string]string{}
s.SetValue((*stringMapValue)(target))
return
}

0 comments on commit daaebb8

Please sign in to comment.