Skip to content

Commit

Permalink
Small fixes for relative paths
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Sep 16, 2020
1 parent 991ec3b commit 1f8c7ad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
14 changes: 9 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"github.com/observatorium/opentelemetry-collector-builder/internal/builder"
)

var cfgFile string
var cfg = builder.DefaultConfig()

// Execute is the main entrypoint for this application
func Execute() {
var cfgFile string
cobra.OnInitialize(initConfig)

cfg := builder.DefaultConfig()
cmd := &cobra.Command{
Use: "otelcol-builder",
Long: "OpenTelemetry Collector distribution builder",
Expand All @@ -47,7 +49,7 @@ func Execute() {
}

// the external config file
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otelcol-builder.yaml)")
cmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otelcol-builder.yaml)")

// the distribution parameters, which we accept as CLI flags as well
cmd.Flags().StringVar(&cfg.Distribution.ExeName, "name", "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution")
Expand All @@ -60,6 +62,10 @@ func Execute() {
// tie Viper to flags
viper.BindPFlags(cmd.Flags())

cmd.Execute()
}

func initConfig() {
// a couple of Viper goodies, to make it easier to use env vars when flags are not desirable
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
Expand All @@ -82,6 +88,4 @@ func Execute() {
cfg.Logger.Error(err, "failed to parse the config")
return
}

cmd.Execute()
}
1 change: 1 addition & 0 deletions internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Module struct {
Name string `mapstructure:"name"` // if not specified, this is package part of the go mod (last part of the path)
Import string `mapstructure:"import"` // if not specified, this is the path part of the go mods
GoMod string `mapstructure:"gomod"` // a gomod-compatible spec for the module
Path string `mapstructure:"path"` // an optional path to the local version of this module
}

// DefaultConfig creates a new config, with default values
Expand Down
22 changes: 9 additions & 13 deletions internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ func GenerateAndCompile(cfg Config) error {
// Generate assembles a new distribution based on the given configuration
func Generate(cfg Config) error {
// if the file does not exist, try to create it
_, err := os.Stat(cfg.Distribution.OutputPath)
if os.IsNotExist(err) {
if err := os.Mkdir(cfg.Distribution.OutputPath, 0644); err != nil {
return err
if _, err := os.Stat(cfg.Distribution.OutputPath); os.IsNotExist(err) {
if err := os.Mkdir(cfg.Distribution.OutputPath, 0755); err != nil {
return fmt.Errorf("failed to create output path: %w", err)
}
}

// something else happened
if err != nil {
return err
} else if err != nil {
// something else happened
return fmt.Errorf("failed to create output path: %w", err)
}

for _, file := range []struct {
Expand All @@ -76,7 +73,7 @@ func Generate(cfg Config) error {
},
} {
if err := processAndWrite(cfg, file.tmpl, file.outFile, cfg); err != nil {
return fmt.Errorf("failed: destination: %q, source: %q: %w", file.outFile, file.tmpl, err)
return fmt.Errorf("failed to generate source file with destination %q, source: %q: %w", file.outFile, file.tmpl, err)
}
}

Expand All @@ -92,13 +89,12 @@ func Compile(cfg Config) error {
}

cfg.Logger.Info("Compiling")
dest := fmt.Sprintf("%s/%s", cfg.Distribution.OutputPath, cfg.Distribution.ExeName)
cmd := exec.Command(cfg.Distribution.Go, "build", "-trimpath", "-o", dest, cfg.Distribution.OutputPath)
cmd := exec.Command(cfg.Distribution.Go, "build", "-trimpath", "-o", cfg.Distribution.ExeName)
cmd.Dir = cfg.Distribution.OutputPath
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w. Output: %q", err, out)
}
cfg.Logger.Info("Compiled", "binary", dest)
cfg.Logger.Info("Compiled", "binary", fmt.Sprintf("%s/%s", cfg.Distribution.OutputPath, cfg.Distribution.ExeName))

return nil
}
Expand Down
13 changes: 13 additions & 0 deletions internal/scaffold/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ require (
{{- end}}
go.opentelemetry.io/collector v0.9.0
)
{{- range .Extensions}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
{{- range .Receivers}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
{{- range .Exporters}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
{{- range .Processors}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
`

0 comments on commit 1f8c7ad

Please sign in to comment.