Skip to content

Commit

Permalink
define config.CommandConfig.parse() and use it instead of using `pa…
Browse files Browse the repository at this point in the history
…rseCommand`
  • Loading branch information
Songmu committed Jan 7, 2018
1 parent 3c93f95 commit b46d0ba
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,14 @@ type MetricPlugin struct {
}

func (pconf *PluginConfig) buildMetricPlugin() (*MetricPlugin, error) {
cmd, err := parseCommand(pconf.Raw, pconf.User, pconf.Env, pconf.TimeoutSeconds)
cmd, err := pconf.CommandConfig.parse()
if err != nil {
return nil, err
}
if cmd == nil {
return nil, fmt.Errorf("failed to parse plugin command. A configuration value of `command` should be string or string slice, but %T", pconf.Raw)
}

cmd.Env, err = pconf.Env.ConvertToStrings()
if err != nil {
return nil, err
}

cmd.TimeoutDuration = time.Duration(pconf.TimeoutSeconds * int64(time.Second))

var (
includePattern *regexp.Regexp
excludePattern *regexp.Regexp
Expand Down Expand Up @@ -271,15 +264,15 @@ type CheckPlugin struct {
}

func (pconf *PluginConfig) buildCheckPlugin(name string) (*CheckPlugin, error) {
cmd, err := parseCommand(pconf.Raw, pconf.User, pconf.Env, pconf.TimeoutSeconds)
cmd, err := pconf.CommandConfig.parse()
if err != nil {
return nil, err
}
if cmd == nil {
return nil, fmt.Errorf("failed to parse plugin command. A configuration value of `command` should be string or string slice, but %T", pconf.Raw)
}

action, err := parseCommand(pconf.Action.Raw, pconf.Action.User, pconf.Action.Env, pconf.Action.TimeoutSeconds)
action, err := pconf.Action.parse()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,7 +300,7 @@ type MetadataPlugin struct {
}

func (pconf *PluginConfig) buildMetadataPlugin() (*MetadataPlugin, error) {
cmd, err := parseCommand(pconf.Raw, pconf.User, pconf.Env, pconf.TimeoutSeconds)
cmd, err := pconf.CommandConfig.parse()
if err != nil {
return nil, err
}
Expand All @@ -321,9 +314,9 @@ func (pconf *PluginConfig) buildMetadataPlugin() (*MetadataPlugin, error) {
}, nil
}

func parseCommand(commandRaw interface{}, user string, env Env, timeoutSeconds int64) (cmd *Command, err error) {
func (cc CommandConfig) parse() (cmd *Command, err error) {
const errFmt = "failed to parse plugin command. A configuration value of `command` should be string or string slice, but %T"
switch t := commandRaw.(type) {
switch t := cc.Raw.(type) {
case string:
cmd = &Command{Cmd: t}
case []interface{}:
Expand All @@ -332,28 +325,27 @@ func parseCommand(commandRaw interface{}, user string, env Env, timeoutSeconds i
for _, vv := range t {
str, ok := vv.(string)
if !ok {
return nil, fmt.Errorf(errFmt, commandRaw)
return nil, fmt.Errorf(errFmt, cc.Raw)
}

args = append(args, str)
}
cmd = &Command{Args: args}
} else {
return nil, fmt.Errorf(errFmt, commandRaw)
return nil, fmt.Errorf(errFmt, cc.Raw)
}
case []string:
cmd = &Command{Args: t}
case nil:
return nil, nil
default:
return nil, fmt.Errorf(errFmt, commandRaw)
return nil, fmt.Errorf(errFmt, cc.Raw)
}
cmd.User = user
cmd.Env, err = env.ConvertToStrings()
cmd.User = cc.User
cmd.Env, err = cc.Env.ConvertToStrings()
if err != nil {
return nil, err
}
cmd.TimeoutDuration = time.Duration(timeoutSeconds * int64(time.Second))
cmd.TimeoutDuration = time.Duration(cc.TimeoutSeconds * int64(time.Second))
return cmd, nil
}

Expand Down

0 comments on commit b46d0ba

Please sign in to comment.