diff scictl.c @ 2:5fa3d2f479b2

sci: initial upload support
author David Demelier <markand@malikania.fr>
date Thu, 10 Jun 2021 10:39:21 +0200
parents 5afdb14df924
children 215c0c3b3609
line wrap: on
line diff
--- a/scictl.c	Tue Jun 08 08:40:01 2021 +0200
+++ b/scictl.c	Thu Jun 10 10:39:21 2021 +0200
@@ -24,22 +24,56 @@
 help(void)
 {
 	fprintf(stderr, "usage: %s job-queue project tag\n", getprogname());
-	fprintf(stderr, "       %s job-list project\n", getprogname());
+	fprintf(stderr, "       %s job-list worker\n", getprogname());
 	fprintf(stderr, "       %s job-save id worker status retcode console\n", getprogname());
 	fprintf(stderr, "       %s project-add name desc url script\n", getprogname());
 	fprintf(stderr, "       %s project-list\n", getprogname());
+	fprintf(stderr, "       %s script-get project\n", getprogname());
 	fprintf(stderr, "       %s worker-add name desc\n", getprogname());
 	fprintf(stderr, "       %s worker-list\n", getprogname());
 	exit(0);
 }
 
+static char *
+readfile(const char *path)
+{
+	FILE *fp, *str;
+	static char console[SCI_MSG_MAX];
+	char buf[BUFSIZ], *ret = console;
+	size_t nr;
+
+	if (strcmp(path, "-") == 0)
+		fp = stdin;
+	else if (!(fp = fopen(path, "r")))
+		err(1, "%s", path);
+
+	if (!(str = fmemopen(console, sizeof (console), "w")))
+		err(1, "fmemopen");
+
+	while ((nr = fread(buf, 1, sizeof (buf), fp)) > 0)
+		fwrite(buf, 1, nr, str);
+
+	if ((ferror(fp) && !feof(fp)) || (ferror(str) && !feof(str)))
+		ret = NULL;
+
+	fclose(str);
+	fclose(fp);
+
+	return ret;
+}
+
 static struct req
 cmd_job_queue(int argc, char **argv)
 {
+	struct job job = {0};
+
 	if (argc < 2)
 		usage();
 
-	return req_job_queue(argv[0], argv[1]);
+	strlcpy(job.project.name, argv[0], sizeof (job.project.name));
+	strlcpy(job.tag, argv[1], sizeof (job.tag));
+
+	return req_job_queue(&job);
 }
 
 static struct req
@@ -55,11 +89,11 @@
 	if ((req = req_job_list(jobs, &jobsz, argv[0])).status)
 		return req;
 
-	printf("%-16s%-16s%-16s%s\n", "ID", "TAG", "PROJECT", "WORKER");
+	printf("%-16s%-16s%s\n", "ID", "TAG", "PROJECT");
 
 	for (size_t i = 0; i < jobsz; ++i) {
-		printf("%-16lld%-16s%-16s%s\n", (long long int)jobs[i].job.id,
-		    jobs[i].job.tag, jobs[i].job.project.name, jobs[i].worker.name);
+		printf("%-16lld%-16s%s\n", (long long int)jobs[i].job.id,
+		    jobs[i].job.tag, jobs[i].job.project.name);
 	}
 
 	return req;
@@ -68,10 +102,18 @@
 static struct req
 cmd_job_save(int argc, char **argv)
 {
+	struct job_result res = {0};
+
 	if (argc < 5)
 		usage();
 
-	return req_job_save(argv[0], argv[1], argv[2], argv[3], argv[4]);
+	res.job.id = strtoll(argv[0], NULL, 10);
+	res.status = strtoll(argv[2], NULL, 10);
+	res.retcode = strtoll(argv[3], NULL, 10);
+	res.console = readfile(argv[4]);
+	strlcpy(res.worker.name, argv[1], sizeof (res.worker.name));
+
+	return req_job_save(&res);
 }
 
 static struct req
@@ -116,6 +158,29 @@
 }
 
 static struct req
+cmd_script_get(int argc, char **argv)
+{
+	char script[SCI_MSG_MAX];
+	struct req req;
+
+	if (argc < 1)
+		usage();
+	if ((req = req_script_get(argv[0], script, sizeof (script))).status)
+		return req;
+
+	printf("%s", script);
+
+	/*
+	 * Don't break up the terminal output if the script does not contain a
+	 * final new line.
+	 */
+	if (script[strlen(script) - 1] != '\n')
+		printf("\n");
+
+	return req;
+}
+
+static struct req
 cmd_worker_add(int argc, char **argv)
 {
 	struct worker wk;
@@ -160,6 +225,7 @@
 	{ "job-save",           cmd_job_save            },
 	{ "project-add",        cmd_project_add         },
 	{ "project-list",       cmd_project_list        },
+	{ "script-get",         cmd_script_get          },
 	{ "worker-add",         cmd_worker_add          },
 	{ "worker-list",        cmd_worker_list         },
 	{ NULL,                 NULL                    }