8000 Warn on empty directory by Integralist · Pull Request #247 · fastly/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Warn on empty directory #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ COMMANDS
package backend
--backend-port=BACKEND-PORT
A port number for the package backend
--force Skip non-empty directory verification step
and force new project creation

compute build [<flags>]
Build a Compute@Edge package locally
Expand Down
30 changes: 29 additions & 1 deletion pkg/compute/compute_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestInit(t *testing.T) {
},
{
name: "with existing package manifest",
args: []string{"compute", "init"},
args: []string{"compute", "init", "--force"}, // --force will ignore that the directory isn't empty
configFile: config.File{
User: config.User{
Token: "123",
Expand Down Expand Up @@ -418,6 +418,34 @@ func TestInit(t *testing.T) {
"Updating package manifest...",
},
},
{
name: "non empty directory",
args: []string{"compute", "init"},
configFile: config.File{
User: config.User{
Token: "123",
Email: "test@example.com",
},
StarterKits: config.StarterKitLanguages{
Rust: []config.StarterKit{
{
Name: "Default",
Path: "https://github.com/fastly/compute-starter-kit-rust-default.git",
Branch: "0.6.0",
},
},
},
},
api: mock.API{
GetTokenSelfFn: tokenOK,
GetUserFn: getUserOk,
CreateServiceFn: createServiceOK,
CreateDomainFn: createDomainOK,
CreateBackendFn: createBackendOK,
},
wantError: "project directory not empty",
manifest: `name = "test"`, // causes a file to be created as part of test setup
},
{
name: "with default name inferred from directory",
args: []string{"compute", "init"},
Expand Down
73 changes: 63 additions & 10 deletions pkg/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ var (
// InitCommand initializes a Compute@Edge project package on the local machine.
type InitCommand struct {
common.Base
client api.HTTPClient
manifest manifest.Data
language string
from string
branch string
tag string
path string
domain string
backend string
backendPort uint
client api.HTTPClient
manifest manifest.Data
language string
from string
branch string
tag string
path string
domain string
backend string
backendPort uint
forceNonEmpty bool
}

// NewInitCommand returns a usable command registered under the parent.
Expand All @@ -73,6 +74,7 @@ func NewInitCommand(parent common.Registerer, client api.HTTPClient, globals *co
c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.domain)
c.CmdClause.Flag("backend", "A hostname, IPv4, or IPv6 address for the package backend").StringVar(&c.backend)
c.CmdClause.Flag("backend-port", "A port number for the package backend").UintVar(&c.backendPort)
c.CmdClause.Flag("force", "Skip non-empty directory verification step and force new project creation").BoolVar(&c.forceNonEmpty)

return &c
}
Expand All @@ -90,6 +92,18 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
text.Output(out, "Press ^C at any time to quit.")
text.Break(out)

cont, err := verifyDirectory(out, in)
if err != nil {
return err
}

if !c.forceNonEmpty && !cont {
return errors.RemediationError{
Inner: fmt.Errorf("project directory not empty"),
Remediation: errors.ExistingDirRemediation,
}
}

var progress text.Progress
if c.Globals.Verbose() {
progress = text.NewVerboseProgress(out)
Expand Down Expand Up @@ -491,6 +505,45 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
return nil
}

// verifyDirectory indicates if the user wants to continue with the execution
// flow when presented with a prompt that suggests the current directory isn't
// empty.
func verifyDirectory(out io.Writer, in io.Reader) (bool, error) {
files, err := os.ReadDir(".")
if err != nil {
return false, err
}

if len(files) > 0 {
dir, err := os.Getwd()
if err != nil {
return false, err
}

label := fmt.Sprintf("The current directory isn't empty. Are you sure you want to initialize a Compute@Edge project in %s? [y/n] ", dir)
cont, err := text.Input(out, label, in)
if err != nil {
return false, fmt.Errorf("error reading input %w", err)
}

contl := strings.ToLower(cont)

if contl == "n" || contl == "no" {
return false, nil
}

if contl == "y" || contl == "yes" {
return true, nil
}

// NOTE: be defensive and default to short-circuiting the execution flow if
// the input is unrecognised.
return false, nil
}

return true, nil
}

func verifyDestination(path string, verbose io.Writer) (abspath string, err error) {
abspath, err = filepath.Abs(path)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/errors/remediation_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ var BugRemediation = strings.Join([]string{
var ServiceIDRemediation = strings.Join([]string{
"Please provide one via the --service-id flag or within your package manifest",
}, " ")

// ExistingDirRemediation suggests moving to another directory and retrying.
var ExistingDirRemediation = strings.Join([]string{
"Please create a new directory and initialize a new project using:",
"`fastly compute init`.",
}, " ")
0