]> git.andy128k.dev Git - ipf.git/commitdiff
application level commands
authorAndrey Kutejko <aku@anahoret.com>
Wed, 20 Aug 2014 12:58:41 +0000 (15:58 +0300)
committerAndrey Kutejko <aku@anahoret.com>
Wed, 20 Aug 2014 12:58:41 +0000 (15:58 +0300)
ipf/application.php
ipf/auth/app.php
ipf/auth/commands/createsuperuser.php [new file with mode: 0644]
ipf/cli.php
ipf/command/createsuperuser.php [deleted file]
ipf/shell.php

index 037f86d17f0b58f00fad32838c550f28190523a1..d6b049db87c2a56e286f537c6961433246b5a570 100644 (file)
@@ -73,6 +73,16 @@ abstract class IPF_Application
         return strtolower($e[count($e)-1]);
     }
 
+    /**
+     * Returns CLI commands
+     *
+     * @return array List of commands
+     */
+    public function commands()
+    {
+        return array();
+    }
+
     /**
      * Returns additional context for templates
      *
index 8c6f4a2036d76f9a2f15c3214d57b49040ae88a8..96bebb6ce6c9c41b2016a1cce7428ac1c7471bca 100644 (file)
@@ -181,5 +181,12 @@ class IPF_Auth_App extends IPF_Application
 
         return $app->getTitle().' | '.$admin->verbose_name().' | '.ucfirst($parts[2]);
     }
+
+    public function commands()
+    {
+        return array(
+            new IPF_Auth_Command_CreateSuperUser,
+        );
+    }
 }
 
diff --git a/ipf/auth/commands/createsuperuser.php b/ipf/auth/commands/createsuperuser.php
new file mode 100644 (file)
index 0000000..19a76fd
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+class IPF_Auth_Command_CreateSuperUser
+{
+    public $command = 'createsuperuser';
+    public $description = 'Create superuser';
+
+    public function run($args=null)
+    {
+        print "Create superuser\n";
+
+        $username = IPF_Shell::ask('  Username: ');
+        $password = IPF_Shell::ask('  Password: ');
+        $email    = IPF_Shell::ask('  E-mail: ');
+
+        $project = IPF_Project::getInstance();
+
+        $su = new User;
+        $su->username     = $username;
+        $su->email        = $email;
+        $su->is_staff     = true;
+        $su->is_active    = true;
+        $su->is_superuser = true;
+        $su->setPassword($password);
+        $su->save();
+        print "Done\n";
+    }
+}
+
index 3e9a0ce2c70a0a9d7ac0ee2a18e52e568a01ed34..eb704a58b3e0fa07c676abce1834acbe0135d075 100644 (file)
@@ -1,5 +1,13 @@
 <?php
 
+/*
+    Command is a class with following informal interface.
+
+    public $command = 'command name';
+    public $description = 'command description';
+    public function run($args=null) { ... body
+*/
+
 class IPF_Cli
 {
     protected $commands;
@@ -7,46 +15,54 @@ class IPF_Cli
     public function __construct()
     {
         $this->commands = array(
-            new IPF_Command_Shell,
-            new IPF_Command_DebugServer,
-            new IPF_Command_Routes,
-            new IPF_Command_BuildModels,
-            new IPF_Command_BuildContribModels,
-            new IPF_Command_Sql,
-            new IPF_Command_SyncDB,
-            new IPF_Command_Fixtures,
-            new IPF_Command_DB,
-            new IPF_Command_DBDump,
-            new IPF_Command_DBRestore,
-            new IPF_Command_CollectStatic,
-            new IPF_Command_Pack,
-            new IPF_Command_Unpack,
-            new IPF_Command_CreateSuperUser,
-            new IPF_Command_CreateMigration,
-            new IPF_Command_Migrate,
-            new IPF_Command_SyncPerms,
+            'IPF' => array(
+                new IPF_Command_Shell,
+                new IPF_Command_DebugServer,
+                new IPF_Command_Routes,
+                new IPF_Command_BuildModels,
+                new IPF_Command_BuildContribModels,
+                new IPF_Command_Sql,
+                new IPF_Command_SyncDB,
+                new IPF_Command_Fixtures,
+                new IPF_Command_DB,
+                new IPF_Command_DBDump,
+                new IPF_Command_DBRestore,
+                new IPF_Command_CollectStatic,
+                new IPF_Command_Pack,
+                new IPF_Command_Unpack,
+                new IPF_Command_CreateMigration,
+                new IPF_Command_Migrate,
+                new IPF_Command_SyncPerms,
+            ),
         );
-        
+
+        foreach (IPF_Project::getInstance()->appList() as $app) {
+            $commands = $app->commands();
+            if (count($commands))
+                $this->commands[$app->getName()] = $commands;
+        }
+
+        $project = array();
         foreach (IPF::get('commands', array()) as $cmd) {
             if (is_string($cmd))
                 $cmd = new $cmd;
-            $this->commands[] = $cmd;
+            $project[] = $cmd;
         }
+        if (count($project))
+            $this->commands['Project'] = $project;
     }
 
     protected function usage()
     {
-        print "Usage: php index.php <subcommand> [options] [args]\n\n";
-        print "Available subcommands:\n";
+        print "Usage: php index.php <subcommand> [options] [args]\n";
 
-        $rows = array();
-        foreach ($this->commands as $command)
-            $rows[] = array(
-                '    ' . $command->command,
-                $command->description,
-            );
+        foreach ($this->commands as $group => $commands) {
+            print "\n\033[1;36m$group:\033[0m\n";
 
-        IPF_Shell::displayTwoColumns($rows);
+            foreach ($commands as $command) {
+                echo '    '.str_pad($command->command, 20) . "\t" . $command->description . "\n";
+            }
+        }
 
         print "\n";
     }
@@ -75,10 +91,12 @@ class IPF_Cli
             return;
         }
 
-        foreach ($this->commands as $command) {
-            if ($command->command === $args[1]) {
-                $command->run(array_slice($args, 2));
-                return;
+        foreach ($this->commands as $group => $commands) {
+            foreach ($commands as $command) {
+                if ($command->command === $args[1]) {
+                    $command->run(array_slice($args, 2));
+                    return;
+                }
             }
         }
 
diff --git a/ipf/command/createsuperuser.php b/ipf/command/createsuperuser.php
deleted file mode 100644 (file)
index 780f6dd..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-class IPF_Command_CreateSuperUser
-{
-    public $command = 'createsuperuser';
-    public $description = 'Create superuser';
-
-    public function run($args=null)
-    {
-        print "Create superuser\n";
-
-        $username = $this->readString("  Username: ");
-        $password = $this->readString("  Password: ");
-        $email    = $this->readString("  E-mail: ");
-
-        $project = IPF_Project::getInstance();
-
-        $su = new User;
-        $su->username     = $username;
-        $su->email        = $email;
-        $su->is_staff     = true;
-        $su->is_active    = true;
-        $su->is_superuser = true;
-        $su->setPassword($password);
-        $su->save();
-        print "Done\n";
-    }
-
-    private function readString($prompt)
-    {
-        $value = '';
-        while (!$value) {
-            print $prompt;
-            $value = trim(fgets(STDIN));
-        }
-        return $value;
-    }
-}
-
index c543e821d87d61732ccaa24ab35a835491bac669..a5f686c97fb2b5422511b0bff7c58f1e4ee65e56 100644 (file)
@@ -42,5 +42,15 @@ class IPF_Shell
             echo str_pad($row[0], $firstColumnSize) . "\t" . $row[1] . "\n";
         }
     }
+
+    public static function ask($prompt)
+    {
+        $value = '';
+        while (!$value) {
+            print $prompt;
+            $value = trim(fgets(STDIN));
+        }
+        return $value;
+    }
 }