dav/main.c

changeset 194
1950f483d3c4
parent 191
0e45b04236a7
child 195
4b7823a58199
--- a/dav/main.c	Sat Feb 27 17:23:36 2016 +0100
+++ b/dav/main.c	Sat Feb 27 19:39:55 2016 +0100
@@ -43,6 +43,7 @@
 #include <libidav/session.h>
 #include "config.h"
 #include "error.h"
+#include "assistant.h"
 #include "main.h"
 
 static DavContext *ctx;
@@ -120,6 +121,8 @@
         ret = cmd_get_property(args);
     } else if(!strcasecmp(cmd, "info")) {
         ret = cmd_info(args);
+    } else if(!strcasecmp(cmd, "create-repository")) {
+        ret = cmd_create_repository(args);
     } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version") || !strcasecmp(cmd, "--version")) {
 #ifdef DEBUG
         fprintf(stderr, "dav %s unstable\n", DAV_VERSION);
@@ -180,6 +183,9 @@
     fprintf(stderr, "        -n <uri>   specify namespace uri\n");
     fprintf(stderr, "        -v         verbose output\n");
     fprintf(stderr, "\n");
+    fprintf(stderr, "Config Commands:\n");
+    fprintf(stderr, "        add-repository\n");
+    fprintf(stderr, "\n");
     fprintf(stderr,
             "Instead of an url you can pass a repository name "
             "with an optional path:\n");
@@ -1150,73 +1156,53 @@
     }
 }
 
+
 /* ---------- config commands ---------- */
 
-static int getkeyvalue(char *arg, char **key, char **value) {
-    // splits a key=value arg
+int cmd_create_repository(CmdArgs *args) {
+    sstr_t line;
     
-    *key = NULL;
-    *value = NULL;
-    if(!arg && !key && !value) {
+    printf("Each repository must have an unique name.\n");
+    char *name = assistant_getcfg("name");
+    if(!name) {
+        fprintf(stderr, "Abort\n");
+        return -1;
+    }
+    if(get_repository(sstr(name))) {
+        fprintf(stderr, "Repository %s already exists.\nAbort\n");
         return -1;
     }
     
-    int haskey = 0;
-    size_t len = strlen(arg);
-    
-    if(len < 3) {
+    printf("\nSpecify the repository base url.\n");
+    char *url = assistant_getcfg("url");
+    if(!url) {
+        fprintf(stderr, "Abort\n");
         return -1;
     }
     
-    int i;
-    for(i=0;i<len;i++) {
-        if(arg[i] == '=') {
-            haskey = 1;
-            break;
-        }
+    printf("\nUser for HTTP authentication.\n");
+    char *user = assistant_getoptcfg("user");
+    
+    char *password = NULL;
+    if(user) {
+        password = assistant_gethiddenoptcfg("password");
     }
+    printf("\n");
     
-    if(haskey) {
-        sstr_t k = sstrn(arg, i);
-        sstr_t v = sstrn(arg+i + 1, len - i - 1);
-        if(k.length > 0 && v.length > 0) {
-            *key = sstrdup(k).ptr;
-            *value = sstrdup(v).ptr;
-            return 0;
-        }
+    Repository repo;
+    memset(&repo, 0, sizeof(Repository));
+    repo.name = name;
+    repo.url = url;
+    repo.user = user;
+    repo.password = password;
+    
+    int ret = 0;
+    if(add_repository(&repo)) {
+        fprintf(stderr, "Cannot write config.xml\n");
+        ret = -1;
+    } else {
+        printf("Added repository: %s (%s)\n", name, url);
     }
     
-    return -1;
+    return ret;
 }
-
-void repository_assistent(Repository *repo, int mode) {
-    char *name = NULL;
-    sstr_t line;
-    
-    // name
-    while(!name) {
-        if(repo->name) {
-            printf("repository name [%s]: ", repo->name);
-        } else {
-            printf("repository name: ");
-        }
-        fflush(stdout);
-        
-        line = util_readline(stdin);
-        if(line.length == 0 && repo->name) {
-            break;
-        } else {
-            name = line.ptr;
-        }
-    }
-    if(name) {
-        if(repo->name) {
-            free(repo->name);
-        }
-        repo->name = name;
-    }
-    
-    // TODO
-    
-    return;
-}

mercurial