Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Sakshi Patle <[email protected]>
  • Loading branch information
Sakshi Patle authored and Sakshi Patle committed Oct 20, 2022
1 parent e150cfd commit d63f40b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
13 changes: 8 additions & 5 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ func NewDefaultConfig() Config {
}
}

// ValidateAndSetGoPath checks whether the current configuration is valid and sets go path
func (c *Config) ValidateAndSetGoPath() error {
// Validate checks whether the current configuration is valid
func (c *Config) Validate() error {
return multierr.Combine(validateModules(c.Extensions), validateModules(c.Receivers), validateModules(c.Exporters), validateModules(c.Processors))
}

// SetGoPath sets go path
func (c *Config) SetGoPath() error {
if !c.SkipCompilation || !c.SkipGetModules {
// #nosec G204
if _, err := exec.Command(c.Distribution.Go, "env").CombinedOutput(); err != nil {
Expand All @@ -99,11 +104,9 @@ func (c *Config) ValidateAndSetGoPath() error {
}
c.Distribution.Go = path
}

c.Logger.Info("Using go", zap.String("go-executable", c.Distribution.Go))
}

return multierr.Combine(validateModules(c.Extensions), validateModules(c.Receivers), validateModules(c.Exporters), validateModules(c.Processors))
return nil
}

// ParseModules will parse the Modules entries and populate the missing values
Expand Down
15 changes: 10 additions & 5 deletions cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ func TestInvalidModule(t *testing.T) {
}

for _, test := range configurations {
assert.True(t, errors.Is(test.cfg.ValidateAndSetGoPath(), test.err))
assert.True(t, errors.Is(test.cfg.SetGoPath(), test.err))
assert.True(t, errors.Is(test.cfg.Validate(), test.err))
}
}

func TestNewDefaultConfig(t *testing.T) {
cfg := NewDefaultConfig()
require.NoError(t, cfg.ParseModules())
require.NoError(t, cfg.ValidateAndSetGoPath())
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
}

func TestNewBuiltinConfig(t *testing.T) {
Expand All @@ -155,7 +157,8 @@ func TestNewBuiltinConfig(t *testing.T) {

require.NoError(t, k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "mapstructure"}))
assert.NoError(t, cfg.ParseModules())
assert.NoError(t, cfg.ValidateAndSetGoPath())
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())

// Unlike the config initialized in NewDefaultConfig(), we expect
// the builtin default to be practically useful, so there must be
Expand All @@ -174,14 +177,16 @@ func TestSkipGoValidation(t *testing.T) {
SkipCompilation: true,
SkipGetModules: true,
}
assert.NoError(t, cfg.ValidateAndSetGoPath())
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
}

func TestSkipGoInitialization(t *testing.T) {
cfg := Config{
SkipCompilation: true,
SkipGetModules: true,
}
assert.NoError(t, cfg.ValidateAndSetGoPath())
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
assert.Zero(t, cfg.Distribution.Go)
}
3 changes: 2 additions & 1 deletion cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestGenerateAndCompileDefault(t *testing.T) {
// we override this version, otherwise this would break during releases
cfg.Distribution.OtelColVersion = "0.52.0"

assert.NoError(t, cfg.ValidateAndSetGoPath())
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
require.NoError(t, GenerateAndCompile(cfg))

// Sleep for 1 second to make sure all processes using the files are completed
Expand Down
6 changes: 5 additions & 1 deletion cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ configuration is provided, ocb will generate a default Collector.
if err := initConfig(cmd.Flags()); err != nil {
return err
}
if err := cfg.ValidateAndSetGoPath(); err != nil {
if err := cfg.Validate(); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}

if err := cfg.SetGoPath(); err != nil {
return fmt.Errorf("go not found: %w", err)
}

if err := cfg.ParseModules(); err != nil {
return fmt.Errorf("invalid module configuration: %w", err)
}
Expand Down

0 comments on commit d63f40b

Please sign in to comment.