diff --git a/cli/analyze.go b/cli/analyze.go index 169a331..fb40ece 100644 --- a/cli/analyze.go +++ b/cli/analyze.go @@ -2,7 +2,6 @@ package cli import ( "fmt" - "log" "time" "github.com/bubunyo/buildgraph/pkg/diff" @@ -21,7 +20,7 @@ and outputs which services are affected by the detected changes.`, } func init() { - analyzeCmd.Flags().StringP("format", "f", "json", "Output format: json, text") + analyzeCmd.Flags().StringP("format", "f", "text", "Output format: text, json") analyzeCmd.Flags().StringP("output", "o", "", "Output file (default: stdout)") analyzeCmd.Flags().BoolP("verbose", "v", false, "Include debug info in output") analyzeCmd.Flags().Bool("no-cache", false, "Ignore baseline, treat everything as new") @@ -41,8 +40,6 @@ func runAnalyze(cmd *cobra.Command, _ []string) error { return fmt.Errorf("detecting root module: %w", err) } - log.Printf("Analyzing project: %s", rootModule) - // Load baseline before parsing so unchanged functions can reuse stored hashes. var previousBaseline *types.Baseline noCache, _ := cmd.Flags().GetBool("no-cache") @@ -91,11 +88,5 @@ func runAnalyze(cmd *cobra.Command, _ []string) error { output, _ := cmd.Flags().GetString("output") writeOutput(result, format, output) - if len(changes) > 0 { - log.Printf("Changes detected: %d", len(changes)) - log.Printf("Services to build: %v", impactResult.ServicesToBuild) - } else { - log.Printf("No changes detected") - } return nil } diff --git a/cli/output.go b/cli/output.go index d9dfe94..1a7fa4f 100644 --- a/cli/output.go +++ b/cli/output.go @@ -3,7 +3,6 @@ package cli import ( "encoding/json" "fmt" - "log" "os" "sort" "strings" @@ -22,13 +21,15 @@ func writeOutput(result *types.Result, format, outputPath string) { var marshalErr error output, marshalErr = json.MarshalIndent(result, "", " ") if marshalErr != nil { - log.Fatalf("failed to marshal result to JSON: %v", marshalErr) + fmt.Fprintf(os.Stderr, "failed to marshal result to JSON: %v\n", marshalErr) + os.Exit(1) } } if outputPath != "" { if err := os.WriteFile(outputPath, output, 0644); err != nil { - log.Fatalf("failed to write output: %v", err) + fmt.Fprintf(os.Stderr, "failed to write output: %v\n", err) + os.Exit(1) } return } @@ -45,7 +46,7 @@ func formatText(result *types.Result) string { if len(result.Changes) > 0 { fmt.Fprintf(sb, "Changes (%d):\n", len(result.Changes)) for _, c := range result.Changes { - fmt.Fprintf(sb, " [%-20s] %s\n", c.Type, c.Function) + fmt.Fprintf(sb, " [%s] %s\n", c.Type, c.Function) if c.Reason != "" { fmt.Fprintf(sb, " reason : %s\n", c.Reason) } diff --git a/cli/pipeline.go b/cli/pipeline.go index c53a040..b87dd8c 100644 --- a/cli/pipeline.go +++ b/cli/pipeline.go @@ -2,7 +2,6 @@ package cli import ( "fmt" - "log" "os" "os/exec" "path/filepath" @@ -30,12 +29,12 @@ func parseProject( ) { a := analyzer.New(cfg, rootModule, rootPath) - log.Printf("Loading packages…") + fmt.Fprintln(os.Stderr, "Loading packages…") if err = a.Load(); err != nil { return } - log.Printf("Building call graph…") + fmt.Fprintln(os.Stderr, "Building call graph…") functions, graph, err = a.BuildGraph() if err != nil { return @@ -48,14 +47,14 @@ func parseProject( prevFuncHashes = prevBaseline.FunctionHashes } - log.Printf("Computing hashes for %d functions…", len(functions)) + fmt.Fprintf(os.Stderr, "Computing hashes for %d functions…\n", len(functions)) if err = a.ComputeHashes(functions, prevSrcHashes, prevFuncHashes); err != nil { return } extDeps, extHash, err = a.ExtractExternalDeps() if err != nil { - log.Printf("Warning: could not extract external deps: %v", err) + fmt.Fprintf(os.Stderr, "Warning: could not extract external deps: %v\n", err) extDeps = map[string]string{} extHash = "" err = nil //nolint:ineffassign @@ -63,7 +62,7 @@ func parseProject( sourceHashes, err = a.ComputeSourceHashes() if err != nil { - log.Printf("Warning: could not compute source hashes: %v", err) + fmt.Fprintf(os.Stderr, "Warning: could not compute source hashes: %v\n", err) sourceHashes = map[string]string{} err = nil //nolint:ineffassign } diff --git a/cli/root.go b/cli/root.go index 8c5ea90..739e4b0 100644 --- a/cli/root.go +++ b/cli/root.go @@ -4,7 +4,7 @@ package cli import ( "errors" - "log" + "fmt" "os" "strings" @@ -71,7 +71,8 @@ func initConfig() { // errors.Is(err, os.ErrNotExist) to correctly catch the missing-file // case and only fatal on genuine read errors (bad permissions, etc.). if !errors.Is(err, os.ErrNotExist) { - log.Fatalf("error reading config file %s: %v", cfgFile, err) + fmt.Fprintf(os.Stderr, "error reading config file %s: %v\n", cfgFile, err) + os.Exit(1) } } } @@ -80,7 +81,8 @@ func initConfig() { func loadConfig() *config.Config { cfg := config.Default() if err := viper.Unmarshal(cfg); err != nil { - log.Fatalf("invalid configuration: %v", err) + fmt.Fprintf(os.Stderr, "invalid configuration: %v\n", err) + os.Exit(1) } if len(cfg.Services) == 0 { cfg.Services = config.Default().Services diff --git a/pkg/impact/impact.go b/pkg/impact/impact.go index db72af8..718595c 100644 --- a/pkg/impact/impact.go +++ b/pkg/impact/impact.go @@ -27,7 +27,6 @@ func (a *Analyzer) ComputeImpact(changes []types.Change) types.Impact { AffectedFunctions: make(map[string][]string), AffectReasons: make(map[string][]string), ServicesToBuild: []string{}, - Changes: changes, } // Get initial changed functions diff --git a/pkg/types/types.go b/pkg/types/types.go index 36ead50..56d6675 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -70,10 +70,9 @@ type Change struct { } type Impact struct { - AffectedFunctions map[string][]string `json:"affected_functions"` - AffectReasons map[string][]string `json:"affect_reasons"` + AffectedFunctions map[string][]string `json:"-"` + AffectReasons map[string][]string `json:"-"` ServicesToBuild []string `json:"services_to_build"` - Changes []Change `json:"changes"` } type DebugInfo struct {