Skip to content

Commit

Permalink
Apply default values before we unmarshal the config, allows not using…
Browse files Browse the repository at this point in the history
… pointers (#11596)

Skip changelog since this is not visible to our users.

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 4, 2024
1 parent bfa1dc5 commit 27bcaf6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 152 deletions.
6 changes: 3 additions & 3 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Distribution struct {
Name string `mapstructure:"name"`
Go string `mapstructure:"go"`
Description string `mapstructure:"description"`
// Deprecated: only here
// Deprecated: [v0.113.0] only here to return a detailed error and not failing during unmarshalling.
OtelColVersion string `mapstructure:"otelcol_version"`
OutputPath string `mapstructure:"output_path"`
Version string `mapstructure:"version"`
Expand All @@ -83,7 +83,7 @@ type retry struct {
}

// NewDefaultConfig creates a new config, with default values
func NewDefaultConfig() Config {
func NewDefaultConfig() *Config {
log, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("failed to obtain a logger instance: %v", err))
Expand All @@ -94,7 +94,7 @@ func NewDefaultConfig() Config {
log.Error("failed to obtain a temporary directory", zap.Error(err))
}

return Config{
return &Config{
OtelColVersion: defaultOtelColVersion,
Logger: log,
Distribution: Distribution{
Expand Down
16 changes: 8 additions & 8 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

const otelcolPath = "go.opentelemetry.io/collector/otelcol"

func runGoCommand(cfg Config, args ...string) ([]byte, error) {
func runGoCommand(cfg *Config, args ...string) ([]byte, error) {
if cfg.Verbose {
cfg.Logger.Info("Running go subcommand.", zap.Any("arguments", args))
}
Expand All @@ -56,7 +56,7 @@ func runGoCommand(cfg Config, args ...string) ([]byte, error) {
}

// GenerateAndCompile will generate the source files based on the given configuration, update go mod, and will compile into a binary
func GenerateAndCompile(cfg Config) error {
func GenerateAndCompile(cfg *Config) error {
if err := Generate(cfg); err != nil {
return err
}
Expand All @@ -70,7 +70,7 @@ func GenerateAndCompile(cfg Config) error {
}

// Generate assembles a new distribution based on the given configuration
func Generate(cfg Config) error {
func Generate(cfg *Config) error {
if cfg.SkipGenerate {
cfg.Logger.Info("Skipping generating source codes.")
return nil
Expand Down Expand Up @@ -101,7 +101,7 @@ func Generate(cfg Config) error {
}

// Compile generates a binary from the sources based on the configuration
func Compile(cfg Config) error {
func Compile(cfg *Config) error {
if cfg.SkipCompilation {
cfg.Logger.Info("Generating source codes only, the distribution will not be compiled.")
return nil
Expand Down Expand Up @@ -132,7 +132,7 @@ func Compile(cfg Config) error {
}

// GetModules retrieves the go modules, updating go.mod and go.sum in the process
func GetModules(cfg Config) error {
func GetModules(cfg *Config) error {
if cfg.SkipGetModules {
cfg.Logger.Info("Generating source codes only, will not update go.mod and retrieve Go modules.")
return nil
Expand Down Expand Up @@ -185,7 +185,7 @@ func GetModules(cfg Config) error {
return downloadModules(cfg)
}

func downloadModules(cfg Config) error {
func downloadModules(cfg *Config) error {
cfg.Logger.Info("Getting go modules")
failReason := "unknown"
for i := 1; i <= cfg.downloadModules.numRetries; i++ {
Expand All @@ -200,7 +200,7 @@ func downloadModules(cfg Config) error {
return fmt.Errorf("%w: %s", errDownloadFailed, failReason)
}

func processAndWrite(cfg Config, tmpl *template.Template, outFile string, tmplParams any) error {
func processAndWrite(cfg *Config, tmpl *template.Template, outFile string, tmplParams any) error {
out, err := os.Create(filepath.Clean(filepath.Join(cfg.Distribution.OutputPath, outFile)))
if err != nil {
return err
Expand All @@ -217,7 +217,7 @@ func (c *Config) allComponents() []Module {

func (c *Config) readGoModFile() (string, map[string]string, error) {
var modPath string
stdout, err := runGoCommand(*c, "mod", "edit", "-print")
stdout, err := runGoCommand(c, "mod", "edit", "-print")
if err != nil {
return modPath, nil, err
}
Expand Down
30 changes: 15 additions & 15 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ var (
}
)

func newTestConfig() Config {
func newTestConfig() *Config {
cfg := NewDefaultConfig()
cfg.downloadModules.wait = 0
cfg.downloadModules.numRetries = 1
return cfg
}

func newInitializedConfig(t *testing.T) Config {
func newInitializedConfig(t *testing.T) *Config {
cfg := newTestConfig()
// Validate and ParseModules will be called before the config is
// given to Generate.
Expand All @@ -137,12 +137,12 @@ func TestVersioning(t *testing.T) {
replaces := generateReplaces()
tests := []struct {
name string
cfgBuilder func() Config
cfgBuilder func() *Config
expectedErr error
}{
{
name: "defaults",
cfgBuilder: func() Config {
cfgBuilder: func() *Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -152,7 +152,7 @@ func TestVersioning(t *testing.T) {
},
{
name: "only gomod file, skip generate",
cfgBuilder: func() Config {
cfgBuilder: func() *Config {
cfg := newTestConfig()
tempDir := t.TempDir()
err := makeModule(tempDir, []byte(goModTestFile))
Expand All @@ -166,7 +166,7 @@ func TestVersioning(t *testing.T) {
},
{
name: "old component version",
cfgBuilder: func() Config {
cfgBuilder: func() *Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
cfg.Exporters = []Module{
Expand All @@ -182,7 +182,7 @@ func TestVersioning(t *testing.T) {
},
{
name: "old component version without strict mode",
cfgBuilder: func() Config {
cfgBuilder: func() *Config {
cfg := newTestConfig()
cfg.Distribution.Go = "go"
cfg.SkipStrictVersioning = true
Expand Down Expand Up @@ -228,11 +228,11 @@ func TestGenerateAndCompile(t *testing.T) {
replaces := generateReplaces()
testCases := []struct {
name string
cfgBuilder func(t *testing.T) Config
cfgBuilder func(t *testing.T) *Config
}{
{
name: "Default Configuration Compilation",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -241,7 +241,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "LDFlags Compilation",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -251,7 +251,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "Build Tags Compilation",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -261,7 +261,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "Debug Compilation",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -272,7 +272,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "No providers",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -282,7 +282,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "With confmap factories",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
Expand All @@ -292,7 +292,7 @@ func TestGenerateAndCompile(t *testing.T) {
},
{
name: "ConfResolverDefaultURIScheme set",
cfgBuilder: func(t *testing.T) Config {
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig()
cfg.ConfResolver = ConfResolver{
DefaultURIScheme: "env",
Expand Down
Loading

0 comments on commit 27bcaf6

Please sign in to comment.