Skip to content

Commit 7f79ec8

Browse files
committed
fix using ARGS macro in wrapped scripts
1 parent e7eea66 commit 7f79ec8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

pkg/snclient/check_wrap.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ func (l *CheckWrap) Check(ctx context.Context, snc *Agent, check *CheckData, _ [
4949
var command string
5050
if l.wrapped {
5151
// substitute $ARGn$ in the command
52+
log.Debugf("command string: %s", l.commandString)
53+
macros["ARGS"] = strings.Join(check.rawArgs, " ")
54+
macros["ARGS\""] = stringJoinQuoted(check.rawArgs, " ")
5255
commandString := ReplaceRuntimeMacros(l.commandString, macros)
56+
5357
cmdToken := utils.Tokenize(commandString)
5458
macros["SCRIPT"] = cmdToken[0]
5559
macros["ARGS"] = strings.Join(cmdToken[1:], " ")
60+
macros["ARGS\""] = stringJoinQuoted(cmdToken[1:], " ")
5661
ext := strings.TrimPrefix(filepath.Ext(cmdToken[0]), ".")
5762
log.Debugf("command wrapping for extension: %s", ext)
5863
wrapping, ok := snc.config.Section("/settings/external scripts/wrappings").GetString(ext)
@@ -62,18 +67,13 @@ func (l *CheckWrap) Check(ctx context.Context, snc *Agent, check *CheckData, _ [
6267
wrapping = l.fixWrappingCmd(ext, wrapping)
6368
log.Debugf("%s wrapper: %s", ext, wrapping)
6469
command = ReplaceRuntimeMacros(wrapping, macros)
70+
log.Debugf("command after macros expanded: %s", command)
6571
} else {
6672
macros["ARGS"] = strings.Join(check.rawArgs, " ")
67-
macros["ARGS\""] = strings.Join(func(arr []string) []string {
68-
quoteds := make([]string, len(arr))
69-
for i, v := range arr {
70-
quoteds[i] = fmt.Sprintf("%q", v)
71-
}
72-
73-
return quoteds
74-
}(check.rawArgs), " ")
73+
macros["ARGS\""] = stringJoinQuoted(check.rawArgs, " ")
7574
log.Debugf("command before macros expanded: %s", l.commandString)
7675
command = ReplaceRuntimeMacros(l.commandString, macros)
76+
log.Debugf("command after macros expanded: %s", command)
7777
}
7878

7979
timeoutSeconds := check.timeout
@@ -133,3 +133,12 @@ func (l *CheckWrap) fixWrappingCmd(ext, wrapping string) string {
133133

134134
return wrapping
135135
}
136+
137+
func stringJoinQuoted(arr []string, sep string) string {
138+
quoteds := make([]string, len(arr))
139+
for i, v := range arr {
140+
quoteds[i] = fmt.Sprintf("%q", v)
141+
}
142+
143+
return strings.Join(quoteds, sep)
144+
}

pkg/snclient/check_wrap_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ check_win_subargs = t\scripts\check_args.ps1 $ARG1$
6262
[/settings/external scripts/wrapped scripts]
6363
check_dummy_wrapped_noparm = check_dummy.EXTENSION
6464
check_dummy_wrapped = check_dummy.EXTENSION $ARG1$ "$ARG2$"
65+
check_dummy_wrapped_args = check_dummy.EXTENSION $ARGS"$
6566
check_dummy_wrapped_ok = check_dummy.EXTENSION 0 "i am ok wrapped"
6667
check_dummy_wrapped_critical = check_dummy.EXTENSION 2 "i am critical wrapped"
6768
@@ -160,6 +161,10 @@ func runTestCheckExternalWrapped(t *testing.T, snc *Agent) {
160161
assert.Equalf(t, CheckExitOK, res.State, "state matches")
161162
assert.Equalf(t, "OK: i am wrapped", string(res.BuildPluginOutput()), "output matches")
162163

164+
res = snc.RunCheck("check_dummy_wrapped_args", []string{"0", "i am wrapped"})
165+
assert.Equalf(t, CheckExitOK, res.State, "state matches")
166+
assert.Equalf(t, "OK: i am wrapped", string(res.BuildPluginOutput()), "output matches")
167+
163168
res = snc.RunCheck("check_dummy_wrapped_ok", []string{})
164169
assert.Equalf(t, CheckExitOK, res.State, "state matches")
165170
assert.Equalf(t, "OK: i am ok wrapped", string(res.BuildPluginOutput()), "output matches")

0 commit comments

Comments
 (0)