diff --git a/composer.json b/composer.json index 9a10379..cff63c4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,9 @@ "commands": [ "config", "config set", - "config get" + "config get", + "config unset", + "config list" ] } } diff --git a/src/Config_Command.php b/src/Config_Command.php index 7a7ab56..ab43c80 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -72,4 +72,88 @@ public function set( $args, $assoc_args ) { $this->fs->dumpFile( $config_file_path, Spyc::YAMLDump( $config, false, false, true ) ); } + + /** + * Unset a config value + * + * ## OPTIONS + * + * + * : Key of config to unset + * + * ## EXAMPLES + * + * # Remove value from config + * $ ee config unset cloudflare-api-key + * + */ + public function unset( $args, $assoc_args ) { + $config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_ROOT_DIR . '/config/config.yml'; + $config = Spyc::YAMLLoad( $config_file_path ); + $key = $args[0]; + + if ( ! isset( $config[ $key ] ) ) { + EE::error( "No config value with key '$key' set" ); + } + + unset( $config[ $key ] ); + + $this->fs->dumpFile( $config_file_path, Spyc::YAMLDump( $config, false, false, true ) ); + } + + /** + * Lists the config values. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - yaml + * - json + * - count + * - text + * --- + * + * ## EXAMPLES + * + * # List all config values + * $ ee config list + * + * # List all config values in JSON + * $ ee config list --format=json + * + * @subcommand list + */ + public function _list( $args, $assoc_args ) { + $config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_ROOT_DIR . '/config/config.yml'; + $config = Spyc::YAMLLoad( $config_file_path ); + $format = \EE\Utils\get_flag_value( $assoc_args, 'format' ); + + if ( empty( $config ) ) { + \EE::error( 'No config values found!' ); + } + + if ( 'text' === $format ) { + foreach ( $config as $key => $value ) { + \EE::log( $key . ': ' . $value ); + } + } else { + $result = array_map( + function ( $key, $value ) { + return [ + 'key' => $key, + 'value' => $value, + ]; + }, + array_keys( $config ), + $config + ); + + $formatter = new \EE\Formatter( $assoc_args, [ 'key', 'value' ] ); + $formatter->display_items( $result ); + } + } }