Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,8 @@ func runCmd(cmd *cobra.Command, args []string) error {
signalMgr := newSignalManager()
// short circuit when --input flag is set
if flagInput != "" {
// create output directory
err := common.CreateOutputDir(localOutputDir)
if err != nil {
err = fmt.Errorf("failed to create output directory: %w", err)
fmt.Fprintf(os.Stderr, "Error: %+v\n", err)
cmd.SilenceUsage = true
return err
}
// skip data collection and use raw data for reports
err = processRawData(localOutputDir)
err := processRawData(localOutputDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
slog.Error(err.Error())
Expand Down Expand Up @@ -1057,16 +1049,6 @@ func runCmd(cmd *cobra.Command, args []string) error {
createPrometheusMetrics(targetContext.metricDefinitions)
}
}
// create the local output directory
if !flagLive && !flagPrometheusServer {
err = common.CreateOutputDir(localOutputDir)
if err != nil {
err = fmt.Errorf("failed to create output directory: %w", err)
fmt.Fprintf(os.Stderr, "Error: %+v\n", err)
cmd.SilenceUsage = true
return err
}
}
// start the metric production for each target
collectOnTargetWG := sync.WaitGroup{}
for i := range targetContexts {
Expand Down
48 changes: 37 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,33 @@ func Execute() {
}
}

// createOutputDir creates the output directory if it does not exist.
// Returns true if the directory was created, false if it already existed.
func createOutputDir(outputDir string) (bool, error) {
// Check if directory already exists
info, err := os.Stat(outputDir)
if err == nil {
// Path exists, verify it's a directory
if !info.IsDir() {
return false, fmt.Errorf("output path exists but is not a directory: %s", outputDir)
}
return false, nil // Already exists
}
// If error is not "not exists", something else is wrong
if !os.IsNotExist(err) {
return false, fmt.Errorf("failed to check output directory: %w", err)
}
// Directory doesn't exist, create it
err = os.MkdirAll(outputDir, 0755) // #nosec G301
if err != nil {
return false, fmt.Errorf("failed to create output directory: %w", err)
}
return true, nil // Created successfully
}

func initializeApplication(cmd *cobra.Command, args []string) error {
timestamp := time.Now().Local().Format("2006-01-02_15-04-05") // app startup time
// verify requested output directory exists or create an output directory
// set output directory path (directory will be created later when needed)
var outputDir string
if flagOutputDir != "" {
var err error
Expand All @@ -157,17 +181,8 @@ func initializeApplication(cmd *cobra.Command, args []string) error {
fmt.Printf("Error: failed to expand output dir %v\n", err)
os.Exit(1)
}
exists, err := util.DirectoryExists(outputDir)
if err != nil {
fmt.Printf("Error: failed to determine if output dir exists: %v\n", err)
os.Exit(1)
}
if !exists {
fmt.Printf("Error: requested output dir, %s, does not exist\n", outputDir)
os.Exit(1)
}
} else {
// set output dir path to app name + timestamp (dont' create the directory)
// set output dir path to app name + timestamp
outputDirName := common.AppName + "_" + timestamp
var err error
// outputDir will be in current working directory
Expand Down Expand Up @@ -220,6 +235,17 @@ func initializeApplication(cmd *cobra.Command, args []string) error {
if gLogFile != nil {
logFilePath = gLogFile.Name()
}
// create the output directory now to fail fast if there are permission or disk space issues
// this validates write access before any data collection begins
created, err := createOutputDir(outputDir)
if err != nil {
slog.Error("failed to create output directory", slog.String("path", outputDir), slog.String("error", err.Error()))
fmt.Printf("Error: failed to create output directory: %v\n", err)
os.Exit(1)
}
if created {
slog.Debug("output directory created", slog.String("path", outputDir))
}
// set app context
cmd.Parent().SetContext(
context.WithValue(
Expand Down
19 changes: 1 addition & 18 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,9 @@ func (rc *ReportingCommand) Run() error {
return err
}
}
// we have output data so create the output directory
err := CreateOutputDir(outputDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
slog.Error(err.Error())
rc.Cmd.SilenceUsage = true
return err
}
// create the raw report before processing the data, so that we can save the raw data even if there is an error while processing
var rawReports []string
rawReports, err = rc.createRawReports(appContext, orderedTargetScriptOutputs)
rawReports, err := rc.createRawReports(appContext, orderedTargetScriptOutputs)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
slog.Error(err.Error())
Expand Down Expand Up @@ -289,15 +281,6 @@ func (rc *ReportingCommand) Run() error {
return nil
}

// CreateOutputDir creates the output directory if it does not exist
func CreateOutputDir(outputDir string) error {
err := os.MkdirAll(outputDir, 0755) // #nosec G301
if err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
return nil
}

// DefaultInsightsFunc returns the insights table values from the table values
func DefaultInsightsFunc(allTableValues []report.TableValues, scriptOutputs map[string]script.ScriptOutput) report.TableValues {
insightsTableValues := report.TableValues{
Expand Down