Billy Barrow 1 месяц назад
Родитель
Сommit
491b486a12
1 измененных файлов с 43 добавлено и 18 удалено
  1. 43 18
      src/cli/Cli.vala

+ 43 - 18
src/cli/Cli.vala

@@ -9,27 +9,52 @@ public static int main(string[] args) {
         return 255;
     }
 
-    var command = args[1];
-    if(command == "manifest") {
-        var m_args = new Invercargill.DataStructures.Vector<string>();
-        m_args.add_all(Invercargill.Wrap.array(args));
-        m_args.remove_at(1);
-        return manifest_main(m_args.to_array());
-    }
-    if(command == "info") {
-        return info(args);
-    }
-    if(command == "repository") {
-        return repository_main(args);
-    }
-    if(command == "install") {
-        return install_main(args);
+    try {
+        var arguments = new Invercargill.DataStructures.Vector<string>();
+        arguments.add_all(Invercargill.Wrap.array(args));
+        var command = arguments.skip(1).where(a => !a.has_prefix("-")).first_or_default();
+    
+        var root_flag_pos = arguments.index_of("--root");
+        if(root_flag_pos != null) {
+            var root_path = arguments.get_or_default(root_flag_pos+1);
+            if(root_path == null) {
+                printerr("Expected path after --root\n");
+                return 255;
+            }
+            var result = Posix.chroot(root_path);
+            if(result != 0) {
+                printerr(@"Unable to chroot to \"$root_path\": $(Posix.strerror(result))\n");
+                return result;
+            }
+    
+            arguments.remove_at(root_flag_pos);
+            arguments.remove_at(root_flag_pos+1);
+        }
+    
+        if(command == "manifest") {
+            var m_args = new Invercargill.DataStructures.Vector<string>();
+            m_args.add_all(Invercargill.Wrap.array(args));
+            m_args.remove("manifest");
+            return manifest_main(m_args.to_array());
+        }
+        if(command == "info") {
+            return info(args);
+        }
+        if(command == "repository") {
+            return repository_main(args);
+        }
+        if(command == "install") {
+            return install_main(args);
+        }
+        if(command == "scaffold") {
+            return scaffold_main(args);
+        }
     }
-    if(command == "scaffold") {
-        return scaffold_main(args);
+    catch(Error e) {
+        printerr(@"Unhandled error: $(e.message)\n");
+        return -1;
     }
 
-
     usage();
     return 255;
 }