summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authornirav <nirav@teisuu.com>2019-03-10 23:45:19 +0530
committerDandelion <nirav@teisuu.com>2019-03-11 23:05:22 +0530
commit63e8c7f9d095fe0b1e0b44f950230c0e238f166d (patch)
tree01e0211f4fdede1eccda010fc944b3a0180a9632 /src/config.c
parent7e8ea36eb8106b4847af13a27a3e8f3177d25809 (diff)
downloadap_client-63e8c7f9d095fe0b1e0b44f950230c0e238f166d.tar.gz
ap_client-63e8c7f9d095fe0b1e0b44f950230c0e238f166d.zip
Add config and log
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..c4aecf0
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,146 @@
+#define _POSIX_C_SOURCE 200809L
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <err.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <limits.h>
+#include <libgen.h>
+#include <jansson.h>
+#include "string-util.h"
+#include "config.h"
+#include "log.h"
+
+#define CONFIG_PATH_SUFFIX "/.config/ap_client/config"
+
+struct config _config;
+const struct config *config = (const struct config *)&_config;
+
+char *get_config_path()
+{
+ char *home = getenv("HOME");
+ if (!home || !*home) {
+ return NULL;
+ }
+ char config_path[PATH_MAX];
+ strlcpy(config_path, home, sizeof(config_path));
+ strlcat(config_path, CONFIG_PATH_SUFFIX, sizeof(config_path));
+ return strdup(config_path);
+}
+
+void config_load()
+{
+ json_t *root;
+ json_error_t error;
+
+ char *path = get_config_path();
+ root = json_load_file(path, 0, &error);
+ if (!root) {
+ log_msg(LOG_ERROR, "load_config", "json parse error: line %d: %s",
+ error.line, error.text);
+ free(path);
+ return;
+ }
+ free(path);
+
+ if (!json_is_object(root)) {
+ log_msg(LOG_ERROR, "load_config", "json root is not object");
+ json_decref(root);
+ return;
+ }
+
+ json_t *instance_url = json_object_get(root, "instance_url");
+ if (json_is_string(instance_url)) {
+ _config.instance_url = strdup(json_string_value(instance_url));
+ }
+
+ json_t *client_id = json_object_get(root, "client_id");
+ if (json_is_string(client_id)) {
+ _config.client_id = strdup(json_string_value(client_id));
+ }
+
+ json_t *client_secret = json_object_get(root, "client_secret");
+ if (json_is_string(client_secret)) {
+ _config.client_secret = strdup(json_string_value(client_secret));
+ }
+
+ json_t *access_token = json_object_get(root, "access_token");
+ if (json_is_string(access_token)) {
+ _config.access_token = strdup(json_string_value(access_token));
+ }
+
+ json_decref(root);
+}
+
+int config_save()
+{
+ json_t *root;
+ json_error_t error;
+
+ root = json_pack_ex(&error, 1, "{s:s, s:s, s:s, s:s}", "instance_url",
+ _config.instance_url, "client_id", _config.client_id,
+ "client_secret", _config.client_secret, "access_token",
+ _config.access_token);
+
+ if (!root) {
+ log_msg(LOG_ERROR, "register_app", "json pack error: line %d: %s",
+ error.line, error.text);
+ return -1;
+ }
+
+ char *config_path = get_config_path();
+ struct stat st;
+ char *dir = dirname(config_path);
+ if (stat(dir, &st)) {
+ if (mkdir(dir, S_IRWXU)) {
+ free(config_path);
+ log_msg(LOG_ERROR, "load_config", "failed to create config dir");
+ return -1;
+ }
+ }
+ free(config_path);
+
+ if (json_dump_file(root, get_config_path(), 0)) {
+ log_msg(LOG_ERROR, "load_config", "unable to save json");
+ json_decref(root);
+ return -1;
+ }
+
+ json_decref(root);
+ return 0;
+}
+
+void config_set_client_id(const char *cid)
+{
+ _config.client_id = strdup(cid);
+}
+
+void config_set_client_secret(const char *cs)
+{
+ _config.client_secret = strdup(cs);
+}
+
+void config_set_instance_url(const char *iu)
+{
+ printf("updating url: %s", iu);
+ _config.instance_url = strdup(iu);
+}
+
+void config_set_access_token(const char *a)
+{
+ _config.access_token = strdup(a);
+}
+
+void config_cleanup()
+{
+ if (_config.client_id)
+ free(_config.client_id);
+ if (_config.client_secret)
+ free(_config.client_secret);
+ if (_config.instance_url)
+ free(_config.instance_url);
+ if (_config.access_token)
+ free(_config.access_token);
+}