add support for merged importing
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 0d663e1..828c7b9 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -108,6 +108,35 @@ static int package_cmd(int cmd, char *package)
 
 static int uci_do_import(int argc, char **argv)
 {
+       struct uci_package *package = NULL;
+       char **configs = NULL;
+       char *name = NULL;
+       int ret = UCI_OK;
+       char **p;
+
+       if (argc > 2)
+               return 255;
+
+       if (argc == 2)
+               name = argv[1];
+       else if (flags & CLI_FLAG_MERGE)
+               /* need a package to merge */
+               return 255;
+
+       if (flags & CLI_FLAG_MERGE) {
+               if (uci_load(ctx, name, &package) != UCI_OK)
+                       package = NULL;
+       }
+       ret = uci_import(ctx, input, name, &package, (name != NULL));
+       if ((ret == UCI_OK) && (flags & CLI_FLAG_MERGE)) {
+               ret = uci_save(ctx, package);
+       }
+
+       if (ret != UCI_OK) {
+               uci_perror(ctx, appname);
+               return 1;
+       }
+
        return 0;
 }
 
@@ -137,13 +166,14 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv)
 
 static int uci_do_section_cmd(int cmd, int argc, char **argv)
 {
+       struct uci_package *p = NULL;
+       struct uci_element *e = NULL;
        char *package = NULL;
        char *section = NULL;
        char *option = NULL;
        char *value = NULL;
        char **ptr = NULL;
-       struct uci_package *p = NULL;
-       struct uci_element *e = NULL;
+       int ret = UCI_OK;
 
        if (argc != 2)
                return 255;
@@ -184,22 +214,13 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                printf("%s\n", value);
                break;
        case CMD_RENAME:
-               if (uci_rename(ctx, p, section, option, value) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_rename(ctx, p, section, option, value);
                break;
        case CMD_SET:
-               if (uci_set(ctx, p, section, option, value) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_set(ctx, p, section, option, value);
                break;
        case CMD_DEL:
-               if (uci_delete(ctx, p, section, option) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_delete(ctx, p, section, option);
                break;
        }
 
@@ -208,7 +229,10 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                return 0;
 
        /* save changes, but don't commit them yet */
-       if (uci_save(ctx, p) != UCI_OK) {
+       if (ret == UCI_OK)
+               ret = uci_save(ctx, p);
+
+       if (ret != UCI_OK) {
                uci_perror(ctx, appname);
                return 1;
        }
@@ -268,7 +292,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       while((c = getopt(argc, argv, "sS")) != -1) {
+       while((c = getopt(argc, argv, "mfsS")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");