(ctx context.Context, actionName string, cmd **policy.ActionCommand, value string, changeCount *int)
| 57 | } |
| 58 | |
| 59 | func (c *policyActionFlags) setActionCommandFromFlags(ctx context.Context, actionName string, cmd **policy.ActionCommand, value string, changeCount *int) error { |
| 60 | if value == "-" { |
| 61 | // not set |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | if value == "" { |
| 66 | log(ctx).Infof(" - removing %v action", actionName) |
| 67 | |
| 68 | *changeCount++ |
| 69 | |
| 70 | *cmd = nil |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | *cmd = &policy.ActionCommand{ |
| 76 | TimeoutSeconds: int(c.policySetActionCommandTimeout.Seconds()), |
| 77 | Mode: c.policySetActionCommandMode, |
| 78 | } |
| 79 | |
| 80 | *changeCount++ |
| 81 | |
| 82 | if c.policySetPersistActionScript { |
| 83 | script, err := os.ReadFile(value) //nolint:gosec |
| 84 | if err != nil { |
| 85 | return errors.Wrap(err, "unable to read script file") |
| 86 | } |
| 87 | |
| 88 | if len(script) > maxScriptLength { |
| 89 | return errors.Errorf("action script file (%v) too long: %v, max allowed %d", value, len(script), maxScriptLength) |
| 90 | } |
| 91 | |
| 92 | log(ctx).Infof(" - setting %v (%v) action script from file %v (%v bytes) with timeout %v", actionName, c.policySetActionCommandMode, value, len(script), c.policySetActionCommandTimeout) |
| 93 | |
| 94 | (*cmd).Script = string(script) |
| 95 | |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // parse path as CSV as if space was the separator, this automatically takes care of quotations |
| 100 | r := csv.NewReader(strings.NewReader(value)) |
| 101 | r.Comma = ' ' // space |
| 102 | |
| 103 | fields, err := r.Read() |
| 104 | if err != nil { |
| 105 | return errors.Wrapf(err, "error parsing %v command", actionName) |
| 106 | } |
| 107 | |
| 108 | (*cmd).Command = fields[0] |
| 109 | (*cmd).Arguments = fields[1:] |
| 110 | |
| 111 | if len((*cmd).Arguments) == 0 { |
| 112 | log(ctx).Infof(" - setting %v (%v) action command to %v and timeout %v", actionName, c.policySetActionCommandMode, quoteArguments((*cmd).Command), c.policySetActionCommandTimeout) |
| 113 | } else { |
| 114 | log(ctx).Infof(" - setting %v (%v) action command to %v with arguments %v and timeout %v", actionName, c.policySetActionCommandMode, quoteArguments((*cmd).Command), quoteArguments((*cmd).Arguments...), c.policySetActionCommandTimeout) |
| 115 | } |
| 116 |
no test coverage detected