-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosc-get.c
98 lines (90 loc) · 2.14 KB
/
osc-get.c
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json-c/json.h>
#define NO_NEW_LINE 1
const char *target_user;
const char *target_data;
void print_users(struct json_object *js_f, const char *u)
{
json_object_object_foreach(js_f, k, v) {
if (u && strcmp(u, k))
continue;
printf("Acount: '%s'\n", k);
printf("ak: %s\nsk: %s\nregion: %s\n",
json_object_to_json_string(
json_object_object_get(v, "access_key")),
json_object_to_json_string(
json_object_object_get(v, "secret_key")),
json_object_to_json_string(
json_object_object_get(v, "region")));
}
}
int main(int ac, char **av)
{
const char *dest0 = "/.osc/config.json";
const char *dest1 = "/.oapi_credentials";
char buf[1024];
char *home = getenv("HOME");
struct json_object *js_f;
int mode = 0;
int flag = 0;
int i;
for (i = 1; i < ac; ++i) {
if (!strcmp(av[i], "-u")) {
mode = 1;
if (i + 1 >= ac) {
fprintf(stderr, "wrong argument\n");
return 1;
}
target_user = av[i + 1];
++i;
} else if (!strcmp(av[i], "-ud")) {
mode = 2;
if (i + 2 >= ac) {
fprintf(stderr, "wrong argument\n");
return 1;
}
target_user = av[i + 1];
if (!strcmp(av[i+2], "ak") ||
!strcmp(av[i+2], "AK")) {
target_data = "access_key";
} else if (!strcmp(av[i+2], "sk") ||
!strcmp(av[i+2], "SK")) {
target_data = "secret_key";
} else {
target_data = av[i + 2];
}
i += 2;
} else if (!strcmp(av[i], "-n")) {
flag |= NO_NEW_LINE;
} else {
fprintf(stderr, "unknow argument\n");
return 1;
}
}
strcpy(stpcpy(buf, home), dest0);
if (access(buf, F_OK ) != 0)
strcpy(stpcpy(buf, home), dest1);
js_f = json_object_from_file(buf);
if (!js_f) {
fprintf(stderr, "can't open %s\n", buf);
return 1;
}
if (mode == 0 || mode == 1)
print_users(js_f, target_user);
else if (mode == 2) {
printf("%s",
json_object_get_string(
json_object_object_get(
json_object_object_get(js_f,
target_user),
target_data)));
if (flag & NO_NEW_LINE)
fflush(stdout);
else
printf("\n");
}
return 0;
}