-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters