Skip to content

Commit feea71a

Browse files
Jérémy Christillinclaude
andcommitted
feat: add interactive server editing wizard
Implemented comprehensive server editing functionality that was previously showing "Edit feature coming soon..." placeholder. Features added: - wizard_edit_server() function with full guided editing experience - update_server_in_env() helper to safely update server configurations - Support for editing all server fields except name (host, user, port, auth, description, default_dir) - Proper handling of authentication method switching (password ↔ key) - Command-line support: `ssh-manager server edit [name]` - Clean removal of old configuration including comments Changes: - cli/lib/config.sh: Added update_server_in_env() function - cli/lib/menu.sh: Added wizard_edit_server() with step-by-step editing - cli/ssh-manager: Replaced placeholder with wizard call - cli/commands/server.sh: Enhanced cmd_server_edit() to support interactive editing The wizard shows current values as defaults, validates inputs, and offers to test the connection after changes are saved. Authentication method changes properly clean up old credentials. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e45544f commit feea71a

File tree

4 files changed

+211
-11
lines changed

4 files changed

+211
-11
lines changed

cli/commands/server.sh

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,43 @@ cmd_server_remove() {
191191
fi
192192
}
193193

194-
# Edit server configuration
195-
cmd_server_edit() {
194+
# Edit server configuration (full .env file)
195+
cmd_server_edit_file() {
196196
local editor=$(get_config "default_editor" "${EDITOR:-nano}")
197-
197+
198198
if [ ! -f "$SSH_MANAGER_ENV" ]; then
199199
print_error "Configuration file not found: $SSH_MANAGER_ENV"
200200
return 1
201201
fi
202-
202+
203203
print_info "Opening configuration in $editor..."
204204
$editor "$SSH_MANAGER_ENV"
205-
205+
206206
print_success "Configuration updated"
207207
}
208208

209+
# Edit server interactively
210+
cmd_server_edit() {
211+
local server="$1"
212+
213+
if [ -z "$server" ]; then
214+
# No server specified, launch wizard
215+
wizard_edit_server
216+
else
217+
# Server specified, edit that specific server
218+
# Check if server exists
219+
local host=$(get_server_config "$server" "HOST")
220+
if [ -z "$host" ]; then
221+
print_error "Server '$server' not found"
222+
return 1
223+
fi
224+
225+
# Set SELECTED_SERVER for wizard
226+
SELECTED_SERVER="$server"
227+
wizard_edit_server
228+
fi
229+
}
230+
209231
# Show server details
210232
cmd_server_show() {
211233
local server="$1"

cli/lib/config.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,62 @@ add_server_to_env() {
174174
print_success "Server '$name' added successfully"
175175
}
176176

177+
# Update server in .env
178+
update_server_in_env() {
179+
local name="$1"
180+
local host="$2"
181+
local user="$3"
182+
local auth_type="$4"
183+
local auth_value="$5"
184+
local port="${6:-22}"
185+
local description="${7:-}"
186+
local default_dir="${8:-}"
187+
188+
local name_upper="$(echo "$name" | tr '[:lower:]' '[:upper:]')"
189+
190+
# Check if server exists
191+
if ! grep -q "^SSH_SERVER_${name_upper}_HOST=" "$SSH_MANAGER_ENV" 2>/dev/null; then
192+
print_error "Server '$name' not found"
193+
return 1
194+
fi
195+
196+
# Backup .env file
197+
cp "$SSH_MANAGER_ENV" "$SSH_MANAGER_ENV.bak"
198+
199+
# Remove old server configuration (all lines including comments and old auth methods)
200+
local temp=$(mktemp)
201+
# Remove server config lines and the comment line before them
202+
sed "/^# Server: $name$/d; /^SSH_SERVER_${name_upper}_/d" "$SSH_MANAGER_ENV" > "$temp"
203+
204+
# Find position to insert (after other servers or at end)
205+
# Add updated server configuration
206+
{
207+
cat "$temp"
208+
echo ""
209+
echo "# Server: $name"
210+
echo "SSH_SERVER_${name_upper}_HOST=$host"
211+
echo "SSH_SERVER_${name_upper}_USER=$user"
212+
echo "SSH_SERVER_${name_upper}_PORT=$port"
213+
214+
if [ "$auth_type" = "password" ]; then
215+
echo "SSH_SERVER_${name_upper}_PASSWORD=$auth_value"
216+
else
217+
echo "SSH_SERVER_${name_upper}_KEYPATH=$auth_value"
218+
fi
219+
220+
if [ -n "$description" ]; then
221+
echo "SSH_SERVER_${name_upper}_DESCRIPTION=\"$description\""
222+
fi
223+
224+
if [ -n "$default_dir" ]; then
225+
echo "SSH_SERVER_${name_upper}_DEFAULT_DIR=$default_dir"
226+
fi
227+
} > "$SSH_MANAGER_ENV"
228+
229+
rm -f "$temp"
230+
print_success "Server '$name' updated successfully"
231+
}
232+
177233
# Remove server from .env
178234
remove_server_from_env() {
179235
local name="$1"

cli/lib/menu.sh

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,130 @@ wizard_add_server() {
231231
fi
232232
}
233233

234+
# Edit server wizard
235+
wizard_edit_server() {
236+
# Select server to edit
237+
if ! select_server_menu "Select Server to Edit"; then
238+
return 1
239+
fi
240+
241+
local server_name="$SELECTED_SERVER"
242+
243+
# Load existing configuration
244+
local current_host=$(get_server_config "$server_name" "HOST")
245+
local current_user=$(get_server_config "$server_name" "USER")
246+
local current_port=$(get_server_config "$server_name" "PORT")
247+
local current_keypath=$(get_server_config "$server_name" "KEYPATH")
248+
local current_password=$(get_server_config "$server_name" "PASSWORD")
249+
local current_description=$(get_server_config "$server_name" "DESCRIPTION")
250+
local current_default_dir=$(get_server_config "$server_name" "DEFAULT_DIR")
251+
252+
current_port=${current_port:-22}
253+
254+
# Determine current auth type
255+
local current_auth_type="key"
256+
if [ -n "$current_password" ]; then
257+
current_auth_type="password"
258+
fi
259+
260+
clear
261+
print_header "Edit Server - $server_name"
262+
print_info "Press Enter to keep current value, or type new value"
263+
echo
264+
265+
# Step 1: Connection Details
266+
print_subheader "Step 1: Connection Details"
267+
echo "----------------------------------------"
268+
269+
local host
270+
prompt_input "Host/IP" "$current_host" "host"
271+
272+
local user
273+
prompt_input "Username" "$current_user" "user"
274+
275+
local port
276+
prompt_input "Port" "$current_port" "port"
277+
278+
# Step 2: Authentication Method
279+
echo
280+
print_subheader "Step 2: Authentication Method"
281+
echo "----------------------------------------"
282+
echo "Current method: $current_auth_type"
283+
echo
284+
echo " 1) 🔑 SSH Key (Recommended)"
285+
echo " More secure, no password needed"
286+
echo
287+
echo " 2) 🔒 Password"
288+
echo " Less secure, password required each time"
289+
echo
290+
read -p "Choose [1-2] or Enter to keep current: " auth_choice
291+
292+
local auth_type auth_value
293+
if [ -z "$auth_choice" ]; then
294+
# Keep current auth method
295+
auth_type="$current_auth_type"
296+
if [ "$auth_type" = "password" ]; then
297+
auth_value="$current_password"
298+
else
299+
auth_value="$current_keypath"
300+
fi
301+
else
302+
case "$auth_choice" in
303+
2)
304+
auth_type="password"
305+
prompt_password "Password" "auth_value"
306+
;;
307+
*)
308+
auth_type="key"
309+
prompt_input "SSH key path" "${current_keypath:-$HOME/.ssh/id_rsa}" "auth_value"
310+
# Expand ~ to home directory
311+
auth_value="${auth_value/#\~/$HOME}"
312+
;;
313+
esac
314+
fi
315+
316+
# Step 3: Optional Settings
317+
echo
318+
print_subheader "Step 3: Optional Settings"
319+
echo "----------------------------------------"
320+
321+
local description
322+
prompt_input "Description (optional)" "$current_description" "description"
323+
324+
local default_dir
325+
prompt_input "Default directory (optional)" "$current_default_dir" "default_dir"
326+
327+
# Show summary
328+
echo
329+
print_subheader "Configuration Summary"
330+
print_table_row "Name:" "$server_name"
331+
print_table_row "Host:" "$host"
332+
print_table_row "User:" "$user"
333+
print_table_row "Port:" "$port"
334+
print_table_row "Auth:" "$auth_type"
335+
if [ "$auth_type" = "key" ]; then
336+
print_table_row "Key:" "$auth_value"
337+
fi
338+
if [ -n "$description" ]; then
339+
print_table_row "Description:" "$description"
340+
fi
341+
if [ -n "$default_dir" ]; then
342+
print_table_row "Default Dir:" "$default_dir"
343+
fi
344+
345+
echo
346+
if prompt_yes_no "Save changes?" "y"; then
347+
update_server_in_env "$server_name" "$host" "$user" "$auth_type" "$auth_value" "$port" "$description" "$default_dir"
348+
349+
echo
350+
if prompt_yes_no "Test connection now?" "y"; then
351+
test_ssh_connection "$server_name"
352+
fi
353+
else
354+
print_info "Changes cancelled"
355+
fi
356+
}
357+
234358
# Server selection menu
235359
select_server_menu() {
236360
local prompt_text="${1:-Select a server}"

cli/ssh-manager

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,9 @@ interactive_mode() {
414414
fi
415415
;;
416416
5)
417-
if select_server_menu "Edit Server"; then
418-
print_info "Edit feature coming soon..."
419-
echo
420-
read -p "Press Enter to continue..."
421-
fi
417+
wizard_edit_server
418+
echo
419+
read -p "Press Enter to continue..."
422420
;;
423421
6)
424422
if select_server_menu "Remove Server"; then
@@ -431,7 +429,7 @@ interactive_mode() {
431429
fi
432430
;;
433431
7)
434-
cmd_server_edit
432+
cmd_server_edit_file
435433
echo
436434
read -p "Press Enter to continue..."
437435
;;

0 commit comments

Comments
 (0)