Skip to content

Commit aa1560d

Browse files
committed
bug fixes.
1 parent 6086bf1 commit aa1560d

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

src/Cli/Commands/Secrets/EditCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,17 @@ public function execute(): int
5656
{
5757
$configPath = $this->input->getOption( 'config', 'config' );
5858
$env = $this->input->getOption( 'env' );
59-
$editor = $this->input->getOption( 'editor' ) ?? getenv( 'EDITOR' ) ?: 'vi';
59+
60+
// Handle editor option - could be null, true (flag without value), or a string
61+
$editorOption = $this->input->getOption( 'editor' );
62+
if( is_string( $editorOption ) && $editorOption !== '' )
63+
{
64+
$editor = $editorOption;
65+
}
66+
else
67+
{
68+
$editor = getenv( 'EDITOR' ) ?: 'vi';
69+
}
6070

6171
// Determine paths based on environment
6272
if( $env )
@@ -106,8 +116,10 @@ public function execute(): int
106116
{
107117
$this->output->success( "Secrets saved to: {$credentialsPath}" );
108118

109-
// First time setup reminder
110-
if( !$env && !file_exists( $configPath . '/.gitignore' ) )
119+
// First time setup reminder - check for .gitignore in project root
120+
$projectRoot = dirname( $configPath );
121+
$gitignorePath = $projectRoot . '/.gitignore';
122+
if( !$env && !file_exists( $gitignorePath ) )
111123
{
112124
$this->output->newLine();
113125
$this->output->warning( "Remember to:" );

src/Cli/Commands/Secrets/ShowCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ public function execute(): int
103103
}
104104

105105
// Create SecretManager and decrypt
106+
// Note: SecretManager::show() internally calls readKey() which handles both file and environment variable sources
106107
$this->secretManager = new SecretManager();
107108

108109
try
109110
{
111+
// The show() method will read the key from either the file or environment variable
110112
$decrypted = $this->secretManager->show( $credentialsPath, $keyPath );
111113
$data = Yaml::parse( $decrypted );
112114

tests/Cli/Commands/Secrets/EditCommandTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,70 @@ public function testExecuteCreatesEnvironmentDirectory(): void
207207
$this->assertStringContainsString( "Secrets saved to: {$credentialsPath}", $outputContent );
208208
}
209209

210+
/**
211+
* Test editor option handling with various input types
212+
*/
213+
public function testEditorOptionHandling(): void
214+
{
215+
// Create a key file first
216+
$keyPath = $this->testConfigPath . '/master.key';
217+
$secretManager = new SecretManager();
218+
$secretManager->generateKey( $keyPath );
219+
220+
// Create initial credentials file
221+
$credentialsPath = $this->testConfigPath . '/secrets.yml.enc';
222+
$tempPlaintextPath = $this->testConfigPath . '/temp.yml';
223+
file_put_contents( $tempPlaintextPath, "test: value" );
224+
$secretManager->encrypt( $tempPlaintextPath, $credentialsPath, $keyPath );
225+
unlink( $tempPlaintextPath );
226+
227+
// Test 1: Editor option with value
228+
$input1 = new Input( [
229+
'--config=' . $this->testConfigPath,
230+
'--editor=echo' // Use echo as a no-op editor
231+
] );
232+
$input1->parse( $this->command );
233+
234+
$output1 = new Output( false );
235+
$this->command->setInput( $input1 );
236+
$this->command->setOutput( $output1 );
237+
238+
// This should use 'echo' as editor
239+
ob_start();
240+
$result1 = $this->command->execute();
241+
$outputContent1 = ob_get_clean();
242+
243+
// Should succeed with echo editor
244+
$this->assertEquals( 0, $result1 );
245+
$this->assertStringContainsString( "Secrets saved to", $outputContent1 );
246+
247+
// Test 2: Editor option without value (becomes boolean true)
248+
$input2 = new Input( [
249+
'--config=' . $this->testConfigPath,
250+
'--editor' // No value - becomes boolean true
251+
] );
252+
$input2->parse( $this->command );
253+
254+
$output2 = new Output( false );
255+
$this->command->setInput( $input2 );
256+
$this->command->setOutput( $output2 );
257+
258+
// This should fall back to EDITOR env var or 'vi'
259+
// Set a test editor to avoid vi
260+
putenv( 'EDITOR=echo' );
261+
262+
ob_start();
263+
$result2 = $this->command->execute();
264+
$outputContent2 = ob_get_clean();
265+
266+
// Should succeed with fallback to env var
267+
$this->assertEquals( 0, $result2 );
268+
$this->assertStringContainsString( "Secrets saved to", $outputContent2 );
269+
270+
// Clean up env var
271+
putenv( 'EDITOR' );
272+
}
273+
210274
/**
211275
* Test that error is handled gracefully
212276
*/

0 commit comments

Comments
 (0)