Load version number from file
[php-sqllint.git] / src / phpsqllint / Cli.php
index 03ee71082751b41b9acab07fcba84afd34cbdcea..4edcf666c777db965c544c1840aa0967f28150b8 100644 (file)
@@ -28,6 +28,16 @@ class Cli
 {
     protected $renderer;
 
+    protected $format = false;
+
+    /**
+     * What syntax highlighting mode should be used
+     *
+     * none, ansi, html
+     */
+    protected $highlight = 'none';
+
+
     /**
      * Start processing.
      *
@@ -36,12 +46,16 @@ class Cli
     public function run()
     {
         try {
-            $parser = $this->loadParameters();
+            $parser = $this->loadOptionParser();
             $files  = $this->parseParameters($parser);
 
             $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,10 +79,8 @@ class Cli
     public function checkFile($filename)
     {
         $this->renderer->startRendering($filename);
-
-        $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;
         }
 
@@ -106,17 +118,86 @@ 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;
+        }
+
+        $typeMap = array(
+            'none' => 'text',
+            'ansi' => 'cli',
+            'html' => 'html',
+        );
+        $options = array(
+            'type' => $typeMap[$this->highlight],
+        );
+        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.
      *
      * @return \Console_CommandLine CLI option parser
      */
-    protected function loadParameters()
+    protected function loadOptionParser()
     {
         $parser = new \Console_CommandLine();
         $parser->description = 'php-sqllint';
-        $parser->version = '0.0.1';
+        $parser->version = 'dev';
+        $parser->avoid_reading_stdin = true;
+
+        $versionFile = __DIR__ . '/../../VERSION';
+        if (file_exists($versionFile)) {
+            $parser->version = trim(file_get_contents($versionFile));
+        }
 
+        $parser->addOption(
+            'format',
+            array(
+                'short_name'  => '-f',
+                'long_name'   => '--format',
+                'description' => 'Reformat SQL instead of checking',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
+        $parser->addOption(
+            'highlight',
+            array(
+                'short_name'  => '-h',
+                'long_name'   => '--highlight',
+                'description' => 'Highlighting mode (when using --format)',
+                'action'      => 'StoreString',
+                'choices'     => array(
+                    'none',
+                    'ansi',
+                    'html',
+                    'auto',
+                ),
+                'default' => 'auto',
+                'add_list_option' => true,
+            )
+        );
         $parser->addOption(
             'renderer',
             array(
@@ -136,7 +217,8 @@ class Cli
         $parser->addArgument(
             'sql_files',
             array(
-                'multiple' => true
+                'description' => 'SQL files, "-" for stdin',
+                'multiple'    => true
             )
         );
 
@@ -159,7 +241,29 @@ class Cli
                 . ucfirst($result->options['renderer']);
             $this->renderer = new $rendClass();
 
+            $this->format = $result->options['format'];
+
+            $this->highlight = $result->options['highlight'];
+            if ($this->highlight == 'auto') {
+                if (php_sapi_name() == 'cli') {
+                    //default coloring to enabled, except
+                    // when piping | to another tool
+                    $this->highlight = 'ansi';
+                    if (function_exists('posix_isatty')
+                        && !posix_isatty(STDOUT)
+                    ) {
+                        $this->highlight = 'none';
+                    }
+                } else {
+                    //no idea where we are, so do not highlight
+                    $this->highlight = 'none';
+                }
+            }
+
             foreach ($result->args['sql_files'] as $filename) {
+                if ($filename == '-') {
+                    continue;
+                }
                 if (!file_exists($filename)) {
                     throw new \Exception('File does not exist: ' . $filename);
                 }