]> pd.if.org Git - zpackage/commitdiff
use default db for note
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 4 Nov 2018 14:02:03 +0000 (14:02 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sun, 4 Nov 2018 14:02:03 +0000 (14:02 +0000)
zpm-note.c [new file with mode: 0644]

diff --git a/zpm-note.c b/zpm-note.c
new file mode 100644 (file)
index 0000000..05ecda2
--- /dev/null
@@ -0,0 +1,102 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <unistd.h>
+
+#include "zpm.h"
+
+void usage(void) {
+       fprintf(stderr, "zpm-note [-I] [-s <status> ...] [-S <status>] [package]\n");
+}
+
+int main(int ac, char **av){
+       int opt;
+       struct zpm pkg;
+       char *dbfile;
+       int fail;
+
+       int select = 1;
+
+       struct zpm_note n = { 0 };
+       n.pkgid = 0;
+       n.hash = 0;
+       n.path = 0;
+
+       dbfile = getenv("ZPMDB");
+       if (!dbfile) {
+               dbfile = "/var/lib/zpm/local.db";
+       }
+
+       /* -l list
+        * -p packageid
+        * -A ack
+        * -n note id
+        * -D delete
+        * -H hash
+        * -a all
+        * -m note message, otherwise EDITOR/vi to edit a text file
+        * zpm note -p pkgid -P path -H hash -m 'foo'
+        * zpm note -l
+        * zpm note -D <n>
+        * zpm note -A <n>
+        */
+       int list = 0, delete = 0, ack = 0;
+       while ((opt = getopt(ac, av, "f:p:P:H:m:n:lDAa")) != -1) {
+               switch (opt) {
+                       case 'f': dbfile = optarg; break;
+                       case 'p': n.pkgid = optarg; break;
+                       case 'P': n.path = optarg; break;
+                       case 'H': n.hash = optarg; break;
+                       case 'm': n.note = optarg; break;
+                       case 'n': n.id = strtoll(optarg, NULL, 10); break;
+                       case 'l': list = 1; break;
+                       case 'D': delete = 1; break;
+                       case 'A': ack = 1; break;
+                       case 'a': select = 3; break;
+
+                       default:
+                                 usage();
+                                 exit(EXIT_FAILURE);
+                                 break;
+               }
+       }
+
+       if (!dbfile) {
+               fprintf(stderr, "must specify db\n");
+               return 1;
+       }
+
+       /* given a package name, get the packages */
+       /* no package name, get all */
+
+       if (zpm_open(&pkg, dbfile)) {
+               if (list) {
+                       while (zpm_note(&pkg, &n, select)) {
+                               printf("%" PRId64 " %s %s\n", n.id,
+                                               n.pkgid ? n.pkgid : "-",
+                                               n.note);
+                               zpm_note_free(&n);
+                       }
+               } else if (n.note) {
+                       n.id = zpm_note_add(&pkg, n.pkgid, n.path, n.hash, "%s", n.note);
+                       if (n.id) {
+                               printf("%" PRId64 "\n", n.id);
+                       } else {
+                               fprintf(stderr, "unable to create note\n");
+                               pkg.error = 1;
+                       }
+               } else if (delete) {
+                       zpm_note_del(&pkg, n.id);
+               } else if (ack) {
+                       zpm_note_ack(&pkg, n.id);
+               }
+       } else {
+               fprintf(stderr, "unable to open %s\n", dbfile);
+       }
+       fail = pkg.error;
+       zpm_close(&pkg);
+       return fail ? EXIT_FAILURE : EXIT_SUCCESS;
+}