Skip to content

Conversation

@harp-intel
Copy link
Contributor

This pull request refactors the logic for creating output directories, centralizing it for better maintainability and ensuring that directories are only created when necessary. The main changes include removing duplicate output directory creation logic from cmd/root.go, introducing a reusable helper function, and updating command handlers to use this new approach.

Refactoring output directory creation logic:

  • Removed the custom output directory creation logic from cmd/root.go, including the createOutputDir function and related conditional checks, to avoid duplication and simplify command initialization. [1] [2]
  • Added a new helper function needsOutputDir in cmd/metrics/metrics.go to determine when the output directory should be created, based on command flags and mode.
  • Updated the runCmd function in cmd/metrics/metrics.go to use needsOutputDir and create the output directory only when required, improving error handling and user feedback.

Applying centralized directory creation in reporting commands:

  • Updated internal/common/common.go to create the output directory at the start of the ReportingCommand.Run method, using the shared utility function for consistency and error handling.

Minor code correctness fix:

  • Fixed variable assignment in ReportingCommand.Run to use the existing rawReports variable, preventing shadowing and potential bugs.

Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
@harp-intel harp-intel requested a review from Copilot November 26, 2025 21:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors output directory creation logic by moving it from the root command initialization to individual command handlers. The changes eliminate duplicate code, ensure directories are created only when necessary, and improve error handling consistency.

Key changes:

  • Removed centralized output directory creation logic from cmd/root.go that attempted to predict which commands need output directories
  • Added command-specific directory creation in cmd/metrics/metrics.go and internal/common/common.go where output is actually generated
  • Fixed a variable shadowing bug in internal/common/common.go

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cmd/root.go Removed the createOutputDir function and preemptive directory creation logic from application initialization
cmd/metrics/metrics.go Added needsOutputDir helper function and directory creation at the start of runCmd
internal/common/common.go Added directory creation at the start of ReportingCommand.Run and fixed variable shadowing issue

Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
@harp-intel harp-intel merged commit 9986904 into main Nov 26, 2025
5 checks passed
@harp-intel harp-intel deleted the outdircmd branch November 26, 2025 22:46
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.

2 participants