Have saveEnv always override an existing environment

`saveEnv` had a flag which would prevent an environment from being
overwritten if it already existed, which was only used by `lumi env
init`. Refactor the code so the check is done inside `lumi` instead of
against this API. We don't need this functionality for the service and
so requiring support for this at the API boundary for environments
feels like a bad idea.
This commit is contained in:
Matt Ellis 2017-08-29 18:05:42 -07:00
parent 871b8ba962
commit a4c97d7225
5 changed files with 15 additions and 14 deletions

View file

@ -4,6 +4,11 @@ package main
import (
"errors"
"fmt"
"os"
"github.com/pulumi/pulumi-fabric/pkg/tokens"
"github.com/pulumi/pulumi-fabric/pkg/workspace"
"github.com/spf13/cobra"
@ -25,6 +30,10 @@ func newEnvInitCmd() *cobra.Command {
return errors.New("missing required environment name")
}
if _, staterr := os.Stat(workspace.EnvPath(tokens.QName(args[0]))); staterr == nil {
return fmt.Errorf("environment '%v' already exists", args[0])
}
return lumiEngine.InitEnv(args[0])
}),
}

View file

@ -15,7 +15,7 @@ func (eng *Engine) DeleteConfig(envName string, key string) error {
if config != nil {
delete(config, tokens.Token(key))
if !eng.saveEnv(info.Target, info.Snapshot, true) {
if !eng.saveEnv(info.Target, info.Snapshot) {
return errors.Errorf("could not save configuration value")
}
}

View file

@ -20,7 +20,7 @@ func (eng *Engine) SetConfig(envName string, key string, value string) error {
config[tokens.Token(key)] = value
if !eng.saveEnv(info.Target, info.Snapshot, true) {
if !eng.saveEnv(info.Target, info.Snapshot) {
return errors.Errorf("could not save configuration value")
}
@ -43,7 +43,7 @@ func (eng *Engine) ReplaceConfig(envName string, newConfig map[string]string) er
info.Target.Config = config
if !eng.saveEnv(info.Target, info.Snapshot, true) {
if !eng.saveEnv(info.Target, info.Snapshot) {
return errors.Errorf("could not save configuration value")
}

View file

@ -104,7 +104,7 @@ func (eng *Engine) deployLatest(info *envCmdInfo, opts deployOptions) error {
// Now save the updated snapshot Notee that if a failure has occurred, the Apply routine above will
// have returned a safe checkpoint.
targ := result.Info.Target
eng.saveEnv(targ, summary.Snap(), true /*overwrite*/)
eng.saveEnv(targ, summary.Snap())
fmt.Fprint(eng.Stdout, colors.Colorize(&footer))
return err

View file

@ -61,7 +61,7 @@ type envCmdInfo struct {
// createEnv just creates a new empty environment without deploying anything into it.
func (eng *Engine) createEnv(name tokens.QName) {
env := &deploy.Target{Name: name}
if success := eng.saveEnv(env, nil, false); success {
if success := eng.saveEnv(env, nil); success {
fmt.Fprintf(eng.Stdout, "Environment '%v' initialized; see `lumi deploy` to deploy into it\n", name)
eng.setCurrentEnv(name, false)
}
@ -190,7 +190,7 @@ func (eng *Engine) readEnv(name tokens.QName) (*deploy.Target, *deploy.Snapshot,
}
// saveEnv saves a new snapshot at the given location, backing up any existing ones.
func (eng *Engine) saveEnv(env *deploy.Target, snap *deploy.Snapshot, existok bool) bool {
func (eng *Engine) saveEnv(env *deploy.Target, snap *deploy.Snapshot) bool {
contract.Require(env != nil, "env")
file := workspace.EnvPath(env.Name)
@ -210,14 +210,6 @@ func (eng *Engine) saveEnv(env *deploy.Target, snap *deploy.Snapshot, existok bo
return false
}
// If it's not ok for the file to already exist, ensure that it doesn't.
if !existok {
if _, staterr := os.Stat(file); staterr == nil {
glog.Errorf("An IO error occurred during the current operation: %v", goerr.Errorf("file '%v' already exists", file))
return false
}
}
// Back up the existing file if it already exists.
backupTarget(file)