Commit 2c46b13
committed
Merge #218: refactor: extract private methods from run_command for single responsibility
acf3e5e refactor: extract private methods from run_command for single responsibility (Jose Celano)
15896eb refactor: extract working directory validation into guard clause method (Jose Celano)
Pull request description:
## Summary
Refactors the `run_command` method in `CommandExecutor` to improve readability and maintainability by extracting helper methods, each with a single responsibility.
## Changes
The `run_command` method was too large with multiple abstraction levels mixed together. This PR extracts the following private helper methods:
- **`validate_working_directory`** - Guard clause for directory existence validation
- **`build_command`** - Constructs `Command` with args and working directory
- **`format_command_display`** - Formats command string for logs/error messages
- **`log_command_start`** - Logs command execution start with tracing
- **`execute_command`** - Runs the command and returns `(ExitStatus, stdout, stderr)`
- **`extract_output`** - Converts raw output bytes to strings
- **`check_command_success`** - Validates exit status and returns error if failed
- **`log_command_output`** - Logs stdout/stderr at debug level
## Result
The `run_command` method now reads as a clean, high-level workflow:
```rust
pub fn run_command(...) -> Result<CommandResult, CommandError> {
Self::validate_working_directory(working_dir)?;
let mut command = Self::build_command(cmd, args, working_dir);
let command_display = Self::format_command_display(cmd, args);
Self::log_command_start(&command_display, working_dir);
let (status, stdout, stderr) = Self::execute_command(&mut command, &command_display)?;
Self::check_command_success(status, &command_display, &stdout, &stderr)?;
Self::log_command_output(&command_display, &stdout, &stderr);
Ok(CommandResult::new(status, stdout, stderr))
}
```
Each step is at the same abstraction level, making the code easier to understand and maintain.
## Testing
- All existing unit tests pass (7 tests in executor module)
- All 1250+ tests in the project pass
- E2E tests pass
ACKs for top commit:
josecelano:
ACK acf3e5e
Tree-SHA512: f85e12f1e5b95981a965dd930c638abe44915901aa1cefdb223f657039449bb43cf6e8620b2abc53bfa06007ac459c0480c54f53970483e68d3dbd04d62a0cc6File tree
1 file changed
+73
-14
lines changed1 file changed
+73
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
54 | 74 | | |
55 | 75 | | |
56 | 76 | | |
| |||
59 | 79 | | |
60 | 80 | | |
61 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
62 | 87 | | |
63 | | - | |
64 | 88 | | |
65 | 89 | | |
66 | 90 | | |
67 | 91 | | |
68 | 92 | | |
69 | 93 | | |
70 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
71 | 105 | | |
72 | 106 | | |
73 | 107 | | |
74 | 108 | | |
75 | 109 | | |
| 110 | + | |
76 | 111 | | |
77 | 112 | | |
78 | 113 | | |
79 | 114 | | |
80 | 115 | | |
81 | 116 | | |
82 | 117 | | |
| 118 | + | |
83 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
84 | 127 | | |
85 | 128 | | |
86 | 129 | | |
87 | 130 | | |
88 | 131 | | |
89 | | - | |
| 132 | + | |
90 | 133 | | |
91 | 134 | | |
92 | 135 | | |
93 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
94 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
95 | 147 | | |
96 | | - | |
97 | | - | |
98 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
99 | 157 | | |
100 | 158 | | |
101 | 159 | | |
102 | 160 | | |
103 | | - | |
| 161 | + | |
104 | 162 | | |
105 | | - | |
106 | | - | |
| 163 | + | |
| 164 | + | |
107 | 165 | | |
108 | 166 | | |
| 167 | + | |
| 168 | + | |
109 | 169 | | |
110 | | - | |
| 170 | + | |
| 171 | + | |
111 | 172 | | |
112 | 173 | | |
113 | 174 | | |
| |||
125 | 186 | | |
126 | 187 | | |
127 | 188 | | |
128 | | - | |
129 | | - | |
130 | 189 | | |
131 | 190 | | |
132 | 191 | | |
| |||
0 commit comments