Automatically detect if we should enable colors or not
[php-sqllint.git] / src / phpsqllint / Cli.php
index 5e4f6388435a2d0e3610e30441ef052c97bbd706..2f2473fa7cf7867b2a9a2ffeb447d6c7e1407725 100644 (file)
@@ -28,6 +28,9 @@ class Cli
 {
     protected $renderer;
 
+    protected $format = false;
+    protected $colors = false;
+
     /**
      * Start processing.
      *
@@ -41,7 +44,11 @@ class Cli
 
             $allfine = true;
             foreach ($files as $filename) {
-                $allfine &= $this->checkFile($filename);
+                if ($this->format) {
+                    $allfine &= $this->formatFile($filename);
+                } else {
+                    $allfine &= $this->checkFile($filename);
+                }
             }
 
             if ($allfine == true) {
@@ -65,14 +72,8 @@ class Cli
     public function checkFile($filename)
     {
         $this->renderer->startRendering($filename);
-
-        if ($filename == '-') {
-            $sql = file_get_contents('php://stdin');
-        } else {
-            $sql = file_get_contents($filename);
-        }
-        if (trim($sql) == '') {
-            $this->renderer->displayError('SQL file empty', '', 0, 0);
+        $sql = $this->loadSql($filename);
+        if ($sql === false) {
             return false;
         }
 
@@ -110,6 +111,38 @@ class Cli
         return false;
     }
 
+    /**
+     * Reformat the given file
+     */
+    protected function formatFile($filename)
+    {
+        $this->renderer->startRendering($filename);
+        $sql = $this->loadSql($filename);
+        if ($sql === false) {
+            return false;
+        }
+
+        $options = array(
+            //FIXME: automatically detect if the shell/tool supports colors
+            'type' => $this->colors ? 'cli' : 'text'
+        );
+        echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
+    }
+
+    protected function loadSql($filename)
+    {
+        if ($filename == '-') {
+            $sql = file_get_contents('php://stdin');
+        } else {
+            $sql = file_get_contents($filename);
+        }
+        if (trim($sql) == '') {
+            $this->renderer->displayError('SQL file empty', '', 0, 0);
+            return false;
+        }
+        return $sql;
+    }
+
     /**
      * Load parameters for the CLI option parser.
      *
@@ -122,6 +155,32 @@ class Cli
         $parser->version = '0.0.2';
         $parser->avoid_reading_stdin = true;
 
+        $parser->addOption(
+            'format',
+            array(
+                'short_name'  => '-f',
+                'long_name'   => '--format',
+                'description' => 'Reformat SQL instead of checking',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
+        $parser->addOption(
+            'colors',
+            array(
+                'long_name'   => '--color',
+                'description' => 'Use colors in formatting output',
+                'action'      => 'StoreTrue',
+            )
+        );
+        $parser->addOption(
+            'nocolors',
+            array(
+                'long_name'   => '--nocolor',
+                'description' => 'Disable colors in formatting output',
+                'action'      => 'StoreTrue',
+            )
+        );
         $parser->addOption(
             'renderer',
             array(
@@ -165,6 +224,23 @@ class Cli
                 . ucfirst($result->options['renderer']);
             $this->renderer = new $rendClass();
 
+            $this->format = $result->options['format'];
+
+            if ($result->options['colors'] !== null) {
+                $this->colors = $result->options['colors'];
+            } else if ($result->options['nocolors'] !== null) {
+                $this->colors = !$result->options['nocolors'];
+            } else {
+                //default coloring to enabled, except
+                // when piping | to another tool
+                $this->colors = true;
+                if (function_exists('posix_isatty')
+                    && !posix_isatty(STDOUT)
+                ) {
+                    $this->colors = false;
+                }
+            }
+
             foreach ($result->args['sql_files'] as $filename) {
                 if ($filename == '-') {
                     continue;