From f8afa3d6e0f505cd28ab59d44f6845dcc81bbefe Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Tue, 24 Oct 2023 17:06:05 +0200 Subject: [PATCH] Add verbose flag --- .chloggen/mx-psi_logging-builder.yaml | 2 +- cmd/builder/internal/builder/config.go | 1 + cmd/builder/internal/builder/main.go | 19 ++++++++++++++----- cmd/builder/internal/command.go | 2 ++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.chloggen/mx-psi_logging-builder.yaml b/.chloggen/mx-psi_logging-builder.yaml index e4ac2fa2747..52cace4b9e7 100755 --- a/.chloggen/mx-psi_logging-builder.yaml +++ b/.chloggen/mx-psi_logging-builder.yaml @@ -7,7 +7,7 @@ change_type: enhancement component: cmd/builder # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "Add logging for `go` subcommands that are ran as part of a build" +note: "Add --verbose flag to log `go` subcommands output that are ran as part of a build" # One or more tracking issues or pull requests related to the change issues: [8715] diff --git a/cmd/builder/internal/builder/config.go b/cmd/builder/internal/builder/config.go index a67e23ca677..c889dbab68e 100644 --- a/cmd/builder/internal/builder/config.go +++ b/cmd/builder/internal/builder/config.go @@ -27,6 +27,7 @@ type Config struct { SkipCompilation bool `mapstructure:"-"` SkipGetModules bool `mapstructure:"-"` LDFlags string `mapstructure:"-"` + Verbose bool `mapstructure:"-"` Distribution Distribution `mapstructure:"dist"` Exporters []Module `mapstructure:"exporters"` diff --git a/cmd/builder/internal/builder/main.go b/cmd/builder/internal/builder/main.go index bf3be1f6ba0..ae212608d14 100644 --- a/cmd/builder/internal/builder/main.go +++ b/cmd/builder/internal/builder/main.go @@ -26,11 +26,20 @@ func runGoCommand(cfg Config, args ...string) error { // #nosec G204 -- cfg.Distribution.Go is trusted to be a safe path and the caller is assumed to have carried out necessary input validation cmd := exec.Command(cfg.Distribution.Go, args...) cmd.Dir = cfg.Distribution.OutputPath - writer := &zapio.Writer{Log: cfg.Logger} - defer func() { _ = writer.Close() }() - cmd.Stdout = writer - cmd.Stderr = writer - return cmd.Run() + + if cfg.Verbose { + writer := &zapio.Writer{Log: cfg.Logger} + defer func() { _ = writer.Close() }() + cmd.Stdout = writer + cmd.Stderr = writer + return cmd.Run() + } + + if out, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("go subcommand failed with args '%v': %w. Output:\n%s", args, err, out) + } + + return nil } // GenerateAndCompile will generate the source files based on the given configuration, update go mod, and will compile into a binary diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 35c086d3d54..873a82fd052 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -31,6 +31,7 @@ const ( distributionOutputPathFlag = "output-path" distributionGoFlag = "go" distributionModuleFlag = "module" + verboseFlag = "verbose" ) var ( @@ -78,6 +79,7 @@ configuration is provided, ocb will generate a default Collector. cmd.Flags().BoolVar(&cfg.SkipGenerate, skipGenerateFlag, false, "Whether builder should skip generating go code (default false)") cmd.Flags().BoolVar(&cfg.SkipCompilation, skipCompilationFlag, false, "Whether builder should only generate go code with no compile of the collector (default false)") cmd.Flags().BoolVar(&cfg.SkipGetModules, skipGetModulesFlag, false, "Whether builder should skip updating go.mod and retrieve Go module list (default false)") + cmd.Flags().BoolVar(&cfg.Verbose, verboseFlag, false, "Whether builder should print verbose output (default false)") cmd.Flags().StringVar(&cfg.LDFlags, ldflagsFlag, "", `ldflags to include in the "go build" command`) cmd.Flags().StringVar(&cfg.Distribution.Name, distributionNameFlag, "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution") if err := cmd.Flags().MarkDeprecated(distributionNameFlag, "use config distribution::name"); err != nil {