Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Implement real arguments for lua script from keywords
Baptiste Daroussin committed 5 years ago
commit 6d75f8c1f9d5a68a986132d0703715abbe15d6a5
parent 7193046
3 files changed +48 -3
modified libpkg/lua_scripts.c
@@ -122,6 +122,23 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type, bool upgrade)
				err(1, "cap_enter failed");
			}
#endif
+
			/* parse and set arguments of the line is in the comments */
+
			if (STARTS_WITH(lscript->script, "-- args: ")) {
+
				char *walk, *begin, *line = NULL;
+
				int spaces, argc = 0;
+
				char **args = NULL;
+

+
				walk = strchr(lscript->script, '\n');
+
				begin = lscript->script + strlen("-- args: ");
+
				line = xstrndup(begin, walk - begin);
+
				spaces = pkg_utils_count_spaces(line);
+
				args = xmalloc((spaces + 1)* sizeof(char *));
+
				walk = xstrdup(line);
+
				while (walk != NULL) {
+
					args[argc++] = pkg_utils_tokenize(&walk);
+
				}
+
				lua_args_table(L, args, argc);
+
			}

			pkg_debug(3, "Scripts: executing lua\n--- BEGIN ---\n%s\nScripts: --- END ---", lscript->script);
			if (luaL_dostring(L, lscript->script)) {
modified libpkg/utils.c
@@ -189,7 +189,7 @@ format_exec_cmd(char **dest, const char *in, const char *prefix,

	buf = xstring_new();

-
	if (line != NULL) {
+
	if (line != NULL && argv != NULL) {
		if (lua) {
			fprintf(buf->fp, "-- args: %s\n", line);
		} else {
modified tests/frontend/create.sh
@@ -22,6 +22,7 @@ tests_init \
	create_no_clobber \
	time \
	create_from_plist_keyword_validation \
+
	create_from_plist_keyword_real_args \

genmanifest() {
	cat << EOF >> +MANIFEST
@@ -245,8 +246,35 @@ create_from_plist_bad_fflags_body() {
		pkg create -o ${TMPDIR} -m . -p test.plist -r .
}

-
create_from_plist_keyword_validation_body()
-
{
+
create_from_plist_keyword_real_args_body() {
+
	preparetestcredentials "test"
+

+
cat << EOF > test.ucl
+
actions: []
+
arguments: true
+
post-install-lua: <<EOS
+
if arg ~= nil then
+
	print("yes")
+
end
+
for i = 1, #arg do
+
	print(arg[i])
+
end
+
EOS
+
EOF
+

+
	genplist "@test A B C D"
+

+
mkdir target
+

+
	atf_check \
+
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .
+

+
	atf_check \
+
		-o inline:"yes\nfile1\nyes\nA\nB\nC\nD\n" \
+
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
+
}
+

+
create_from_plist_keyword_validation_body() {
	preparetestcredentials "test"

cat << EOF > test.ucl