Skip to content

Commit

Permalink
Add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Oct 24, 2023
1 parent 5b4ca8e commit f8afa3d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .chloggen/mx-psi_logging-builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
19 changes: 14 additions & 5 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 35 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L31-L35

Added lines #L31 - L35 were not covered by tests
}

if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("go subcommand failed with args '%v': %w. Output:\n%s", args, err, out)
}

Check warning on line 40 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L39-L40

Added lines #L39 - L40 were not covered by tests

return nil
}

// GenerateAndCompile will generate the source files based on the given configuration, update go mod, and will compile into a binary
Expand Down
2 changes: 2 additions & 0 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
distributionOutputPathFlag = "output-path"
distributionGoFlag = "go"
distributionModuleFlag = "module"
verboseFlag = "verbose"
)

var (
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f8afa3d

Please sign in to comment.