Skip to content

Commit 39ee545

Browse files
authored
Merge pull request #13 from EasyEngine/update/config
Add config unset and list commands with format support
2 parents 691b771 + bdc13cb commit 39ee545

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"commands": [
2222
"config",
2323
"config set",
24-
"config get"
24+
"config get",
25+
"config unset",
26+
"config list"
2527
]
2628
}
2729
}

src/Config_Command.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,88 @@ public function set( $args, $assoc_args ) {
7272

7373
$this->fs->dumpFile( $config_file_path, Spyc::YAMLDump( $config, false, false, true ) );
7474
}
75+
76+
/**
77+
* Unset a config value
78+
*
79+
* ## OPTIONS
80+
*
81+
* <key>
82+
* : Key of config to unset
83+
*
84+
* ## EXAMPLES
85+
*
86+
* # Remove value from config
87+
* $ ee config unset cloudflare-api-key
88+
*
89+
*/
90+
public function unset( $args, $assoc_args ) {
91+
$config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_ROOT_DIR . '/config/config.yml';
92+
$config = Spyc::YAMLLoad( $config_file_path );
93+
$key = $args[0];
94+
95+
if ( ! isset( $config[ $key ] ) ) {
96+
EE::error( "No config value with key '$key' set" );
97+
}
98+
99+
unset( $config[ $key ] );
100+
101+
$this->fs->dumpFile( $config_file_path, Spyc::YAMLDump( $config, false, false, true ) );
102+
}
103+
104+
/**
105+
* Lists the config values.
106+
*
107+
* [--format=<format>]
108+
* : Render output in a particular format.
109+
* ---
110+
* default: table
111+
* options:
112+
* - table
113+
* - csv
114+
* - yaml
115+
* - json
116+
* - count
117+
* - text
118+
* ---
119+
*
120+
* ## EXAMPLES
121+
*
122+
* # List all config values
123+
* $ ee config list
124+
*
125+
* # List all config values in JSON
126+
* $ ee config list --format=json
127+
*
128+
* @subcommand list
129+
*/
130+
public function _list( $args, $assoc_args ) {
131+
$config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_ROOT_DIR . '/config/config.yml';
132+
$config = Spyc::YAMLLoad( $config_file_path );
133+
$format = \EE\Utils\get_flag_value( $assoc_args, 'format' );
134+
135+
if ( empty( $config ) ) {
136+
\EE::error( 'No config values found!' );
137+
}
138+
139+
if ( 'text' === $format ) {
140+
foreach ( $config as $key => $value ) {
141+
\EE::log( $key . ': ' . $value );
142+
}
143+
} else {
144+
$result = array_map(
145+
function ( $key, $value ) {
146+
return [
147+
'key' => $key,
148+
'value' => $value,
149+
];
150+
},
151+
array_keys( $config ),
152+
$config
153+
);
154+
155+
$formatter = new \EE\Formatter( $assoc_args, [ 'key', 'value' ] );
156+
$formatter->display_items( $result );
157+
}
158+
}
75159
}

0 commit comments

Comments
 (0)