Skip to content

Commit

Permalink
code initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Dec 31, 2020
1 parent 2616017 commit 6817804
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
osc-get: osc-get.o
$(CC) -ljson-c -pedantic -Wall -Wextra osc-get.o -o osc-get
92 changes: 92 additions & 0 deletions osc-get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#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 *dest = "/.oapi_credentials";
char buf[1024];
char *home = getenv("HOME");
struct json_object *js_f;
int mode = 0;
int flag = 0;

for (int 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), dest);
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;
}

0 comments on commit 6817804

Please sign in to comment.