MCPcopy
hub / github.com/kopia/kopia / setActionCommandFromFlags

Method setActionCommandFromFlags

cli/command_policy_set_actions.go:59–118  ·  view source on GitHub ↗
(ctx context.Context, actionName string, cmd **policy.ActionCommand, value string, changeCount *int)

Source from the content-addressed store, hash-verified

57}
58
59func (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

Callers 1

setActionsFromFlagsMethod · 0.95

Calls 3

quoteArgumentsFunction · 0.85
ErrorfMethod · 0.80
ReadMethod · 0.45

Tested by

no test coverage detected