Skip to content

Commit

Permalink
Add possibility to specify required Pulumi version
Browse files Browse the repository at this point in the history
  • Loading branch information
bonddim committed Oct 21, 2024
1 parent 8cce2d8 commit a4d0c4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 11 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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")
}
18 changes: 13 additions & 5 deletions internal/pulumi/pulumi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"

"github.com/blang/semver"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
)

Expand All @@ -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)
Expand Down

0 comments on commit a4d0c4f

Please sign in to comment.