Skip to content

Commit

Permalink
Merge pull request #117 from mackerelio/filter-rolefullnames
Browse files Browse the repository at this point in the history
filter invalid roleFullNames with warning logs
  • Loading branch information
Songmu committed Jul 16, 2015
2 parents 4a8a1db + 759a4a9 commit d866453
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ import (
// allow options like -role=... -role=...
type roleFullnamesFlag []string

var roleFullnamePattern = regexp.MustCompile(`^[\w-]+:\s*[\w-]+$`)
var roleFullnamePattern = regexp.MustCompile(`^[a-zA-Z0-9][-_a-zA-Z0-9]*:\s*[a-zA-Z0-9][-_a-zA-Z0-9]*$`)

func (r *roleFullnamesFlag) String() string {
return fmt.Sprint(*r)
}

func (r *roleFullnamesFlag) Set(input string) error {
inputRoles := strings.Split(input, ",")

for _, inputRole := range inputRoles {
if roleFullnamePattern.MatchString(inputRole) == false {
return fmt.Errorf("Bad format for role fullname (expecting <service>:<role>): %s", inputRole)
}
}

*r = append(*r, inputRoles...)

return nil
}

Expand Down Expand Up @@ -88,14 +80,14 @@ func resolveConfig() (*config.Config, *otherOptions) {
otherOptions := &otherOptions{}

var (
conffile = flag.String("conf", config.DefaultConfig.Conffile, "Config file path (Configs in this file are over-written by command line options)")
apibase = flag.String("apibase", config.DefaultConfig.Apibase, "API base")
pidfile = flag.String("pidfile", config.DefaultConfig.Pidfile, "File containing PID")
root = flag.String("root", config.DefaultConfig.Root, "Directory containing variable state information")
apikey = flag.String("apikey", "", "API key from mackerel.io web site")
diagnostic = flag.Bool("diagnostic", false, "Enables diagnostic features")
runOnce = flag.Bool("once", false, "Show spec and metrics to stdout once")
printVersion = flag.Bool("version", false, "Prints version and exit")
conffile = flag.String("conf", config.DefaultConfig.Conffile, "Config file path (Configs in this file are over-written by command line options)")
apibase = flag.String("apibase", config.DefaultConfig.Apibase, "API base")
pidfile = flag.String("pidfile", config.DefaultConfig.Pidfile, "File containing PID")
root = flag.String("root", config.DefaultConfig.Root, "Directory containing variable state information")
apikey = flag.String("apikey", "", "API key from mackerel.io web site")
diagnostic = flag.Bool("diagnostic", false, "Enables diagnostic features")
runOnce = flag.Bool("once", false, "Show spec and metrics to stdout once")
printVersion = flag.Bool("version", false, "Prints version and exit")
)

var verbose bool
Expand Down Expand Up @@ -145,6 +137,15 @@ func resolveConfig() (*config.Config, *otherOptions) {
}
})

r := []string{}
for _, roleFullName := range conf.Roles {
if !roleFullnamePattern.MatchString(roleFullName) {
logger.Errorf("Bad format for role fullname (expecting <service>:<role>. Alphabet, numbers, hyphens and underscores are acceptable, but the first character must not be a hyphen or an underscore.): '%s'", roleFullName)
} else {
r = append(r, roleFullName)
}
}
conf.Roles = r
return conf, nil
}

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ diagnostic=false
confFile.Close()
defer os.Remove(confFile.Name())

os.Args = []string{"mackerel-agent", "-conf=" + confFile.Name(), "-role=My-Service:default", "-verbose", "-diagnostic"}
os.Args = []string{"mackerel-agent", "-conf=" + confFile.Name(), "-role=My-Service:default,INVALID#SERVICE", "-verbose", "-diagnostic"}
// Overrides Args from go test command
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.PanicOnError)

Expand Down

0 comments on commit d866453

Please sign in to comment.