From a4d0c4f0132edb290e23deab6ec6454b12e30437 Mon Sep 17 00:00:00 2001 From: Dmytro Bondar Date: Mon, 21 Oct 2024 10:05:54 +0200 Subject: [PATCH] Add possibility to specify required Pulumi version --- internal/cli/run.go | 2 +- internal/config/config.go | 15 +++++++++++---- internal/pulumi/pulumi.go | 18 +++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/internal/cli/run.go b/internal/cli/run.go index 2ca7795..84ec0da 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -12,7 +12,7 @@ import ( // Run runs pulumi command with the given arguments, attaches stdout/stderr, and exits the main program on failure. func (c *CLI) Run(args []string) error { // init pulumi cli - pulumiCli, err := pulumi.NewCLI(c.ctx) + pulumiCli, err := pulumi.NewCLI(c.ctx, c.cfg.PulumiVersion) if err != nil { return err } diff --git a/internal/config/config.go b/internal/config/config.go index 790062a..5af3810 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -9,10 +9,11 @@ import ( ) type Config struct { - Changed bool - Reversed bool - Sorted bool - Since string + Changed bool + Reversed bool + Sorted bool + Since string + PulumiVersion string `mapstructure:"pulumi-version"` } // NewConfig reads in config file and ENV variables if set. @@ -31,6 +32,7 @@ func NewConfig(cfgFile string) (*Config, error) { viper.AutomaticEnv() viper.SetEnvPrefix("pulumer") viper.EnvKeyReplacer(strings.NewReplacer("-", "_")) + setDefaults() // If a config file is found, read it in. if err := viper.ReadInConfig(); err != nil { @@ -48,3 +50,8 @@ func NewConfig(cfgFile string) (*Config, error) { return cfg, nil } + +func setDefaults() { + // Minimum required version of pulumi cli. + viper.SetDefault("pulumi-version", "3.135.0") +} diff --git a/internal/pulumi/pulumi.go b/internal/pulumi/pulumi.go index 91141d3..4d910b8 100644 --- a/internal/pulumi/pulumi.go +++ b/internal/pulumi/pulumi.go @@ -6,6 +6,7 @@ import ( "io" "os" + "github.com/blang/semver" "github.com/pulumi/pulumi/sdk/v3/go/auto" ) @@ -14,18 +15,25 @@ type CLI struct { cmd auto.PulumiCommand } -func NewCLI(ctx context.Context) (*CLI, error) { - cmd, err := getPulumi(ctx) +func NewCLI(ctx context.Context, version string) (*CLI, error) { + requiredVersion, err := semver.Parse(version) + if err != nil { + return nil, fmt.Errorf("invalid version: %w", err) + } + + cmd, err := getPulumi(ctx, requiredVersion) if err != nil { return nil, err } return &CLI{cmd: cmd, ctx: ctx}, nil } -func getPulumi(ctx context.Context) (auto.PulumiCommand, error) { - cmd, err := auto.NewPulumiCommand(&auto.PulumiCommandOptions{}) +func getPulumi(ctx context.Context, version semver.Version) (auto.PulumiCommand, error) { + cmd, err := auto.NewPulumiCommand(&auto.PulumiCommandOptions{ + Version: version, + }) if err != nil { - fmt.Fprintln(os.Stderr, "pulumi cli not found, installing...") + fmt.Fprintln(os.Stderr, "pulumi cli with required version not found in PATH, installing...") cmd, err = auto.InstallPulumiCommand(ctx, &auto.PulumiCommandOptions{}) if err != nil { return nil, fmt.Errorf("unable to install pulumi cli: %w", err)