From 6e7cffd60bfb7f67ed4635c2374e9e21da3e1a6e Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 9 Oct 2014 17:16:17 -0700 Subject: [PATCH] command/init: Only initialize a blank state with remote --- command/init.go | 39 +++++++++++++++++++++++---------------- command/remote.go | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/command/init.go b/command/init.go index d05a0b143..7ed54d4e0 100644 --- a/command/init.go +++ b/command/init.go @@ -52,14 +52,6 @@ func (c *InitCommand) Run(args []string) int { } } - // Validate the remote configuration - if !remoteConf.Empty() { - if err := remote.ValidateConfig(&remoteConf); err != nil { - c.Ui.Error(fmt.Sprintf("%s", err)) - return 1 - } - } - source := args[0] // Get our pwd since we need it @@ -99,20 +91,35 @@ func (c *InitCommand) Run(args []string) int { // Handle remote state if configured if !remoteConf.Empty() { - change, err := remote.RefreshState(&remoteConf) + // Ensure remote state is not already enabled + haveLocal, err := remote.HaveLocalState() if err != nil { - c.Ui.Error(fmt.Sprintf( - "Failed to refresh from remote state: %v", err)) + c.Ui.Error(fmt.Sprintf("Failed to check for local state: %v", err)) + return 1 + } + if haveLocal { + c.Ui.Error("Remote state is already enabled. Aborting.") return 1 } - // Log the change that took place - c.Ui.Output(fmt.Sprintf("%s", change)) - - // Use an error exit code if the update was not a success - if !change.SuccessfulPull() { + // Check if we have the non-managed state file + haveNonManaged, err := remote.ExistsFile(DefaultStateFilename) + if err != nil { + c.Ui.Error(fmt.Sprintf("Failed to check for state file: %v", err)) return 1 } + if haveNonManaged { + c.Ui.Error(fmt.Sprintf("Existing state file '%s' found. Aborting.", + DefaultStateFilename)) + return 1 + } + + // Initialize a blank state file with remote enabled + remoteCmd := &RemoteCommand{ + Meta: c.Meta, + remoteConf: remoteConf, + } + return remoteCmd.initBlankState() } return 0 } diff --git a/command/remote.go b/command/remote.go index 1faa1877c..1ccb90cfd 100644 --- a/command/remote.go +++ b/command/remote.go @@ -35,7 +35,7 @@ func (c *RemoteCommand) Run(args []string) int { cmdFlags := flag.NewFlagSet("remote", flag.ContinueOnError) cmdFlags.BoolVar(&c.conf.disableRemote, "disable", false, "") cmdFlags.BoolVar(&c.conf.pullOnDisable, "pull", true, "") - cmdFlags.StringVar(&c.conf.statePath, "state", "", "path") + cmdFlags.StringVar(&c.conf.statePath, "state", DefaultStateFilename, "path") cmdFlags.StringVar(&c.conf.backupPath, "backup", "", "path") cmdFlags.StringVar(&c.remoteConf.AuthToken, "auth", "", "") cmdFlags.StringVar(&c.remoteConf.Name, "name", "", "")