consistent exit codes
authorMichael Wallner <mike@php.net>
Tue, 24 Mar 2015 19:49:27 +0000 (20:49 +0100)
committerMichael Wallner <mike@php.net>
Tue, 24 Mar 2015 19:49:27 +0000 (20:49 +0100)
bin/pharext
src/pharext/Command.php
src/pharext/Installer.php
src/pharext/Packager.php

index ec47e3a14a1c8cfa244ec1c50c919297e7060920..911b258b1f47e349d06b92667419046624737b15 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
index 61b8810adcffd5bb2c65dd116a493743d7e81538..7664a35867e568f20ea812a7750f88fba0ac0c42 100644 (file)
@@ -7,6 +7,27 @@ namespace pharext;
  */
 interface Command
 {
+       /**
+        * Argument error
+        */
+       const EARGS = 1;
+       /**
+        * Build error
+        */
+       const EBUILD = 2;
+       /**
+        * Signature error
+        */
+       const ESIGN = 3;
+       /**
+        * Extract/unpack error
+        */
+       const EEXTRACT = 4;
+       /**
+        * Install error
+        */
+       const EINSTALL = 5;
+       
        /**
         * Retrieve command line arguments
         * @return pharext\CliArgs
index b470c3a1d8b890edb8d5def5c6a290089a0aa218..83a40ad9769d4babb16dfd063c27d10f3dd6d5f0 100644 (file)
@@ -76,22 +76,27 @@ class Installer implements Command
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
-               $list = new SplObjectStorage();
-               $phar = new Phar(Phar::running(false));
-               $temp = $this->extract($phar);
-
-               foreach ($phar as $entry) {
-                       $dep_file = $entry->getBaseName();
-                       if (fnmatch("*.ext.phar*", $dep_file)) {
-                               $dep_phar = new Phar("$temp/$dep_file");
-                               $list[$dep_phar] = $this->extract($dep_phar);
+               try {
+                       $list = new SplObjectStorage();
+                       $phar = new Phar(Phar::running(false));
+                       $temp = $this->extract($phar);
+
+                       foreach ($phar as $entry) {
+                               $dep_file = $entry->getBaseName();
+                               if (fnmatch("*.ext.phar*", $dep_file)) {
+                                       $dep_phar = new Phar("$temp/$dep_file");
+                                       $list[$dep_phar] = $this->extract($dep_phar);
+                               }
                        }
-               }
-               /* the actual ext.phar at last */
-               $list[$phar] = $temp;
+                       /* the actual ext.phar at last */
+                       $list[$phar] = $temp;
 
-               /* installer hooks */
-               $hook = $this->hooks($list);
+                       /* installer hooks */
+                       $hook = $this->hooks($list);
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EEXTRACT);
+               }
 
                /* standard arg stuff */
                $errs = [];
@@ -114,7 +119,7 @@ class Installer implements Command
                        }
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(2);
+                       exit(self::EARGS);
                }
 
                foreach ($this->args->validate() as $error) {
@@ -131,23 +136,33 @@ class Installer implements Command
                        if (!$this->args["quiet"]) {
                                $this->help($prog);
                        }
-                       exit(1);
+                       exit(self::EARGS);
                }
 
-               /* post process hooks */
-               foreach ($hook as $callback) {
-                       if (is_callable($callback)) {
-                               $callback($this);
+               try {
+                       /* post process hooks */
+                       foreach ($hook as $callback) {
+                               if (is_callable($callback)) {
+                                       $callback($this);
+                               }
                        }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EARGS);
                }
 
                /* install packages */
-               foreach ($list as $phar) {
-                       $this->info("Installing %s ...\n", basename($phar->getPath()));
-                       $this->install($list[$phar]);
-                       $this->activate($list[$phar]);
-                       $this->cleanup($list[$phar]);
-                       $this->info("Successfully installed %s!\n", basename($phar->getPath()));
+               try {
+                       foreach ($list as $phar) {
+                               $this->info("Installing %s ...\n", basename($phar->getPath()));
+                               $this->install($list[$phar]);
+                               $this->activate($list[$phar]);
+                               $this->cleanup($list[$phar]);
+                               $this->info("Successfully installed %s!\n", basename($phar->getPath()));
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EINSTALL);
                }
        }
        
@@ -155,32 +170,26 @@ class Installer implements Command
         * Phpize + trinity
         */
        private function install($temp) {
-               try {
-                       // phpize
-                       $this->info("Running phpize ...\n");
-                       $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
-                       $phpize->run($this->args->verbose);
-
-                       // configure
-                       $this->info("Running configure ...\n");
-                       $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
-                       $configure->run($this->args->verbose);
-                               
-                       // make
-                       $this->info("Running make ...\n");
-                       $make = new Task\Make($temp);
-                       $make->run($this->args->verbose);
-
-                       // install
-                       $this->info("Running make install ...\n");
-                       $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
-                       $install = new Task\Make($temp, ["install"], $sudo);
-                       $install->run($this->args->verbose);
-               
-               } catch (\Exception $e) {
-                       $this->error("%s\n", $e->getMessage());
-                       exit(2);
-               }
+               // phpize
+               $this->info("Running phpize ...\n");
+               $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
+               $phpize->run($this->args->verbose);
+
+               // configure
+               $this->info("Running configure ...\n");
+               $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
+               $configure->run($this->args->verbose);
+
+               // make
+               $this->info("Running make ...\n");
+               $make = new Task\Make($temp);
+               $make->run($this->args->verbose);
+
+               // install
+               $this->info("Running make install ...\n");
+               $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
+               $install = new Task\Make($temp, ["install"], $sudo);
+               $install->run($this->args->verbose);
        }
 
        private function cleanup($temp) {
@@ -202,15 +211,10 @@ class Installer implements Command
                $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
                $type = $this->metadata("type") ?: "extension";
                
-               try {
-                       $this->info("Running INI activation ...\n");
-                       $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
-                       if (!$activate->run($this->args->verbose)) {
-                               $this->info("Extension already activated ...\n");
-                       }
-               } catch (\Exception $e) {
-                       $this->error("%s\n", $e->getMessage());
-                       exit(3);
+               $this->info("Running INI activation ...\n");
+               $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
+               if (!$activate->run($this->args->verbose)) {
+                       $this->info("Extension already activated ...\n");
                }
        }
 }
index efd72b657913327f74c9360280f506a1ba8946b2..b3be6d09524ccdbb86e60311685689f0adf0a8ee 100644 (file)
@@ -104,7 +104,7 @@ class Packager implements Command
                        }
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(2);
+                       exit(self::EARGS);
                }
 
                try {
@@ -141,7 +141,7 @@ class Packager implements Command
                        if (!$this->args["quiet"]) {
                                $this->help($prog);
                        }
-                       exit(1);
+                       exit(self::EARGS);
                }
                
                $this->createPackage();
@@ -225,7 +225,12 @@ class Packager implements Command
                                "type" => $this->args->zend ? "zend_extension" : "extension",
                        ]);
                        $file = (new Task\PharBuild($this->source, $meta))->run();
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EBUILD);
+               }
 
+               try {
                        if ($this->args->sign) {
                                $this->info("Using private key to sign phar ...\n");
                                $pass = (new Task\Askpass)->run($this->args->verbose);
@@ -235,7 +240,7 @@ class Packager implements Command
 
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(4);
+                       exit(self::ESIGN);
                }
 
                if ($this->args->gzip) {
@@ -286,7 +291,7 @@ class Packager implements Command
 
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(4);
+                       exit(self::EBUILD);
                }
        }
 }