Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

The pkg/cli/logs.go file had grown to 1,354 lines (69% over the 800-line threshold), making it difficult to navigate and maintain. This refactoring splits it into focused modules with clear boundaries.

File Structure

Original:

  • logs.go (1,354 lines) - monolithic implementation

Refactored:

  • logs_command.go (229 lines) - CLI command definition and flag parsing
  • logs_orchestrator.go (683 lines) - download coordination, pagination, filtering
  • logs_github_api.go (269 lines) - GitHub API interactions
  • logs_display.go (180 lines) - table rendering and metrics formatting
  • logs_utils.go (91 lines) - helper functions

Key Changes

  • Extracted GitHub API layer: fetchJobStatuses(), fetchJobDetails(), listWorkflowRunsWithPagination()
  • Isolated orchestration logic: DownloadWorkflowLogs(), downloadRunArtifactsConcurrent()
  • Separated presentation: displayLogsOverview() with metrics formatting
  • Modularized utilities: workflow discovery and helper functions

Each module has a dedicated logger instance following the cli:logs_* naming convention. Public API remains unchanged - NewLogsCommand() and DownloadWorkflowLogs() maintain their signatures.

Result

83% reduction in main command file size (1,354 → 229 lines), with all modules under 700 lines. Clear separation of concerns: command interface, orchestration, API calls, display, and utilities.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/user
    • Triggering command: /usr/bin/gh gh api user --jq .login ithub/workflows 6436160/b112/vet.cfg (http block)
    • Triggering command: /usr/bin/gh gh api user --jq .login 91/push_to_pull_request_branch.js GOPROXY /usr/bin/gh te GOWORK 75227ef3bc569b41--git-dir gh repo�� pload-artifact/git/ref/tags/v5 --json e/git --jq .owner.login + "-1 64/bin/go e/git (http block)
    • Triggering command: /usr/bin/gh gh api user --jq .login ithub/workflows log e/git-remote-https -n1 --format=format:-1 8d2c81aea4ddbf42xterm-color e/git-remote-https om/a�� 2536-38582/test-3046845998/.github/workflows -pack 0/x64/bin/node -json GO111MODULE 64/bin/go git (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor Large Go File: pkg/cli/logs.go</issue_title>
<issue_description>## Overview

The file pkg/cli/logs.go has grown to 1,354 lines, significantly exceeding the healthy 800-line threshold. This makes it difficult to navigate, maintain, and test effectively. The file contains the core gh aw logs command implementation with multiple distinct functional areas that should be split into focused modules.

Current State

  • File: pkg/cli/logs.go
  • Size: 1,354 lines (69% over threshold)
  • Test Coverage: 1,646 lines of tests (1.22 ratio - excellent coverage to preserve!)
  • Complexity: High - handles CLI command setup, GitHub API interactions, concurrent downloads, metrics extraction, output rendering, and workflow discovery

Functional Analysis

The file contains these distinct functional areas:

  1. CLI Command Setup (lines 121-282): Cobra command definition with extensive flags
  2. Core Download Orchestration (lines 283-689): Main DownloadWorkflowLogs function with pagination logic
  3. Concurrent Download Operations (lines 690-948): Artifact downloading with progress tracking
  4. GitHub API Integration (lines 949-1089): Workflow run listing and job status fetching
  5. Output & Display (lines 1090-1272): Table rendering and metrics formatting
  6. Utility Functions (lines 1273-1354): Workflow name discovery and helper functions

Refactoring Strategy

Proposed File Splits

1. logs_command.go (CLI Command Definition)

Responsibility: Cobra command setup, flag definitions, and command entry point
Functions to move:

  • NewLogsCommand() (lines 121-282)

Estimated LOC: ~200 lines

Rationale: Separates the CLI interface layer from business logic


2. logs_orchestrator.go (Download Orchestration)

Responsibility: Main download workflow coordination, pagination, iteration logic
Functions to move:

  • DownloadWorkflowLogs() (lines 283-689)

Estimated LOC: ~450 lines

Rationale: Core orchestration logic is complex enough to warrant its own file


3. logs_download.go (Concurrent Download Operations)

Responsibility: Artifact downloading, concurrent processing, cache management
Functions to move:

  • downloadRunArtifactsConcurrent() (lines 690-948)
  • Related download helper functions

Estimated LOC: ~300 lines

Rationale: Isolates concurrent operations and artifact handling logic


4. logs_github_api.go (GitHub API Integration)

Responsibility: GitHub API calls for workflow runs, jobs, and statuses
Functions to move:

  • listWorkflowRunsWithPagination() (lines 949-1089)
  • fetchJobStatuses() (lines 26-71)
  • fetchJobDetails() (lines 74-119)

Estimated LOC: ~250 lines

Rationale: Encapsulates all GitHub API interactions in one place


5. logs_display.go (Output & Rendering)

Responsibility: Table formatting, metrics display, console output
Functions to move:

  • displayLogsOverview() (lines 1090-1272)

Estimated LOC: ~200 lines

Rationale: Separates presentation logic from data operations


6. logs_utils.go (Utility Functions)

Responsibility: Helper functions for workflow discovery and common operations
Functions to move:

  • getAgenticWorkflowNames() (lines 1273-1332)
  • contains() (lines 1335-1342)
  • Other utility functions

Estimated LOC: ~100 lines

Rationale: Keeps utilities separate and easily reusable


Shared Types & Constants

Create logs_types.go to hold:

  • All struct definitions (WorkflowRun, ProcessedRun, DownloadResult, LogMetrics, etc.)
  • Constants (BatchSize, MaxConcurrentDownloads, MaxIterations)
  • The logger: var logsLog = logger.New("cli:logs")

Estimated LOC: ~100 lines

Test Coverage Plan

The existing test file logs_test.go (1,646 lines) already provides excellent coverage. When splitting the file:

Testing Strategy

  1. Preserve Existing Tests: All 1,646 lines of existing tests remain valid
  2. Run Tests Frequently: After each file split, verify make test-unit passes
  3. No Test Rewrite Needed: Since we're preserving public APIs, existing tests continue to work

Optional Test Organization

Consider splitting logs_test.go to match the new structure (optional, not required):

  • logs_command_test.go - Command flag parsing tests
  • logs_orchestrator_test.go - Main workflow tests
  • logs_download_test.go - Concurrent download tests
  • logs_github_api_test.go - API integration tests
  • logs_display_test.go - Output formatting tests

Target Coverage: Maintain current >80% coverage for all new files

Implementation Guidelines

Phase 1: Create Shared Types File

  1. Create pkg/cli/logs_types.go
  2. Move all struct definitions, constants, and the logger
  3. Update imports in logs.go
  4. Verify `make test-unit...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 18, 2025 10:13
… 1-3)

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor large Go file pkg/cli/logs.go for better maintainability Refactor logs.go: split 1,354-line file into 5 focused modules Dec 18, 2025
Copilot AI requested a review from mnkiefer December 18, 2025 10:31
@pelikhan pelikhan marked this pull request as ready for review December 18, 2025 10:58
@pelikhan pelikhan merged commit 9a7ed3e into main Dec 18, 2025
98 checks passed
@pelikhan pelikhan deleted the copilot/refactor-large-go-file-logs branch December 18, 2025 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[file-diet] Refactor Large Go File: pkg/cli/logs.go

3 participants