From 1f8c7adb627893e4f5a3f6d24ef2c324a60ab3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Wed, 16 Sep 2020 11:59:54 +0200 Subject: [PATCH] Small fixes for relative paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juraci Paixão Kröhling --- cmd/root.go | 14 +++++++++----- internal/builder/config.go | 1 + internal/builder/main.go | 22 +++++++++------------- internal/scaffold/gomod.go | 13 +++++++++++++ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 8119f35..5b11018 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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", @@ -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") @@ -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() @@ -82,6 +88,4 @@ func Execute() { cfg.Logger.Error(err, "failed to parse the config") return } - - cmd.Execute() } diff --git a/internal/builder/config.go b/internal/builder/config.go index 2c7b8ee..75b9c93 100644 --- a/internal/builder/config.go +++ b/internal/builder/config.go @@ -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 diff --git a/internal/builder/main.go b/internal/builder/main.go index 4574b58..82708a9 100644 --- a/internal/builder/main.go +++ b/internal/builder/main.go @@ -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 { @@ -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) } } @@ -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 } diff --git a/internal/scaffold/gomod.go b/internal/scaffold/gomod.go index 48fa1d5..e0326dd 100644 --- a/internal/scaffold/gomod.go +++ b/internal/scaffold/gomod.go @@ -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}} `