pulumi/cmd/util.go
Matt Ellis 93ab134bbb Have the CLI keep track of the current environment
Previously, the engine was concered with maintaing information about
the currently active environment. Now, the CLI is in charge of
this. As part of this change, the engine can now assume that every
environment has a non empty name (and I've added asserts on the
entrypoints of the engine API to ensure that any consumer of the
engine passes a non empty environment name)
2017-10-02 16:57:41 -07:00

56 lines
1.3 KiB
Go

package cmd
import (
"os"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/workspace"
)
// newWorkspace creates a new workspace using the current working directory.
func newWorkspace() (workspace.W, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
return workspace.New(pwd)
}
// explicitOrCurrent returns an environment name after ensuring the environment exists. When a empty
// environment name is passed, the "current" ambient environment is returned
func explicitOrCurrent(name string) (tokens.QName, error) {
if name == "" {
return getCurrentEnv()
}
_, _, _, err := lumiEngine.Environment.GetEnvironment(tokens.QName(name))
return tokens.QName(name), err
}
// getCurrentEnv reads the current environment.
func getCurrentEnv() (tokens.QName, error) {
w, err := newWorkspace()
if err != nil {
return tokens.QName(""), err
}
return w.Settings().Env, nil
}
// setCurrentEnv changes the current environment to the given environment name, issuing an error if it doesn't exist.
func setCurrentEnv(name tokens.QName, verify bool) error {
if verify {
if _, _, _, err := lumiEngine.Environment.GetEnvironment(name); err != nil {
return err
}
}
// Switch the current workspace to that environment.
w, err := newWorkspace()
if err != nil {
return err
}
w.Settings().Env = name
return w.Save()
}