107 ret = cmd_set_property(args); |
107 ret = cmd_set_property(args); |
108 } else if(!strcasecmp(cmd, "get-property")) { |
108 } else if(!strcasecmp(cmd, "get-property")) { |
109 ret = cmd_get_property(args); |
109 ret = cmd_get_property(args); |
110 } else if(!strcasecmp(cmd, "info")) { |
110 } else if(!strcasecmp(cmd, "info")) { |
111 ret = cmd_info(args); |
111 ret = cmd_info(args); |
|
112 } else if(!strcasecmp(cmd, "add-repository")) { |
|
113 ret = cmd_add_repository(args); |
112 } else { |
114 } else { |
113 print_usage(argv[0]); |
115 print_usage(argv[0]); |
114 } |
116 } |
115 |
117 |
116 dav_context_destroy(ctx); |
118 dav_context_destroy(ctx); |
1116 free(buf); |
1118 free(buf); |
1117 return str; |
1119 return str; |
1118 } |
1120 } |
1119 } |
1121 } |
1120 |
1122 |
|
1123 /* ---------- config commands ---------- */ |
|
1124 |
|
1125 static int getkeyvalue(char *arg, char **key, char **value) { |
|
1126 // splits a key=value arg |
|
1127 |
|
1128 *key = NULL; |
|
1129 *value = NULL; |
|
1130 if(!arg && !key && !value) { |
|
1131 return -1; |
|
1132 } |
|
1133 |
|
1134 int haskey = 0; |
|
1135 size_t len = strlen(arg); |
|
1136 |
|
1137 if(len < 3) { |
|
1138 return -1; |
|
1139 } |
|
1140 |
|
1141 int i; |
|
1142 for(i=0;i<len;i++) { |
|
1143 if(arg[i] == '=') { |
|
1144 haskey = 1; |
|
1145 break; |
|
1146 } |
|
1147 } |
|
1148 |
|
1149 if(haskey) { |
|
1150 sstr_t k = sstrn(arg, i); |
|
1151 sstr_t v = sstrn(arg+i + 1, len - i - 1); |
|
1152 if(k.length > 0 && v.length > 0) { |
|
1153 *key = sstrdup(k).ptr; |
|
1154 *value = sstrdup(v).ptr; |
|
1155 return 0; |
|
1156 } |
|
1157 } |
|
1158 |
|
1159 return -1; |
|
1160 } |
|
1161 |
|
1162 void repository_assistent(Repository *repo, int mode) { |
|
1163 char *name = NULL; |
|
1164 sstr_t line; |
|
1165 |
|
1166 // name |
|
1167 while(!name) { |
|
1168 if(repo->name) { |
|
1169 printf("repository name [%s]: ", repo->name); |
|
1170 } else { |
|
1171 printf("repository name: "); |
|
1172 } |
|
1173 fflush(stdout); |
|
1174 |
|
1175 line = util_readline(stdin); |
|
1176 if(line.length == 0 && repo->name) { |
|
1177 break; |
|
1178 } else { |
|
1179 name = line.ptr; |
|
1180 } |
|
1181 } |
|
1182 if(name) { |
|
1183 if(repo->name) { |
|
1184 free(repo->name); |
|
1185 } |
|
1186 repo->name = name; |
|
1187 } |
|
1188 |
|
1189 // TODO |
|
1190 |
|
1191 return; |
|
1192 } |
|
1193 |
|
1194 int cmd_add_repository(CmdArgs *a) { |
|
1195 char *interactive = cmd_getoption(a, "interactive"); |
|
1196 |
|
1197 if(!interactive && a->argc < 2) { |
|
1198 fprintf(stderr, "Too few arguments\n"); |
|
1199 fprintf(stderr, "Alternatively, use the interactive mode"); |
|
1200 return -1; |
|
1201 } |
|
1202 |
|
1203 Repository *repo = repository_new(); |
|
1204 // create repo object and fill config with cmd args |
|
1205 for(int i=0;i<a->argc;i++) { |
|
1206 char *key; |
|
1207 char *value; |
|
1208 if(!getkeyvalue(a->argv[i], &key, &value)) { |
|
1209 if(!strcmp(key, "password")) { |
|
1210 // passwords from command line args are plain text |
|
1211 // but the repository needs a base64 encoded password |
|
1212 char *pw = util_base64encode(value, strlen(value)); |
|
1213 free(value); |
|
1214 value = pw; |
|
1215 } |
|
1216 |
|
1217 if(repo_add_config(repo, key, value)) { |
|
1218 fprintf( |
|
1219 stderr, |
|
1220 "Unkown repository config element: %s\n", |
|
1221 key); |
|
1222 } |
|
1223 |
|
1224 free(key); |
|
1225 free(value); |
|
1226 } else { |
|
1227 fprintf(stderr, "´%s´ not in key=value format\n", a->argv[i]); |
|
1228 // TODO: cleanup |
|
1229 return -1; |
|
1230 } |
|
1231 } |
|
1232 |
|
1233 if(interactive) { |
|
1234 // start interactive assistent to adjust config |
|
1235 repository_assistent(repo, 0); |
|
1236 } |
|
1237 |
|
1238 return 0; |
|
1239 } |