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
16 changes: 16 additions & 0 deletions cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,29 @@ func processRawData(localOutputDir string) error {
printOutputFileNames([][]string{filesWritten})
return nil
}

func needsOutputDir(cmd *cobra.Command) bool {
return !flagLive && !flagPrometheusServer && !cmd.Flags().Changed("prometheus-server-addr") && !flagShowMetricNames
}

func runCmd(cmd *cobra.Command, args []string) error {
// appContext is the application context that holds common data and resources.
appContext := cmd.Parent().Context().Value(common.AppContext{}).(common.AppContext)
localTempDir := appContext.LocalTempDir
localOutputDir := appContext.OutputDir
// Setup signal manager for coordinated shutdown
signalMgr := newSignalManager()
// create output directory if needed
if needsOutputDir(cmd) {
err := util.CreateDirectoryIfNotExists(localOutputDir, 0755) // #nosec G301
if err != nil {
err = fmt.Errorf("failed to create output directory: %w", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
slog.Error(err.Error())
cmd.SilenceUsage = true
return err
}
}
// short circuit when --input flag is set
if flagInput != "" {
// skip data collection and use raw data for reports
Expand Down
61 changes: 0 additions & 61 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,43 +211,6 @@ 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
// skip creating output directory for config command since it doesn't generate output files
// also skip for metrics command with --live, --prometheus-server, or --list flags since they don't write files
// also skip for update command since it doesn't generate output files
needsOutputDir := true
if cmd.Name() == "config" || cmd.Name() == "update" {
needsOutputDir = false
} else if cmd.Name() == "metrics" {
// check if --live flag is set
if liveFlag, err := cmd.Flags().GetBool("live"); err == nil && liveFlag {
needsOutputDir = false
}
// check if --prometheus-server flag is set
if prometheusFlag, err := cmd.Flags().GetBool("prometheus-server"); err == nil && prometheusFlag {
needsOutputDir = false
}
// check if --prometheus-server-addr flag has been changed (which implies prometheus server mode)
if cmd.Flags().Changed("prometheus-server-addr") {
needsOutputDir = false
}
// check if --list flag is set (just lists metrics and exits)
if listFlag, err := cmd.Flags().GetBool("list"); err == nil && listFlag {
needsOutputDir = false
}
}
if needsOutputDir {
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 Expand Up @@ -350,30 +313,6 @@ func onIntelNetwork() bool {
return true
}

// 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 checkForUpdates(version string) (bool, manifest, error) {
latestManifest, err := getLatestManifest()
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ func (rc *ReportingCommand) Run() error {
slog.Error("error sending signal to children", slog.String("error", err.Error()))
}
}()
// create output directory
err := util.CreateDirectoryIfNotExists(outputDir, 0755) // #nosec G301
if err != nil {
err = fmt.Errorf("failed to create output directory: %w", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
slog.Error(err.Error())
rc.Cmd.SilenceUsage = true
return err
}

var orderedTargetScriptOutputs []TargetScriptOutputs
var myTargets []target.Target
Expand Down Expand Up @@ -206,7 +215,7 @@ func (rc *ReportingCommand) Run() error {
}
// 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