Add a way to set arbitrary data accessible by all tasks. Use it to allow autologin for testing multiple instances more easily.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3689 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-12-01 23:29:53 +00:00
parent 00c1ec660e
commit 84a3d7348d
4 changed files with 42 additions and 2 deletions

View File

@ -196,8 +196,9 @@ function getPermissionsForUser(userName) {
function query(headers) {
var session = getCookies(headers).session;
var entry;
if (entry = readSession(session)) {
return {session: entry, permissions: getPermissions(session)};
var autologin = tildefriends.args.autologin;
if (entry = autologin ? {name: autologin} : readSession(session)) {
return {session: entry, permissions: autologin ? getPermissionsForUser(autologin) : getPermissions(session)};
}
}

View File

@ -306,6 +306,7 @@ typedef struct tf_run_args_t {
const char* db_path;
const char* secrets_path;
int count;
const char* args;
bool help;
} tf_run_args_t;
@ -324,6 +325,7 @@ static int _tf_run_task(const tf_run_args_t* args, int index)
tf_task_set_ssb_port(task, args->ssb_port ? args->ssb_port + index : 0);
tf_task_set_http_port(task, args->http_port ? args->http_port + index : 0);
tf_task_set_https_port(task, args->https_port ? args->https_port + index : 0);
tf_task_set_args(task, args->args);
const char* db_path = args->db_path;
const char* secrets_path = args->secrets_path;
char db_path_buffer[256];
@ -363,6 +365,7 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
{ "db-path", 'd', offsetof(tf_run_args_t, db_path), NULL, XOPT_TYPE_STRING, NULL, "Sqlite database path (default: db.sqlite)." },
{ "secrets-path", 'i', offsetof(tf_run_args_t, secrets_path), NULL, XOPT_TYPE_STRING, NULL, "Secrets/identity path." },
{ "count", 'n', offsetof(tf_run_args_t, count), NULL, XOPT_TYPE_INT, NULL, "Number of instances to run." },
{ "args", 'a', offsetof(tf_run_args_t, args), NULL, XOPT_TYPE_STRING, NULL, "Arguments of the form key=value,foo=bar,verbose=true." },
{ "help", 'h', offsetof(tf_run_args_t, help), NULL, XOPT_TYPE_BOOL, NULL, "Shows this help message." },
XOPT_NULLOPTION,
};

View File

@ -107,6 +107,7 @@ typedef struct _tf_task_t {
int _https_port;
char _db_path[256];
char _secrets_path[256];
const char* _args;
} tf_task_t;
typedef struct _export_record_t {
@ -1260,6 +1261,35 @@ void tf_task_activate(tf_task_t* task)
JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, task->_ssb_port));
JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port));
JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port));
JSValue args = JS_NewObject(context);
JS_SetPropertyStr(context, tildefriends, "args", args);
if (task->_args)
{
char* saveptr = NULL;
char* copy = strdup(task->_args);
char* start = copy;
while (true)
{
char* assignment = strtok_r(start, ",", &saveptr);
start = NULL;
if (!assignment)
{
break;
}
char* equals = strchr(assignment, '=');
if (equals)
{
*equals = '\0';
JS_SetPropertyStr(context, args, assignment, JS_NewString(context, equals + 1));
}
else
{
printf("Assignment missing '=': %s.\n", assignment);
exit(1);
}
}
free(copy);
}
JS_SetPropertyStr(context, global, "tildefriends", tildefriends);
if (task->_trusted)
@ -1463,6 +1493,11 @@ void tf_task_set_secrets_path(tf_task_t* task, const char* secrets_path)
snprintf(task->_secrets_path, sizeof(task->_secrets_path), "%s", secrets_path);
}
void tf_task_set_args(tf_task_t* task, const char* args)
{
task->_args = args;
}
const char* tf_task_get_name(tf_task_t* task)
{
return task->_scriptName;

View File

@ -40,6 +40,7 @@ void tf_task_set_http_port(tf_task_t* task, int port);
void tf_task_set_https_port(tf_task_t* task, int port);
void tf_task_set_db_path(tf_task_t* task, const char* path);
void tf_task_set_secrets_path(tf_task_t* task, const char* path);
void tf_task_set_args(tf_task_t* task, const char* args);
void tf_task_activate(tf_task_t* task);
void tf_task_run(tf_task_t* task);
int tf_task_execute(tf_task_t* task, const char* file);