aboutsummaryrefslogtreecommitdiff
path: root/clients/bokfctl.c
blob: b5ecf4787211c9ace8df16dfd71dc216d26babcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "client.h"
#include "util.h"
#include "version.h"
#include "yyjson.h"

static void usage(void)
{
    fprintf(stderr,
            "usage: bokfctl [options] <cmd> [args-json]\n"
            "\n"
            "options:\n"
            "  --socket TARGET  unix path, tcp:host:port or tls:host:port\n"
            "                   (env BOKFD_SOCKET, default /run/bokfd/bokfd.sock)\n"
            "  --user NAME      login user (env BOKFD_USER)\n"
            "  --password PW    login password (env BOKFD_PASSWORD)\n"
            "  --token TOKEN    API token instead of user/password (env BOKFD_TOKEN)\n"
            "  --org ID         active org for this request\n"
            "  --version\n"
            "\n"
            "examples:\n"
            "  bokfctl health\n"
            "  bokfctl org.create '{\"name\":\"AB Ett\"}'\n"
            "  bokfctl describe\n");
}

static int is_local_cmd(const char *cmd)
{
    return strcmp(cmd, "health") == 0 || strcmp(cmd, "meta") == 0 ||
           strcmp(cmd, "session.open") == 0;
}

int main(int argc, char **argv)
{
    const char *target = getenv("BOKFD_SOCKET");
    if (!target)
        target = "/run/bokfd/bokfd.sock";
    const char *user = getenv("BOKFD_USER");
    const char *password = getenv("BOKFD_PASSWORD");
    const char *token = getenv("BOKFD_TOKEN");
    int64_t org = 0;
    const char *cmd = NULL;
    const char *args_json = NULL;

    for (int i = 1; i < argc; i++) {
        const char *a = argv[i];
        const char *val = NULL;
        if (!strncmp(a, "--socket", 8) &&
            (a[8] == '\0' || a[8] == '=')) {
            val = a[8] == '=' ? a + 9 : (i + 1 < argc ? argv[++i] : NULL);
            if (!val) {
                fprintf(stderr, "bokfctl: --socket requires a value\n");
                return 2;
            }
            target = val;
        } else if (!strncmp(a, "--user", 6) &&
                   (a[6] == '\0' || a[6] == '=')) {
            val = a[6] == '=' ? a + 7 : (i + 1 < argc ? argv[++i] : NULL);
            if (!val) {
                fprintf(stderr, "bokfctl: --user requires a value\n");
                return 2;
            }
            user = val;
        } else if (!strncmp(a, "--password", 10) &&
                   (a[10] == '\0' || a[10] == '=')) {
            val = a[10] == '=' ? a + 11 : (i + 1 < argc ? argv[++i] : NULL);
            if (!val) {
                fprintf(stderr, "bokfctl: --password requires a value\n");
                return 2;
            }
            password = val;
        } else if (!strncmp(a, "--token", 7) &&
                   (a[7] == '\0' || a[7] == '=')) {
            val = a[7] == '=' ? a + 8 : (i + 1 < argc ? argv[++i] : NULL);
            if (!val) {
                fprintf(stderr, "bokfctl: --token requires a value\n");
                return 2;
            }
            token = val;
        } else if (!strncmp(a, "--org", 5) &&
                   (a[5] == '\0' || a[5] == '=')) {
            val = a[5] == '=' ? a + 6 : (i + 1 < argc ? argv[++i] : NULL);
            if (!val) {
                fprintf(stderr, "bokfctl: --org requires a value\n");
                return 2;
            }
            org = strtoll(val, NULL, 10);
        } else if (strcmp(a, "--version") == 0) {
            printf("bokfctl %s\n", BOKF_VERSION);
            return 0;
        } else if (strcmp(a, "--help") == 0 || strcmp(a, "-h") == 0) {
            usage();
            return 0;
        } else if (a[0] == '-' && a[1] != '\0') {
            fprintf(stderr, "bokfctl: unknown option %s\n", a);
            usage();
            return 2;
        } else if (!cmd) {
            cmd = a;
        } else if (!args_json) {
            args_json = a;
        } else {
            fprintf(stderr, "bokfctl: unexpected argument %s\n", a);
            return 2;
        }
    }
    if (!cmd) {
        usage();
        return 2;
    }

    struct client_conn conn;
    if (client_connect(target, &conn) != 0) {
        fprintf(stderr, "bokfctl: cannot connect to %s: %s\n", target,
                client_last_error());
        return 2;
    }

    char *session = NULL;
    if (!is_local_cmd(cmd)) {
        char *lerr = NULL;
        int login_rc;
        if (token && *token) {
            login_rc = client_token_login(&conn, token, &session, &lerr);
        } else if (user && password) {
            login_rc = client_login(&conn, user, password, &session, &lerr);
        } else {
            fprintf(stderr,
                    "bokfctl: set BOKFD_TOKEN or BOKFD_USER/BOKFD_PASSWORD (or --token/--user/--password) to log in\n");
            client_close(&conn);
            return 2;
        }
        if (login_rc != 0) {
            fprintf(stderr, "%s\n", lerr ? lerr : "login failed");
            int rc = lerr && lerr[0] == '{' ? 1 : 2;
            free(lerr);
            client_close(&conn);
            return rc;
        }
    }

    char *req = NULL;
    if (strcmp(cmd, "raw") == 0) {
        if (!args_json) {
            fprintf(stderr, "bokfctl: raw requires a full request JSON\n");
            client_close(&conn);
            return 2;
        }
        req = xstrdup(args_json);
    } else {
        req = client_make_request(cmd, session, org, args_json, "cli");
        if (!req) {
            fprintf(stderr, "bokfctl: args must be a JSON object\n");
            free(session);
            client_close(&conn);
            return 2;
        }
    }
    free(session);
    if (client_send_line(&conn, req) != 0) {
        fprintf(stderr, "bokfctl: send failed: %s\n", client_last_error());
        free(req);
        client_close(&conn);
        return 2;
    }
    free(req);

    char *resp = client_read_line(&conn);
    client_close(&conn);
    if (!resp) {
        fprintf(stderr, "bokfctl: no response: %s\n", client_last_error());
        return 2;
    }
    yyjson_doc *rd = yyjson_read(resp, strlen(resp), 0);
    char *pretty = rd ? yyjson_write(rd, YYJSON_WRITE_PRETTY, NULL) : NULL;
    printf("%s\n", pretty ? pretty : resp);
    int ok = rd && yyjson_is_obj(yyjson_doc_get_root(rd)) &&
             yyjson_get_bool(yyjson_obj_get(yyjson_doc_get_root(rd), "ok"));
    if (rd)
        yyjson_doc_free(rd);
    free(pretty);
    free(resp);
    return ok ? 0 : 1;
}