]> pd.if.org Git - zpackage/blob - zpm-note.c
fix status after dry run
[zpackage] / zpm-note.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <unistd.h>
8
9 #include "zpm.h"
10
11 void usage(void) {
12         fprintf(stderr, "zpm-note [-I] [-s <status> ...] [-S <status>] [package]\n");
13 }
14
15 int main(int ac, char **av){
16         int opt;
17         struct zpm pkg;
18         char *dbfile;
19         int fail;
20
21         int select = 1;
22
23         struct zpm_note n = { 0 };
24         n.pkgid = 0;
25         n.hash = 0;
26         n.path = 0;
27
28         dbfile = getenv("ZPMDB");
29         if (!dbfile) {
30                 dbfile = "/var/lib/zpm/local.db";
31         }
32
33         /* -l list
34          * -p packageid
35          * -A ack
36          * -n note id
37          * -D delete
38          * -H hash
39          * -a all
40          * -m note message, otherwise EDITOR/vi to edit a text file
41          * zpm note -p pkgid -P path -H hash -m 'foo'
42          * zpm note -l
43          * zpm note -D <n>
44          * zpm note -A <n>
45          */
46         int list = 0, delete = 0, ack = 0;
47         while ((opt = getopt(ac, av, "f:p:P:H:m:n:lDAa")) != -1) {
48                 switch (opt) {
49                         case 'f': dbfile = optarg; break;
50                         case 'p': n.pkgid = optarg; break;
51                         case 'P': n.path = optarg; break;
52                         case 'H': n.hash = optarg; break;
53                         case 'm': n.note = optarg; break;
54                         case 'n': n.id = strtoll(optarg, NULL, 10); break;
55                         case 'l': list = 1; break;
56                         case 'D': delete = 1; break;
57                         case 'A': ack = 1; break;
58                         case 'a': select = 3; break;
59
60                         default:
61                                   usage();
62                                   exit(EXIT_FAILURE);
63                                   break;
64                 }
65         }
66
67         if (!dbfile) {
68                 fprintf(stderr, "must specify db\n");
69                 return 1;
70         }
71
72         /* given a package name, get the packages */
73         /* no package name, get all */
74
75         if (zpm_open(&pkg, dbfile)) {
76                 if (list) {
77                         while (zpm_note(&pkg, &n, select)) {
78                                 printf("%" PRId64 " %s %s\n", n.id,
79                                                 n.pkgid ? n.pkgid : "-",
80                                                 n.note);
81                                 zpm_note_free(&n);
82                         }
83                 } else if (n.note) {
84                         n.id = zpm_note_add(&pkg, n.pkgid, n.path, n.hash, "%s", n.note);
85                         if (n.id) {
86                                 printf("%" PRId64 "\n", n.id);
87                         } else {
88                                 fprintf(stderr, "unable to create note\n");
89                                 pkg.error = 1;
90                         }
91                 } else if (delete) {
92                         zpm_note_del(&pkg, n.id);
93                 } else if (ack) {
94                         zpm_note_ack(&pkg, n.id);
95                 }
96         } else {
97                 fprintf(stderr, "unable to open %s\n", dbfile);
98         }
99         fail = pkg.error;
100         zpm_close(&pkg);
101         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
102 }