Skip to content

Commit

Permalink
#15 Added use command.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuschka committed Jul 28, 2020
1 parent 18f5e77 commit 44bf7d5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/commands/help_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Commands:
list List terraform versions.
install Install terraform version.
which Print selected terraform version.
use Change selected version in .terraform-version file.
uptodate Check for updates.
help Print this usage information.
version Print tfvm version.
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/tfvm_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func RunTfvmCommand(args []string) error {
} else if args[0] == "info" {
printInfo()
return nil
} else if args[0] == "use" {
return RunTfvmUseCommand(args[1:])
} else if args[0] == "version" {
printVersion()
return nil
Expand Down
65 changes: 65 additions & 0 deletions internal/commands/use_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package commands

import (
"errors"
inventoryPkg "github.com/cbuschka/tfvm/internal/inventory"
"github.com/cbuschka/tfvm/internal/util"
"github.com/cbuschka/tfvm/internal/version"
workspacePkg "github.com/cbuschka/tfvm/internal/workspace"
)

func RunTfvmUseCommand(args []string) error {
inventory, err := inventoryPkg.GetInventory()
if err != nil {
return err
}

err = inventory.Update()
if err != nil {
return err
}

versionSpec, err := getTfVersionSpecToUse(args)
if err != nil {
return err
}

_, err = inventory.GetTerraformRelease(versionSpec)
if err != nil {
if version.IsNoSuchTerraformRelease(err) {
util.Die(1, "No matching terraform version for %s.", versionSpec.String())
return err
}
return err
}

workspace, err := workspacePkg.GetWorkspace()
if err != nil {
return err
}

err = workspace.WriteTerraformVersionSelection(versionSpec.String())
if err != nil {
if workspacePkg.IsNoConfigExists(err) {
util.Die(1, "No .terraform-version found.")
}
return err
}

return nil
}

func getTfVersionSpecToUse(args []string) (*version.TerraformVersionSpec, error) {
if len(args) != 1 {
util.Die(1, "Version to use required.")
return nil, errors.New("unreachable code")
}

versionSpec, err := version.ParseTerraformVersionSpec(args[0])
if err != nil {
util.Die(1, "Invalid version '%s'.", args[0])
return nil, errors.New("unreachable code")
}

return versionSpec, nil
}
2 changes: 1 addition & 1 deletion internal/workspace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const noConfigExistsMsg = "config not exists"

func isNoConfigExists(err error) bool {
func IsNoConfigExists(err error) bool {
return err.Error() == noConfigExistsMsg
}

Expand Down
25 changes: 24 additions & 1 deletion internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/cbuschka/tfvm/internal/util"
"github.com/cbuschka/tfvm/internal/version"
"io/ioutil"
"os"
)

type Workspace struct {
Expand Down Expand Up @@ -51,6 +53,27 @@ func newNoTfVersionSelected() error {
return errors.New(noTfVersionSelectedMsg)
}

func (workspace *Workspace) WriteTerraformVersionSelection(tfVersionSelection string) error {
configFile, err := getNearestConfigFileFromCwd()
if err != nil {
if os.IsNotExist(err) {
return newNoConfigExists()
}

return err
}

err = ioutil.WriteFile(configFile, []byte(tfVersionSelection), 0644)
if err != nil {
return nil
}

util.Print("%s written.", configFile)

return nil

}

func (workspace *Workspace) GetTerraformVersionSelection() (*TerraformVersionSelection, error) {

tfVersionEnvVar := util.GetFirstEnv("TFVM_TERRAFORM_VERSION", "TERRAFORM_VERSION")
Expand All @@ -66,7 +89,7 @@ func (workspace *Workspace) GetTerraformVersionSelection() (*TerraformVersionSel

config, err := getConfiguration()
if err != nil {
if isNoConfigExists(err) {
if IsNoConfigExists(err) {
return nil, newNoTfVersionSelected()
}
return nil, err
Expand Down

0 comments on commit 44bf7d5

Please sign in to comment.