Skip to content

Commit

Permalink
Add CacheCommand to facilitate some things
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Mar 6, 2024
1 parent 565795a commit ad5ee8d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/qit-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,8 @@ jobs:
run: bash .github/workflows/tests/environments/start-and-assert.sh

- name: Find and delete all hidden JSON files in the directory
run: |
DIR_PATH=$(php src/qit-cli.php qit:dir | tr -d '\n')
FILES_TO_DELETE=$(find "$DIR_PATH" -type f -name ".*.json")
for file in $FILES_TO_DELETE; do
echo "Removing $file"
rm -f "$file"
done
- name: Run setup again to re-create it
run: bash .github/workflows/tests/environments/setup.sh ${{ secrets.QIT_STAGING_SECRET }} https://stagingcompatibilitydashboard.wpcomstaging.com
working-directory: src
run: php qit-cli.php cache delete environment_monitor

- name: Run a env:list to trigger the Dangling Environment Cleanup
working-directory: src
Expand Down
64 changes: 64 additions & 0 deletions src/src/Commands/CacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace QIT_CLI\Commands;

use QIT_CLI\App;
use QIT_CLI\Cache;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CacheCommand extends Command {
protected static $defaultName = 'cache'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

protected function configure() {
$this
->addArgument( 'action', InputArgument::REQUIRED, 'The action to perform. (get, set, delete)' )
->addArgument( 'key', InputArgument::REQUIRED, 'The key to cache.' )
->addArgument( 'value', InputArgument::OPTIONAL, 'The value when (set).' )
->addArgument( 'expiration', InputArgument::OPTIONAL, 'The expiration when (set).' )
->setDescription( 'Manipulates the QIT Cache' );
}

protected function execute( InputInterface $input, OutputInterface $output ): int {
$action = $input->getArgument( 'action' );
$key = $input->getArgument( 'key' );
$cache = App::make( Cache::class );

switch ( $action ) {
case 'get':
$output->writeln( $cache->get( $key ) );
break;
case 'set':
// Check expiration is set.
if ( ! $input->hasArgument( 'expiration' ) ) {
$output->writeln( 'Expiration is required when setting a cache value.' );

return Command::FAILURE;
}
// Validate expiration is an integer.
if ( ! is_numeric( $input->getArgument( 'expiration' ) ) ) {
$output->writeln( 'Expiration must be an integer.' );

return Command::FAILURE;
}
// Check value is set.
if ( ! $input->hasArgument( 'value' ) ) {
$output->writeln( 'Value is required when setting a cache value.' );

return Command::FAILURE;
}
$cache->set( $key, $input->getArgument( 'value' ), (int) $input->getArgument( 'expiration' ) );
break;
case 'delete':
$cache->delete( $key );
break;
default:
$output->writeln( 'Invalid action.' );
break;
}

return Command::SUCCESS;
}
}
2 changes: 2 additions & 0 deletions src/src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use QIT_CLI\App;
use QIT_CLI\Cache;
use QIT_CLI\Commands\CacheCommand;
use QIT_CLI\Commands\ConfigDirCommand;
use QIT_CLI\Commands\CreateMassTestCommands;
use QIT_CLI\Commands\CreateRunCommands;
Expand Down Expand Up @@ -197,6 +198,7 @@ public function filter( $in, $out, &$consumed, $closing ): int {
$application->add( $container->make( AddBackend::class ) );
$application->add( $container->make( SetProxyCommand::class ) );
$application->add( $container->make( SyncCommand::class ) );
$application->add( $container->make( CacheCommand::class ) );

// Only show options to remove and see the current environment if there's at least one environment added.
if ( count( ManagerBackend::get_configured_manager_backends( false ) ) > 0 ) {
Expand Down

0 comments on commit ad5ee8d

Please sign in to comment.