Skip to content

refactor: Extract profile command handlers from main.rs to dedicated module #402

@joshrotenberg

Description

@joshrotenberg

Problem

The main.rs file has grown to 1,589 lines and contains substantial command implementation logic that should be modularized. The execute_profile_command function alone spans ~660 lines (lines 755-1415), making main.rs harder to maintain and test.

Current State

File Size

main.rs: 1,589 lines
  - execute_profile_command: ~660 lines (42% of file)
  - execute_files_key_command: ~40 lines  
  - execute_api_command: ~20 lines
  - Other command dispatchers and utilities

Already Modularized ✅

  • commands/api.rs - API commands properly extracted
  • commands/files_key.rs - Files.com key management extracted
  • commands/cloud/* - Cloud commands modularized
  • commands/enterprise/* - Enterprise commands modularized

Still in main.rs ❌

  • Profile commands - 660 lines of implementation in execute_profile_command
  • Command dispatching logic
  • Some workflow command handlers

Proposed Refactoring

Phase 1: Extract Profile Commands (High Priority)

Create commands/profile.rs following the existing pattern:

// commands/profile.rs
pub mod profile {
    use crate::cli::{OutputFormat, ProfileCommands};
    use crate::config::Config;
    use crate::connection::ConnectionManager;
    use crate::error::Result as CliResult;
    
    pub async fn handle_profile_command(
        profile_cmd: &ProfileCommands,
        conn_mgr: &ConnectionManager,
        output_format: OutputFormat,
    ) -> CliResult<()> {
        match profile_cmd {
            ProfileCommands::List => handle_list(conn_mgr, output_format).await,
            ProfileCommands::Show { name } => handle_show(conn_mgr, name, output_format).await,
            ProfileCommands::Set { .. } => handle_set(/* ... */).await,
            ProfileCommands::Remove { name } => handle_remove(conn_mgr, name).await,
            ProfileCommands::Validate => handle_validate(conn_mgr).await,
            // ... other commands
        }
    }
    
    async fn handle_list(
        conn_mgr: &ConnectionManager,
        output_format: OutputFormat,
    ) -> CliResult<()> {
        // Move list implementation here (~100 lines)
    }
    
    async fn handle_validate(conn_mgr: &ConnectionManager) -> CliResult<()> {
        // Move validate implementation here (~120 lines)
    }
    
    // ... other handler functions
}

Update main.rs:

Commands::Profile(profile_cmd) => {
    commands::profile::handle_profile_command(profile_cmd, &conn_mgr, cli.output).await?
}

Phase 2: Simplify main.rs (Medium Priority)

After extracting profile commands, main.rs should focus on:

  • CLI parsing and initialization
  • Configuration loading
  • Command routing/dispatching only
  • Error handling at the top level

Target: < 300 lines for main.rs

Phase 3: Consider Additional Extractions (Low Priority)

  • Move format_command() to a telemetry/logging module
  • Extract workflow dispatchers to commands/workflows.rs
  • Consider moving init_tracing() to a separate module

Benefits

  1. Maintainability: Smaller, focused files easier to understand
  2. Testability: Profile commands can have unit tests in commands/profile.rs
  3. Consistency: Matches existing pattern (api.rs, files_key.rs)
  4. Separation of Concerns: main.rs focuses on application entry point
  5. Easier Navigation: Developers can find profile logic in one place

Testing Requirements

  • All existing tests must continue to pass
  • Consider adding unit tests for individual profile handlers
  • Integration tests should remain unchanged

Implementation Notes

  • Follow the pattern established by commands/api.rs and commands/files_key.rs
  • Maintain all existing functionality - this is purely a code organization change
  • No behavior changes - only code movement
  • Keep public API surface minimal

Acceptance Criteria

  • commands/profile.rs module created with all profile command handlers
  • main.rs reduced to < 500 lines (target: ~300)
  • All 67 tests still passing
  • No clippy warnings
  • Code formatted with cargo fmt
  • Documentation updated if needed

Priority

Medium - This is technical debt cleanup that will improve maintainability but isn't blocking any features.

Related

  • Similar to how commands/api.rs and commands/files_key.rs were already extracted
  • Part of ongoing code quality improvements

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions