Make new
output an error if the directory isn't empty (#1810)
This allows us to get rid of the `mkdir <dir> && cd <dir>` instructions in all our tutorials before `pulumi new`, because anyone who runs `pulumi new` in a non-empty directory will be forced to create a new directory in order to proceed.
This commit is contained in:
parent
a0cf415179
commit
15aca4e9d8
23
cmd/new.go
23
cmd/new.go
|
@ -16,6 +16,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
@ -92,6 +93,13 @@ func newNewCmd() *cobra.Command {
|
|||
}
|
||||
}
|
||||
|
||||
// Return an error if the directory isn't empty.
|
||||
if !force {
|
||||
if err = errorIfNotEmptyDirectory(cwd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// If we're going to be creating a stack, get the current backend, which
|
||||
// will kick off the login flow (if not already logged-in).
|
||||
var b backend.Backend
|
||||
|
@ -337,6 +345,21 @@ func newNewCmd() *cobra.Command {
|
|||
return cmd
|
||||
}
|
||||
|
||||
// errorIfNotEmptyDirectory returns an error if path is not empty.
|
||||
func errorIfNotEmptyDirectory(path string) error {
|
||||
infos, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(infos) > 0 {
|
||||
return errors.Errorf("%s is not empty; "+
|
||||
"rerun in an empty directory, pass the path to an empty directory to --dir, or use --force", path)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDevStackName returns the stack name suffixed with -dev.
|
||||
func getDevStackName(name string) string {
|
||||
const suffix = "-dev"
|
||||
|
|
Loading…
Reference in a new issue