Skip to content

Commit

Permalink
Bugfix: make docker exec commands interactive (#102)
Browse files Browse the repository at this point in the history
* Make all docker exec commands interactive by default, which is how docker-compose works

* bump to 5.5.1
  • Loading branch information
defunctl authored Jan 12, 2022
1 parent 96c30e7 commit 1b9c5c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
11 changes: 6 additions & 5 deletions app/Commands/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function handle( Runner $runner, ResultRecorder $recorder ): int {
$tty = true;
}

// Force all docker exec commands to be interactive so the user can provide input, if required
if ( str_contains( $command, 'exec' ) && ! str_contains( $command, ' -i ' ) && ! str_contains( $command, ' --interactive ' ) ) {
$command = str_replace( 'exec', 'exec -i', $command );
}

$response = $runner->output( $this )
->tty( $tty )
->withEnvironmentVariables( [
Expand All @@ -69,11 +74,7 @@ public function handle( Runner $runner, ResultRecorder $recorder ): int {

$recorder->add( $response->process()->getOutput() );

if ( ! $response->ok() ) {
return self::EXIT_ERROR;
}

return self::EXIT_SUCCESS;
return $response->ok() ? self::SUCCESS : self::FAILURE;
}

}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

//'version' => app('git.version'),
'version' => '5.5.0',
'version' => '5.5.1',

/*
|--------------------------------------------------------------------------
Expand Down
52 changes: 47 additions & 5 deletions tests/Feature/Commands/DockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function setUp(): void {
public function test_it_can_proxy_docker_commands() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( "docker exec --tty php-fpm-container-id '/bin/bash'" )
->with( "docker exec -i --tty php-fpm-container-id '/bin/bash'" )
->once()
->andReturnSelf();
$this->runner->shouldReceive( 'ok' )->once()->andReturnTrue();
Expand All @@ -47,7 +47,7 @@ public function test_it_can_proxy_version_options() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( sprintf(
"docker exec --tty php-fpm-container-id php %s",
"docker exec -i --tty php-fpm-container-id php %s",
ArgumentRewriter::OPTION_VERSION )
)
->once()
Expand All @@ -71,7 +71,7 @@ public function test_it_can_proxy_version_flags() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( sprintf(
"docker exec --tty php-fpm-container-id composer %s",
"docker exec -i --tty php-fpm-container-id composer %s",
ArgumentRewriter::FLAG_VERSION )
)
->once()
Expand All @@ -94,7 +94,7 @@ public function test_it_can_proxy_version_flags() {
public function test_it_can_disable_tty() {
$this->runner->shouldReceive( 'tty' )->with( false )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( "docker exec php-fpm-container-id '/bin/bash'" )
->with( "docker exec -i php-fpm-container-id '/bin/bash'" )
->once()
->andReturnSelf();
$this->runner->shouldReceive( 'ok' )->once()->andReturnTrue();
Expand All @@ -113,7 +113,7 @@ public function test_it_can_disable_tty() {
public function test_it_returns_failed_exit_code_on_bad_command() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( "docker exec --tty php-fpm-container-id '/bin/invalid-command'" )
->with( "docker exec -i --tty php-fpm-container-id '/bin/invalid-command'" )
->once()
->andReturnSelf();
$this->runner->shouldReceive( 'ok' )->once()->andReturnFalse();
Expand All @@ -130,4 +130,46 @@ public function test_it_returns_failed_exit_code_on_bad_command() {
$this->assertSame( 1, $tester->getStatusCode() );
}

public function test_it_does_not_add_duplicate_interactive_flags() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( "docker exec --tty -i php-fpm-container-id '/bin/bash'" )
->once()
->andReturnSelf();
$this->runner->shouldReceive( 'ok' )->once()->andReturnTrue();

$command = $this->app->make( Docker::class );

$tester = $this->runCommand( $command, [
'exec',
'--tty',
'-i',
'php-fpm-container-id',
'/bin/bash',
] );

$this->assertSame( 0, $tester->getStatusCode() );
}

public function test_it_does_not_add_duplicate_interactive_options() {
$this->runner->shouldReceive( 'tty' )->with( true )->once()->andReturnSelf();
$this->runner->shouldReceive( 'run' )
->with( "docker exec --tty --interactive php-fpm-container-id '/bin/bash'" )
->once()
->andReturnSelf();
$this->runner->shouldReceive( 'ok' )->once()->andReturnTrue();

$command = $this->app->make( Docker::class );

$tester = $this->runCommand( $command, [
'exec',
'--tty',
'--interactive',
'php-fpm-container-id',
'/bin/bash',
] );

$this->assertSame( 0, $tester->getStatusCode() );
}

}

0 comments on commit 1b9c5c4

Please sign in to comment.