Skip to content
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

feat: add config mode to datadog-setup.php #1951

Merged
merged 47 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fc40344
[PROF-6905] extract `find_ini_files()` function
realFlowControl Mar 1, 2023
57a6966
[PROF-6905] add `config_list` implementation for `config list` command
realFlowControl Mar 10, 2023
1884373
[PROF-6905] add `config_get()` implementation for `config get` command
realFlowControl Mar 10, 2023
0829d9d
[PROF-6905] add `config_set` implementation for `config set` command
realFlowControl Mar 14, 2023
07f0b24
[PROF-6905] use `INI_SCANNER_RAW` mode for `parse_ini_file` as we nee…
realFlowControl Mar 14, 2023
9b96891
[PROF-6905] clenaup
realFlowControl Mar 14, 2023
cb84057
[PROF-6905] fix typo in variable name
realFlowControl Mar 14, 2023
507fff5
[PROF-6905] finish comment for `find_ini_files()` function
realFlowControl Mar 14, 2023
bcd306e
[PROF-6905] clenaup
realFlowControl Mar 14, 2023
c193f84
[PROF-6905] this is horrible, but it works, will optimize later this …
realFlowControl Mar 14, 2023
48a1a42
[PROF-6905] clenaup
realFlowControl Mar 14, 2023
df4da0f
[PROF-6905] clenaup
realFlowControl Mar 14, 2023
14c7c2c
[PROF-6905] clenaup
realFlowControl Mar 14, 2023
bcf02bb
[PROF-6905] fix output format to be INI parseable string
realFlowControl Mar 14, 2023
004fb7f
[PROF-6905] normalize `-d` argument
realFlowControl Mar 14, 2023
29d89bc
style: fix lint
morrisonlevi Mar 15, 2023
bc66682
[PROF-6905] optimize cli parsing
realFlowControl Mar 20, 2023
832100e
[PROF-6905] fix style
realFlowControl Mar 20, 2023
7efa099
help me CI, you are my last hope!
realFlowControl Mar 20, 2023
0e2d75f
[PROF-6905] add special case handling `--option=value:`
realFlowControl Mar 20, 2023
a6c924c
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Mar 20, 2023
9e12902
WIP refactoring
morrisonlevi Mar 21, 2023
72faab3
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Mar 22, 2023
e6b262b
add support for commented out ini settings in `config set` command
realFlowControl Mar 23, 2023
b40e41f
WIP: moar stuff, but still stuff todo
realFlowControl Mar 23, 2023
3b69c6a
fix style
realFlowControl Mar 24, 2023
d5f39de
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Mar 24, 2023
38204bb
add tests
realFlowControl Mar 24, 2023
d98dd21
fix lint
realFlowControl Mar 24, 2023
bd64267
add documentation
realFlowControl Mar 24, 2023
a20a301
remove error handling as our own parser fixes this
realFlowControl Mar 24, 2023
fa0ced4
fix lint
realFlowControl Mar 24, 2023
a127761
fix lint
realFlowControl Mar 24, 2023
c56cd69
WIP
realFlowControl Mar 26, 2023
ddbd1ee
error out on parsing errors in CLI
realFlowControl Mar 28, 2023
3eb5d77
remove whitespace to please linter
realFlowControl Mar 29, 2023
91fa4f0
handle missing --php-bin value
realFlowControl Mar 29, 2023
3733099
WIP
realFlowControl Mar 29, 2023
8a915de
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Apr 6, 2023
1ee5af9
cleanup
realFlowControl Apr 6, 2023
bde1176
optimised `find_all_ini_files()`
realFlowControl Apr 6, 2023
95e942b
Handle more edge cases
morrisonlevi Apr 14, 2023
caad1ce
fix typo
realFlowControl Apr 19, 2023
b1e791a
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Apr 19, 2023
ea51e18
extract line print for `IniRecord` into method and "fix" PHP binary
realFlowControl Apr 19, 2023
045371b
fix lint
realFlowControl Apr 19, 2023
02577f3
Merge branch 'master' into florian/PROF-6905-add-config-mode
realFlowControl Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions datadog-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function cmd_config_set(array $options): void
// If we are here, we could not find the INI setting in any files, so
// we try and look for commented versions

$iniFilePaths = find_ini_files($phpProps) + get_ini_files($phpProps);
$iniFilePaths = array_merge(find_ini_files($phpProps), get_ini_files($phpProps));

$matchCount = [];
// look for INI setting in INI files
Expand Down Expand Up @@ -1040,7 +1040,10 @@ function get_architecture()
);
}

function parse_cli_arguments(array $argv = null): array
/**
* @return array|false
*/
function parse_cli_arguments(array $argv = null)
{
if (is_null($argv)) {
$argv = $_SERVER['argv'];
Expand All @@ -1059,16 +1062,17 @@ function parse_cli_arguments(array $argv = null): array
// parse long option
$key = substr($token, 2);
$value = false;
// look ahead to next $token
if (isset($argv[0]) && substr($argv[0], 0, 1) !== '-') {
$value = array_shift($argv);
if ($value === null) {
$value = false;
}
}
// --php-bin=php
if ($value === false && strpos($key, '=') !== false) {
if (strpos($key, '=') !== false) {
list($key, $value) = explode('=', $key, 2);
} else {
// look ahead to next $token
if (isset($argv[0]) && substr($argv[0], 0, 1) !== '-') {
$value = array_shift($argv);
if ($value === null) {
$value = false;
}
}
realFlowControl marked this conversation as resolved.
Show resolved Hide resolved
}
} elseif (substr($token, 0, 1) === '-') {
// parse short option
Expand All @@ -1088,6 +1092,12 @@ function parse_cli_arguments(array $argv = null): array
$value = substr($token, 2);
}
} else {
if (count($arguments['opts'])) {
// php datadog-setup.php --php-bin=all php6
// The "php6" is a problem
echo "Parse error at token '$token'", PHP_EOL;
return false;
}
// parse command
if ($arguments['cmd'] === null) {
$arguments['cmd'] = $token;
Expand Down Expand Up @@ -1122,29 +1132,15 @@ function parse_cli_arguments(array $argv = null): array
*/
function parse_validate_user_options()
{
/*
$shortOptions = "h";
$longOptions = [
OPT_HELP,
OPT_PHP_BIN . ':',
OPT_FILE . ':',
OPT_INSTALL_DIR . ':',
OPT_UNINSTALL,
OPT_ENABLE_APPSEC,
OPT_ENABLE_PROFILING,
];
$options = getopt($shortOptions, $longOptions);
*/

$args = parse_cli_arguments();
$options = $args['opts'];

// Help and exit
if (key_exists('h', $options) || key_exists(OPT_HELP, $options)) {
if ($args === false || key_exists('h', $args['opts']) || key_exists(OPT_HELP, $args['opts'])) {
print_help();
exit(0);
}


$options = $args['opts'];
$normalizedOptions = [];

$normalizedOptions[OPT_UNINSTALL] = isset($options[OPT_UNINSTALL]);
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/PHPInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public function cliArguments()
{
return [
[
'datadog-setup.php config set -d datadog.profiling.enabled=1 -ddatadog.profiling.log_level=trace',
[
'cmd' => 'config set',
'opts' => [
'd' => [
'datadog.profiling.enabled=1',
'datadog.profiling.log_level=trace'
]
]
]
], [
'datadog-setup.php config get -d datadog.profiling.enabled -dfoobar',
[
'cmd' => 'config get',
Expand Down Expand Up @@ -217,6 +228,16 @@ public function testCliArgumentParsing(string $cli, array $expect)
);
}

public function testFailingCliArgumentParsing()
{
$this->expectOutputString("Parse error at token 'php6'" . PHP_EOL);
$command = explode(' ', 'datadog-setup.php config get --php-bin=all php6 -ddatadog.profiling.enabled');
$opts = parse_cli_arguments($command);
$this->assertFalse(
$opts
);
}

public function iniFileContents()
{
return [
Expand Down