]> _ Git - cubist_util.git/commitdiff
wip #4666
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 3 Sep 2021 16:57:58 +0000 (18:57 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 3 Sep 2021 16:57:58 +0000 (18:57 +0200)
src/CommandLine.php

index 56fc23c372a657d342431b331b393010f0cac2e0..7c3b245ed8a936741a7d15cc4c5424287bb8d4b2 100644 (file)
@@ -7,251 +7,264 @@ use Cubist\Net\SSH2;
 class CommandLine
 {
 
-       protected $program;
-       protected $args = array();
-       protected $env = array();
-       protected $cd = null;
-       protected $commande = null;
-       protected $output;
-       protected $temp_output = true;
-       protected $error;
-       protected $nohup = false;
-       protected $execTime;
-       protected $lang;
-       protected $ssh = null;
-       protected $longArgSeparator = '=';
-
-       function __construct($program, $output = null, $error = true)
-       {
-               if (stristr($program, ' ')) {
-                       $program = '"' . $program . '"';
-               }
-               $this->program = $program;
-               $this->commande = null;
-               if (null === $output) {
-                       $this->output = tempnam(sys_get_temp_dir(), 'Cubist');
-               } else {
-                       $this->temp_output = false;
-                       $this->output = $output;
-               }
-               $this->error = $error;
-       }
-
-       public function setArg($name = null, $val = null)
-       {
-               if (null === $name && null === $val) {
-                       return;
-               }
-               if (stristr($val, ' ') && !stristr($val, '>') && !stristr($val, '<')) {
-                       $val = '"' . $val . '"';
-               }
-               $this->args[] = array($name, $val);
-       }
-
-       public function __get($varname)
-       {
-               if ($varname == 'output') {
-                       return file_get_contents($this->output);
-               } elseif ($varname == 'outputfile') {
-                       return $this->output;
-               } elseif ($varname == 'commande' || $varname == 'command') {
-                       if (null === $this->commande) {
-                               $this->makeCommande($this->output, $this->error);
-                       }
-                       return $this->commande;
-               } elseif ($varname == 'execTime') {
-                       return $this->execTime;
-               }
-               return null;
-       }
-
-       public function cd($path)
-       {
-               $this->cd = $path;
-       }
-
-       public function setLang($lang)
-       {
-               $this->setEnv('LANG', $lang);
-       }
-
-       public function setEnv($name, $val)
-       {
-               if (PATH_SEPARATOR == ':') {
-                       $this->env[$name] = $val;
-               }
-       }
-
-       public function setPath($val)
-       {
-               if (PATH_SEPARATOR == ':') {
-                       $this->setEnv('PATH', $val);
-               } else {
-                       $this->program = $val . '' . $this->program;
-               }
-       }
-
-       public function setNohup($val)
-       {
-               $this->nohup = $val;
-       }
-
-       public function setManualArg($val)
-       {
-               $this->args[] = array(null, $val);
-       }
-
-       protected function _preExecute()
-       {
-
-       }
-
-       public function execute($fonction = 'shell_exec')
-       {
-               $startTime = microtime(true);
-               $this->_preExecute();
-               if ($fonction instanceof SSH2) {
-                       $this->makeCommande();
-                       $o = $fonction->exec($this->commande);
-                       file_put_contents($this->output, $o['output'] . "\n\n---\n\n" . $o['error']);
-               } else if (null === $this->ssh) {
-                       $this->makeCommande($this->output, $this->error);
-                       $fonction($this->commande);
-               } else {
-                       $this->makeCommande();
-
-                       $key = '';
-                       if (null !== $this->ssh['key']) {
-                               $key = ' -i ' . $this->ssh['key'];
-                       }
-
-                       $c = 'echo "' . $this->commande . '" | ssh ' . $key . ' -p ' . $this->ssh['port'] . ' ' . $this->ssh['username'] . '@' . $this->ssh['host'] . ' \'bash -s\'';
-                       $c .= ' > ' . $this->output;
-                       if ($this->error) {
-                               $c .= ' 2>&1 ';
-                       }
-
-                       $fonction($c);
-               }
-               $endTime = microtime(true);
-               $this->execTime = $endTime - $startTime;
-       }
-
-       public function __set($varname, $value)
-       {
-               $this->setArg($varname, $value);
-       }
-
-       public function __destruct()
-       {
+    protected $program;
+    protected $args = array();
+    protected $env = array();
+    protected $cd = null;
+    protected $commande = null;
+    protected $output;
+    protected $temp_output = true;
+    protected $error;
+    protected $nohup = false;
+    protected $execTime;
+    protected $lang;
+    protected $ssh = null;
+    protected $longArgSeparator = '=';
+
+    function __construct($program, $output = null, $error = true)
+    {
+        if (stristr($program, ' ')) {
+            $program = '"' . $program . '"';
+        }
+        $this->program = $program;
+        $this->commande = null;
+        if (null === $output) {
+            $this->output = tempnam(sys_get_temp_dir(), 'Cubist');
+        } else {
+            $this->temp_output = false;
+            $this->output = $output;
+        }
+        $this->error = $error;
+    }
+
+    public function setArg($name = null, $val = null)
+    {
+        if (null === $name && null === $val) {
+            return;
+        }
+        if (stristr($val, ' ') && !stristr($val, '>') && !stristr($val, '<')) {
+            $val = '"' . $val . '"';
+        }
+        $this->args[] = array($name, $val);
+    }
+
+    public function __get($varname)
+    {
+        if ($varname == 'output') {
+            return file_get_contents($this->output);
+        } elseif ($varname == 'outputfile') {
+            return $this->output;
+        } elseif ($varname == 'commande' || $varname == 'command') {
+            if (null === $this->commande) {
+                $this->makeCommande($this->output, $this->error);
+            }
+            return $this->commande;
+        } elseif ($varname == 'execTime') {
+            return $this->execTime;
+        }
+        return null;
+    }
+
+    public function cd($path)
+    {
+        $this->cd = $path;
+    }
+
+    public function setLang($lang)
+    {
+        $this->setEnv('LANG', $lang);
+    }
+
+    public function setEnv($name, $val)
+    {
+        if (PATH_SEPARATOR == ':') {
+            $this->env[$name] = $val;
+        }
+    }
+
+    public function setPath($val)
+    {
+        if (PATH_SEPARATOR == ':') {
+            $this->setEnv('PATH', $val);
+        } else {
+            $this->program = $val . '' . $this->program;
+        }
+    }
+
+    public function setNohup($val)
+    {
+        $this->nohup = $val;
+    }
+
+    public function setManualArg($val)
+    {
+        $this->args[] = array(null, $val);
+    }
+
+    protected function _preExecute()
+    {
+
+    }
+
+    public function execute($fonction = 'shell_exec')
+    {
+        $startTime = microtime(true);
+        $this->_preExecute();
+        if ($fonction instanceof SSH2) {
+            $this->makeCommande();
+            $o = $fonction->exec($this->commande);
+            file_put_contents($this->output, $o['output'] . "\n\n---\n\n" . $o['error']);
+        } else if (null === $this->ssh) {
+            $this->makeCommande($this->output, $this->error);
+            $fonction($this->commande);
+        } else {
+            $this->makeCommande();
+
+            $key = '';
+            if (null !== $this->ssh['key']) {
+                $key = ' -i ' . $this->ssh['key'];
+            }
+
+            $c = 'echo "' . $this->commande . '" | ssh ' . $key . ' -p ' . $this->ssh['port'] . ' ' . $this->ssh['username'] . '@' . $this->ssh['host'] . ' \'bash -s\'';
+            $c .= ' > ' . $this->output;
+            if ($this->error) {
+                $c .= ' 2>&1 ';
+            }
+
+            $fonction($c);
+        }
+        $endTime = microtime(true);
+        $this->execTime = $endTime - $startTime;
+    }
+
+    public function __set($varname, $value)
+    {
+        $this->setArg($varname, $value);
+    }
+
+    public function __destruct()
+    {
 //             if ($this->temp_output && file_exists($this->output)) {
 //                     unlink($this->output);
 //             }
-               return;
-       }
-
-       public function setLongArgumentSeparator($separator = "=")
-       {
-               $this->longArgSeparator = $separator;
-               return $this;
-       }
-
-       public function makeCommande($output = false, $error = false)
-       {
-               $commande = $this->program;
-               $commandes = array();
-               foreach ($this->args as $arg) {
-                       if (strlen($arg[0]) == 1) {
-                               $commande .= ' -' . $arg[0];
-                               if (null !== $arg[1]) {
-                                       $commande .= ' ' . $arg[1];
-                               }
-                       } elseif (null === $arg[0]) {
-                               $commande .= ' ' . $arg[1];
-                       } else {
-                               if (substr($arg[0], 0, 1) == '-') {
-                                       $commande .= ' ' . $arg[0];
-                                       if (null !== $arg[1]) {
-                                               $commande .= ' ' . $arg[1];
-                                       }
-                               } else {
-                                       $commande .= ' --' . $arg[0];
-                                       if (null !== $arg[1]) {
-                                               $commande .= $this->longArgSeparator . $arg[1];
-                                       }
-                               }
-                       }
-               }
-
-               if (null !== $this->cd) {
-                       $commandes[] = 'cd ' . $this->cd;
-               }
-               if (count($this->env) > 0) {
-                       foreach ($this->env as $var => $val) {
-                               if ($var == 'PATH') {
-                                       $commandes[] = 'export ' . $var . '=$' . $var . ':' . $val;
-                               } else {
-                                       $commandes[] = 'export ' . $var . '=' . $val;
-                               }
-                       }
-               }
-
-               if ($this->nohup) {
-                       $commande = 'nohup ' . $commande;
-               }
-
-               $commandes[] = $commande;
-
-               $this->commande = implode(';', $commandes);
-
-               if ($output) {
-                       $this->commande .= ' > ' . $output;
-               }
-               if ($error) {
-                       $this->commande .= ' 2>&1 ';
-               }
-               if ($this->nohup) {
-                       $this->commande .= ' & echo $!';
-               }
-       }
-
-       public static function getArgs($argv)
-       {
-               array_shift($argv);
-
-               $i = 0;
-               $res = array();
-               $skip = false;
-               foreach ($argv as $j => $v) {
-                       if ($skip) {
-                               $skip = false;
-                               continue;
-                       }
-                       if (substr($v, 0, 2) == '--') {
-                               list($k, $value) = explode('=', $v);
-                               $res[trim($k, '-')] = trim($value, ' "\'');
-                       } elseif (substr($v, 0, 1) == '-') {
-                               $res[trim($v, '-')] = trim($argv[$j + 1], ' \'"');
-                               $skip = true;
-                       } else {
-                               $res[$i] = trim($v, "'\" ");
-                       }
-                       $i++;
-               }
-               return $res;
-       }
-
-       public function setSSH($host, $username, $password, $port = 22, $key = null)
-       {
-               $this->ssh = array('host' => $host, 'username' => $username, 'password' => $password, 'port' => $port, 'key' => $key);
-       }
-
-       public function debug()
-       {
-
-               $e = explode('/', $this->program);
-               $p = array_pop($e);
-               file_put_contents('/tmp/' . $p . '.' . microtime(true) . '.txt', $this->commande . "\n\n" . file_get_contents($this->output));
-       }
+        return;
+    }
+
+    public function setLongArgumentSeparator($separator = "=")
+    {
+        $this->longArgSeparator = $separator;
+        return $this;
+    }
+
+    public function makeCommande($output = false, $error = false)
+    {
+        $commande = $this->program;
+        $commandes = array();
+        foreach ($this->args as $arg) {
+            if (strlen($arg[0]) == 1) {
+                $commande .= ' -' . $arg[0];
+                if (null !== $arg[1]) {
+                    $commande .= ' ' . $arg[1];
+                }
+            } elseif (null === $arg[0]) {
+                $commande .= ' ' . $arg[1];
+            } else {
+                if (substr($arg[0], 0, 1) == '-') {
+                    $commande .= ' ' . $arg[0];
+                    if (null !== $arg[1]) {
+                        $commande .= ' ' . $arg[1];
+                    }
+                } else {
+                    $commande .= ' --' . $arg[0];
+                    if (null !== $arg[1]) {
+                        $commande .= $this->longArgSeparator . $arg[1];
+                    }
+                }
+            }
+        }
+
+        if (null !== $this->cd) {
+            $commandes[] = 'cd ' . $this->cd;
+        }
+        if (count($this->env) > 0) {
+            foreach ($this->env as $var => $val) {
+                if ($var == 'PATH') {
+                    $commandes[] = 'export ' . $var . '=$' . $var . ':' . $val;
+                } else {
+                    $commandes[] = 'export ' . $var . '=' . $val;
+                }
+            }
+        }
+
+        if ($this->nohup) {
+            $commande = 'nohup ' . $commande;
+        }
+
+        $commandes[] = $commande;
+
+        $this->commande = implode(';', $commandes);
+
+        if ($output) {
+            $this->commande .= ' > ' . $output;
+        }
+        if ($error) {
+            $this->commande .= ' 2>&1 ';
+        }
+        if ($this->nohup) {
+            $this->commande .= ' & echo $!';
+        }
+    }
+
+    public static function getArgs($argv)
+    {
+        array_shift($argv);
+
+        $i = 0;
+        $res = array();
+        $skip = false;
+        foreach ($argv as $j => $v) {
+            if ($skip) {
+                $skip = false;
+                continue;
+            }
+            if (substr($v, 0, 2) == '--') {
+                list($k, $value) = explode('=', $v);
+                $res[trim($k, '-')] = trim($value, ' "\'');
+            } elseif (substr($v, 0, 1) == '-') {
+                $res[trim($v, '-')] = trim($argv[$j + 1], ' \'"');
+                $skip = true;
+            } else {
+                $res[$i] = trim($v, "'\" ");
+            }
+            $i++;
+        }
+        return $res;
+    }
+
+    public function setSSH($host, $username, $password, $port = 22, $key = null)
+    {
+        $this->ssh = array('host' => $host, 'username' => $username, 'password' => $password, 'port' => $port, 'key' => $key);
+    }
+
+    public function debug()
+    {
+        $e = explode('/', $this->program);
+        $p = array_pop($e);
+        file_put_contents('/tmp/' . $p . '.' . microtime(true) . '.txt', $this->commande . "\n\n" . file_get_contents($this->output));
+    }
+
+
+    /**
+     * @return string
+     */
+    public function getOutput()
+    {
+        return file_get_contents($this->output);
+    }
+
+    public function getCommand()
+    {
+        return $this->commande;
+    }
 
 }