-
Notifications
You must be signed in to change notification settings - Fork 33
#41 Replace console commandline #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6179d48
2a387ab
98d8410
2bf57d7
08b4c94
9ff6096
89b44bf
d840264
2cab43f
d09e03b
90c8b30
9673a0f
e24ac6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace Crypt\Console; | ||
|
|
||
| class PinCliParameters | ||
| { | ||
| private $verbose; | ||
| private $log; | ||
|
|
||
| public function __construct($verbose = 0, $log = null) { | ||
| $this->verbose = $verbose; | ||
| $this->log = $log; | ||
| } | ||
|
|
||
| public function getVerbose() { | ||
| return $this->verbose; | ||
| } | ||
|
|
||
| public function getLog() { | ||
| return $this->log; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| <?php | ||
|
|
||
| namespace Crypt\Console; | ||
|
|
||
|
|
||
| require_once __DIR__ . '/PinCliParameters.php'; | ||
|
|
||
| class SimpleCliWrapper | ||
| { | ||
| const DEFAULT_VERBOSITY = 0; | ||
| const INVALID_INPUT = -1; | ||
| const VERBOSE_LONG = 'verbose'; | ||
| const VERBOSE_SHORT = 'v'; | ||
| const HELP_SHORT = 'h'; | ||
| const HELP_LONG = 'help'; | ||
| const LOG_SHORT = 'l'; | ||
| const LOG_LONG = 'log'; | ||
| const OPTIONAL_INDICATOR = '::'; | ||
|
|
||
| /** | ||
| * The old definition for the CLI options was: | ||
| * ```xml | ||
| * <description>Utility that emulates GnuPG 1.x passphrase handling over pipe-based IPC for GnuPG 2.x.</description> | ||
| * <version>@package-version@</version> | ||
| * <option name="log"> | ||
| * <short_name>-l</short_name> | ||
| * <long_name>--log</long_name> | ||
| * <description>Optional location to log pinentry activity.</description> | ||
| * <action>StoreString</action> | ||
| * </option> | ||
| * <option name="verbose"> | ||
| * <short_name>-v</short_name> | ||
| * <long_name>--verbose</long_name> | ||
| * <description>Sets verbosity level. Use multiples for more detail (e.g. "-vv").</description> | ||
| * <action>Counter</action> | ||
| * <default>0</default> | ||
| * </option> | ||
| * </description> | ||
| * ``` | ||
| * | ||
| * @return PinCliParameters | ||
| */ | ||
| public function parseCli() | ||
| { | ||
| $shortOpts = implode('', [ | ||
| self::VERBOSE_SHORT . self::OPTIONAL_INDICATOR, | ||
| self::LOG_SHORT . self::OPTIONAL_INDICATOR, | ||
| self::HELP_SHORT . self::OPTIONAL_INDICATOR | ||
| ]); | ||
|
|
||
| $longOpts = [ | ||
| self::VERBOSE_LONG . self::OPTIONAL_INDICATOR, | ||
| self::LOG_LONG . self::OPTIONAL_INDICATOR, | ||
| self::HELP_LONG . self::OPTIONAL_INDICATOR | ||
| ]; | ||
|
|
||
| $opts = getopt($shortOpts, $longOpts); | ||
| if (isset($opts[self::HELP_SHORT]) || isset($opts[self::HELP_LONG])) { | ||
| $this->printHelp(); | ||
| exit(1); | ||
| } | ||
|
|
||
| $verbosityLevel = self::getVerbosityLevel($opts); | ||
| if ($verbosityLevel === self::INVALID_INPUT) { | ||
| $this->writeToErrOrEcho("Invalid verbosity level. Please use -h or --help.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $logLocation = self::getLogLocation($opts); | ||
| if ($logLocation === self::INVALID_INPUT) { | ||
| $this->writeToErrOrEcho("Invalid log location. Please use -h or --help.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| return new PinCliParameters( | ||
| $verbosityLevel, | ||
| $logLocation | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * replication of previous behavior from PEAR Console_CommandLine | ||
| * which is abandoned now. | ||
| * | ||
| * ``` | ||
| * public function stderr($msg) | ||
| * { | ||
| * if (defined('STDERR')) { | ||
| * fwrite(STDERR, $msg); | ||
| * } else { | ||
| * echo $msg; | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param $msg | ||
| * @return void | ||
| */ | ||
| public function writeToErrOrEcho($msg) | ||
| { | ||
| if (defined('STDERR')) { | ||
| fwrite(STDERR, $msg); | ||
| } else { | ||
| echo $msg; | ||
| } | ||
| } | ||
|
|
||
| private function printHelp() | ||
| { | ||
| echo " | ||
| Utility that emulates GnuPG 1.x passphrase handling over pipe-based IPC for GnuPG 2.x. | ||
|
|
||
| Options: | ||
| -h, --help: Display this help message. | ||
| -l, --log: Optional location to log pinentry activity. | ||
| -v, --verbose: Verbosity level for logging. | ||
|
|
||
| The default verbosity level is 0. | ||
| Increase verbosity levels for more detail: | ||
| Short Syntax: -vvv | ||
| Long Syntax: --verbose 3 | ||
|
|
||
| Set the Log Location: | ||
| Short Syntax: -l/path/to/log/file | ||
| Long Syntax: --log /path/to/log/file | ||
|
|
||
| the short syntax will be taken before the long syntax. | ||
| "; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param array $opts | ||
| * | ||
| * @return int | ||
| */ | ||
| public static function getVerbosityLevel(array $opts) | ||
| { | ||
| if (!isset($opts[self::VERBOSE_SHORT]) && !isset($opts[self::VERBOSE_LONG])) { | ||
| return self::DEFAULT_VERBOSITY; | ||
| } | ||
|
|
||
|
|
||
| // the default options with just a -v is false, but based | ||
| // on the old system, it would be level 0 | ||
| if (isset($opts[self::VERBOSE_SHORT])) { | ||
| if ($opts[self::VERBOSE_SHORT] === false) { | ||
| return self::DEFAULT_VERBOSITY; | ||
| } | ||
|
|
||
| // the first v will be stripped -v so we count the amounts of v's after that | ||
| return (int)mb_strlen($opts[self::VERBOSE_SHORT]); | ||
| } | ||
|
|
||
| if (isset($opts[self::VERBOSE_LONG]) && !is_numeric($opts[self::VERBOSE_LONG])) { | ||
| return self::INVALID_INPUT; | ||
| } | ||
|
|
||
| if (isset($opts[self::VERBOSE_LONG])) { | ||
| return (int)$opts[self::VERBOSE_LONG]; | ||
| } | ||
|
|
||
| return self::INVALID_INPUT; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public static function getLogLocation(array $opts) | ||
| { | ||
| if (!isset($opts[self::LOG_SHORT]) && !isset($opts[self::LOG_LONG])) { | ||
| return ""; | ||
| } | ||
|
|
||
| if (isset($opts[self::LOG_SHORT]) && is_string($opts[self::LOG_SHORT])) { | ||
| return $opts[self::LOG_SHORT]; | ||
| } | ||
|
|
||
| if (isset($opts[self::LOG_LONG]) && is_string($opts[self::LOG_LONG])) { | ||
| return $opts[self::LOG_LONG]; | ||
| } | ||
|
|
||
| return self::INVALID_INPUT; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ | |
| /** | ||
| * CLI user-interface and parser. | ||
| */ | ||
| require_once 'Console/CommandLine.php'; | ||
| require_once __DIR__ . '/../Console/SimpleCliWrapper.php'; | ||
|
|
||
| /** | ||
| * A command-line dummy pinentry program for use with gpg-agent and Crypt_GPG | ||
|
|
@@ -137,7 +137,7 @@ class Crypt_GPG_PinEntry | |
| /** | ||
| * The command-line interface parser for this pinentry | ||
| * | ||
| * @var Console_CommandLine | ||
| * @var \Crypt\Console\SimpleCliWrapper | ||
| * | ||
| * @see Crypt_GPG_PinEntry::getParser() | ||
| */ | ||
|
|
@@ -192,13 +192,13 @@ class Crypt_GPG_PinEntry | |
| */ | ||
| public function __invoke() | ||
| { | ||
| $this->parser = $this->getCommandLineParser(); | ||
| $this->parser = new \Crypt\Console\SimpleCliWrapper(); | ||
|
|
||
| try { | ||
| $result = $this->parser->parse(); | ||
| $result = $this->parser->parseCli(); | ||
|
|
||
| $this->setVerbosity($result->options['verbose']); | ||
| $this->setLogFilename($result->options['log']); | ||
| $this->setVerbosity($result->getVerbose()); | ||
| $this->setLogFilename($result->getLog()); | ||
|
|
||
| $this->connect(); | ||
| $this->initPinsFromENV(); | ||
|
|
@@ -211,10 +211,6 @@ public function __invoke() | |
| } | ||
|
|
||
| $this->disconnect(); | ||
|
|
||
| } catch (Console_CommandLine_Exception $e) { | ||
| $this->log($e->getMessage() . PHP_EOL, self::VERBOSITY_ERRORS); | ||
| exit(1); | ||
| } catch (Exception $e) { | ||
| $this->log($e->getMessage() . PHP_EOL, self::VERBOSITY_ERRORS); | ||
| $this->log($e->getTraceAsString() . PHP_EOL, self::VERBOSITY_ERRORS); | ||
|
|
@@ -259,57 +255,19 @@ public function setLogFilename($filename) | |
| } | ||
|
|
||
| if ($filename != '') { | ||
| if (($this->logFile = fopen($filename, 'w')) === false) { | ||
| if (($this->logFile = fopen($filename, 'wb')) === false) { | ||
| $this->log( | ||
| 'Unable to open log file "' . $filename . '" ' | ||
| . 'for writing.' . PHP_EOL, | ||
| self::VERBOSITY_ERRORS | ||
| ); | ||
| exit(1); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just removed the else. because object calistenics / google coding standard. if you are more a dijkstra fan i can add the else again. you need to scroll a bit down and you see it was not removed just the "else clause" and the dead code. |
||
| } else { | ||
| stream_set_write_buffer($this->logFile, 0); | ||
| } | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the CLI user-interface definition for this pinentry | ||
| * | ||
| * Detects whether or not this package is PEAR-installed and appropriately | ||
| * locates the XML UI definition. | ||
| * | ||
| * @return string|null The location of the CLI user-interface definition XML. | ||
| */ | ||
| protected function getUIXML() | ||
| { | ||
| // Find PinEntry config depending on the way how the package is installed | ||
| $ds = DIRECTORY_SEPARATOR; | ||
| $root = __DIR__ . $ds . '..' . $ds . '..' . $ds; | ||
| $paths = [ | ||
| '@data-dir@' . $ds . '@package-name@' . $ds . 'data', // PEAR | ||
| $root . 'data', // Git | ||
| $root . 'data' . $ds . 'Crypt_GPG' . $ds . 'data', // Composer | ||
| ]; | ||
|
|
||
| foreach ($paths as $path) { | ||
| if (file_exists($path . $ds . 'pinentry-cli.xml')) { | ||
| return $path . $ds . 'pinentry-cli.xml'; | ||
| } | ||
| stream_set_write_buffer($this->logFile, 0); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the CLI parser for this pinentry | ||
| * | ||
| * @return Console_CommandLine the CLI parser for this pinentry. | ||
| */ | ||
| protected function getCommandLineParser() | ||
| { | ||
| return Console_CommandLine::fromXmlFile($this->getUIXML()); | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -331,7 +289,7 @@ protected function log($data, $level) | |
| fwrite($this->logFile, $data); | ||
| fflush($this->logFile); | ||
| } else { | ||
| $this->parser->outputter->stderr($data); | ||
| $this->parser->writeToErrOrEcho($data); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wb was one of the warning i have. i have a lot of inspections running and did not even react to most warnings.
i can revert this if wanted.