You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix Destroy Command: Accept Working Directory Parameter
Epic Subissue: 9 of 9 Issue: #Y (to be assigned) Parent Epic: #34 - Create Environment Command Depends On: Subissue 8 - Fix Destroy Command Created State Handling Related: Destroy Presentation Layer, Command Dispatcher
Overview
Fix a critical bug in the destroy command where it doesn't accept or use the --working-dir parameter, causing it to fail when environments are created with custom working directories. The destroy command is hardcoded to look for environments in the data/ directory, ignoring the --working-dir CLI argument that the create command properly supports.
This bug prevents users from managing environment lifecycles when working with custom workspace locations, a feature explicitly supported by the --working-dir flag in the CLI.
Dependencies: This issue depends on Subissue 8 (Created State Handling) being completed first, as manual testing requires being able to destroy Created state environments successfully.
Goals
Fix destroy command to accept working_dir parameter
Pass working_dir through the command execution chain
Maintain backward compatibility with default behavior
Add comprehensive tests for both default and custom working directories
Default to . (current directory) when not specified
Implementation Plan
Subtask 1: Update Presentation Layer (30 minutes)
Modify destroy::handle() signature to accept working_dir: &Path
Update repository creation to use the provided working directory
Verify compilation succeeds
Subtask 2: Update Command Dispatcher (30 minutes)
Modify commands::handle_command() to pass working_dir to destroy::handle()
Verify integration with CLI argument parsing
Test with both default and custom working directories
Subtask 3: Testing (2-3 hours)
Add unit test: destroy with custom working directory
Add integration test: create → destroy with default working directory
Add integration test: create → destroy with custom working directory (temp dir)
Add integration test: create → provision → destroy with custom working directory
Update existing tests if needed
Verify all pre-commit checks pass
Acceptance Criteria
Quality Checks:
Pre-commit checks pass: ./scripts/pre-commit.sh
Bug Fix - Working Directory Support:
Destroy command accepts working_dir parameter in its signature
working_dir is properly passed from CLI through command dispatcher to destroy handler
Repository factory uses provided working_dir instead of hardcoded "data"
Default behavior unchanged (works with environments in ./data/ when using default --working-dir .)
Manual Test 1 - Default working directory:
# Create test configuration
cat > /tmp/test-default.json << 'EOF'{ "environment": { "name": "test-default" }, "ssh_credentials": { "private_key_path": "fixtures/testing_rsa", "public_key_path": "fixtures/testing_rsa.pub", "username": "torrust", "port": 22 }}EOF# Create environment in default location (current directory)
./target/release/torrust-tracker-deployer create environment --env-file /tmp/test-default.json --log-output file-and-stderr
# Verify environment created in ./data/test-default/
[ -f ./data/test-default/environment.json ] &&echo"✅ Environment created in default location"# Destroy should work without specifying working-dir (uses default: .)
./target/release/torrust-tracker-deployer destroy test-default --log-output file-and-stderr
# Verify environment removed
[ !-d ./data/test-default ] &&echo"✅ Environment destroyed successfully"# Cleanup
rm /tmp/test-default.json
Manual Test 2 - Custom working directory:
# Create test configuration
cat > /tmp/test-custom.json << 'EOF'{ "environment": { "name": "test-custom" }, "ssh_credentials": { "private_key_path": "fixtures/testing_rsa", "public_key_path": "fixtures/testing_rsa.pub", "username": "torrust", "port": 22 }}EOF# Create environment in temporary directory
TEMP_DIR=$(mktemp -d)echo"Using temp directory: $TEMP_DIR"
./target/release/torrust-tracker-deployer --working-dir "$TEMP_DIR" create environment --env-file /tmp/test-custom.json --log-output file-and-stderr
# Verify environment created in temp directory
[ -f"$TEMP_DIR/data/test-custom/environment.json" ] &&echo"✅ Environment created in custom location"# Destroy should work with same working-dir
./target/release/torrust-tracker-deployer --working-dir "$TEMP_DIR" destroy test-custom --log-output file-and-stderr
# Verify environment removed
[ !-d"$TEMP_DIR/data/test-custom" ] &&echo"✅ Environment destroyed successfully"# Cleanup
rm -rf "$TEMP_DIR"
rm /tmp/test-custom.json
Manual Test 3 - Full lifecycle with custom working directory:
# This test verifies create → provision → destroy works with custom working directory# Note: This test requires LXD to be available and configured# Create test configuration
cat > /tmp/test-lifecycle.json << 'EOF'{ "environment": { "name": "test-lifecycle" }, "ssh_credentials": { "private_key_path": "fixtures/testing_rsa", "public_key_path": "fixtures/testing_rsa.pub", "username": "torrust", "port": 22 }}EOF# Create temporary workspace
TEMP_DIR=$(mktemp -d)echo"Using temp directory: $TEMP_DIR"# Create environment
./target/release/torrust-tracker-deployer --working-dir "$TEMP_DIR" create environment --env-file /tmp/test-lifecycle.json
# Provision environment (currently provision command may not support --working-dir, use default for now)# TODO: Update this test when provision command also supports --working-dir# For now, this test documents the expected behavior# Destroy should work with custom working-dir
./target/release/torrust-tracker-deployer --working-dir "$TEMP_DIR" destroy test-lifecycle --log-output file-and-stderr
# Verify environment removed
[ !-d"$TEMP_DIR/data/test-lifecycle" ] &&echo"✅ Environment destroyed successfully"# Cleanup
rm -rf "$TEMP_DIR"
rm /tmp/test-lifecycle.json
Integration:
Destroy command works correctly with default working directory (backward compatibility)
Destroy command works correctly with custom working directories
Destroy command behavior matches create command's working directory handling
Error messages are clear when environment is not found in the specified working directory
Fix Destroy Command: Accept Working Directory Parameter
Epic Subissue: 9 of 9
Issue: #Y (to be assigned)
Parent Epic: #34 - Create Environment Command
Depends On: Subissue 8 - Fix Destroy Command Created State Handling
Related: Destroy Presentation Layer, Command Dispatcher
Overview
Fix a critical bug in the destroy command where it doesn't accept or use the
--working-dirparameter, causing it to fail when environments are created with custom working directories. The destroy command is hardcoded to look for environments in thedata/directory, ignoring the--working-dirCLI argument that the create command properly supports.This bug prevents users from managing environment lifecycles when working with custom workspace locations, a feature explicitly supported by the
--working-dirflag in the CLI.Dependencies: This issue depends on Subissue 8 (Created State Handling) being completed first, as manual testing requires being able to destroy Created state environments successfully.
Goals
working_dirparameterworking_dirthrough the command execution chain🏗️ Architecture Requirements
DDD Layer: Presentation (CLI interface)
Module Path:
src/presentation/commands/destroy.rs+src/presentation/commands/mod.rsPattern: CLI Command Pattern
Module Structure Requirements
Architectural Constraints
.)Anti-Patterns to Avoid
Specifications
Current Behavior (Incorrect)
The destroy command is hardcoded to look for environments in the
data/directory:The command dispatcher doesn't pass
working_dirto the destroy handler:Impact: Environments created with custom
--working-dircannot be found by the destroy command:Expected Behavior
The destroy command should accept and use the
working_dirparameter just like the create command:Step 1: Update
destroy::handle()signature to acceptworking_dir:Step 2: Update the command dispatcher to pass
working_dir:Consistency with Create Command
The fix ensures destroy behaves exactly like create:
Both commands now:
working_dirparameter from CLI.(current directory) when not specifiedImplementation Plan
Subtask 1: Update Presentation Layer (30 minutes)
destroy::handle()signature to acceptworking_dir: &PathSubtask 2: Update Command Dispatcher (30 minutes)
commands::handle_command()to passworking_dirtodestroy::handle()Subtask 3: Testing (2-3 hours)
Acceptance Criteria
Quality Checks:
./scripts/pre-commit.shBug Fix - Working Directory Support:
Destroy command accepts
working_dirparameter in its signatureworking_diris properly passed from CLI through command dispatcher to destroy handlerRepository factory uses provided
working_dirinstead of hardcoded"data"Default behavior unchanged (works with environments in
./data/when using default--working-dir .)Manual Test 1 - Default working directory:
Manual Test 2 - Custom working directory:
Manual Test 3 - Full lifecycle with custom working directory:
Integration:
Related Documentation
Notes
Discovery Context
This bug was discovered during manual testing of the create environment command (Issue #34):
--working-dir ../my-test/environment.json(root-level data directory)data/my-test/data/my-test/, the destroy command could find it--working-dirparameterWhy This Issue Depends on Subissue 8
Manual testing for this issue requires creating environments and then destroying them. To test properly, we need:
Without Subissue 8 fixed, testing would fail because:
By completing Issue #X first, we ensure that any failures in this issue are truly related to working directory handling, not state handling.
Consistency Across Commands
After this fix, working directory handling will be consistent:
Before:
createcommand: Uses--working-dirparameterdestroycommand: Ignores--working-dir, hardcoded todata/After:
createcommand: Uses--working-dirparameterdestroycommand: Uses--working-dirparameterFuture work: Other commands (
provision,configure, etc.) should also be audited for consistent working directory support.Backward Compatibility
This fix maintains backward compatibility:
--working-diris not specified, it defaults to.(current directory)--working-dircontinue to work exactly as before./data/{ENV_NAME}/by defaultThe fix only changes behavior when users explicitly specify a custom
--working-dir, which currently doesn't work for destroy.Implementation Simplicity
The fix is straightforward:
working_dir: &Pathrepository_factory.create(working_dir.to_path_buf())destroy::handle(&environment, working_dir)?Total lines changed: ~3 lines of code
This is a minimal, low-risk change that significantly improves user experience when working with multiple workspaces.