Skip to content
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

fix(compute/init): Init no longer fails if directory of same name as starter kit exists in current directory #1349

Merged
merged 7 commits into from
Nov 20, 2024
66 changes: 29 additions & 37 deletions pkg/commands/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
spinner.Message(msg + "...")
spinner.StopFailMessage(msg)
spinErr = spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
}
Expand Down Expand Up @@ -848,8 +847,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
if fi, err := os.Stat(c.CloneFrom); err == nil && fi.IsDir() {
if err := cp.Copy(c.CloneFrom, c.dir); err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -863,8 +861,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
u, err := url.Parse(c.CloneFrom)
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return fmt.Errorf("could not read --from URL: %w", err)
Expand All @@ -874,8 +871,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
// empty and the string ends up in u.Path.
if u.Host == "" && u.Scheme == "" {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return fmt.Errorf("--from url seems invalid: %s", c.CloneFrom)
Expand All @@ -889,8 +885,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
if gitRepositoryRegEx.MatchString(c.CloneFrom) {
if err := c.ClonePackageFromEndpoint(c.CloneFrom, branch, tag); err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -900,8 +895,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
}

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -925,8 +919,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
err = fmt.Errorf("failed to get package '%s': %w", req.URL.String(), err)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -937,14 +930,28 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
err := fmt.Errorf("failed to get package '%s': %s", req.URL.String(), res.Status)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}

tempdir, err := tempDir("package-init-download")
if err != nil {
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("error creating temporary path for package template download: %w", err)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}
defer os.RemoveAll(tempdir)

filename := filepath.Base(c.CloneFrom)
filename := filepath.Join(
tempdir,
filepath.Base(c.CloneFrom),
)
ext := filepath.Ext(filename)

// gosec flagged this:
Expand All @@ -957,30 +964,18 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
err = fmt.Errorf("failed to create local %s archive: %w", filename, err)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}
defer func() {
// NOTE: Later on we rename the file to include an extension and the
// following call to os.Remove works still because the `filename` variable
// that is still in scope is also updated to include the extension.
err := os.Remove(filename)
if err != nil {
c.Globals.ErrLog.Add(err)
text.Info(out, "We were unable to clean-up the local %s file (it can be safely removed)", filename)
}
}()

_, err = io.Copy(f, res.Body)
if err != nil {
err = fmt.Errorf("failed to write %s archive to disk: %w", filename, err)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand Down Expand Up @@ -1028,8 +1023,7 @@ mimes:
if err != nil {
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -1045,8 +1039,7 @@ mimes:
err = fmt.Errorf("failed to extract %s archive content: %w", filename, err)
c.Globals.ErrLog.Add(err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand All @@ -1058,8 +1051,7 @@ mimes:

if err := c.ClonePackageFromEndpoint(c.CloneFrom, branch, tag); err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
if spinErr := spinner.StopFail(); spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
Expand Down
78 changes: 78 additions & 0 deletions pkg/commands/compute/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestInit(t *testing.T) {
manifestIncludes string
manifestPath string
stdin string
setupSteps func() error
}{
{
name: "broken endpoint",
Expand Down Expand Up @@ -149,6 +150,28 @@ func TestInit(t *testing.T) {
"SUCCESS: Initialized package",
},
},
{
name: "with --from set to starter kit repository when dir with same name exists in pwd",
args: args("compute init --auto-yes --from https://github.com/fastly/compute-starter-kit-rust-default"),
configFile: config.File{
StarterKits: config.StarterKitLanguages{
Rust: []config.StarterKit{
{
Name: "Default",
Path: "https://github.com/fastly/compute-starter-kit-rust-default.git",
},
},
},
},
wantOutput: []string{
"Fetching package template",
"Reading fastly.toml",
"SUCCESS: Initialized package",
},
setupSteps: func() error {
return os.MkdirAll("compute-starter-kit-rust-default", 0755)
},
},
{
name: "with --from set to starter kit repository with .git extension and branch",
args: args("compute init --from https://github.com/fastly/compute-starter-kit-rust-default.git --branch main"),
Expand All @@ -168,6 +191,28 @@ func TestInit(t *testing.T) {
"SUCCESS: Initialized package",
},
},
{
name: "with --from set to starter kit repository with .git extension and branch when dir with same name exists in pwd",
args: args("compute init --auto-yes --from https://github.com/fastly/compute-starter-kit-rust-default.git --branch main"),
configFile: config.File{
StarterKits: config.StarterKitLanguages{
Rust: []config.StarterKit{
{
Name: "Default",
Path: "https://github.com/fastly/compute-starter-kit-rust-default.git",
},
},
},
},
wantOutput: []string{
"Fetching package template",
"Reading fastly.toml",
"SUCCESS: Initialized package",
},
setupSteps: func() error {
return os.MkdirAll("compute-starter-kit-rust-default.git", 0755)
},
},
{
name: "with --from set to zip archive",
args: args("compute init --from https://github.com/fastly/compute-starter-kit-rust-default/archive/refs/heads/main.zip"),
Expand All @@ -187,6 +232,32 @@ func TestInit(t *testing.T) {
"SUCCESS: Initialized package",
},
},
{
name: "with --from set to zip archive when file with same name exists in pwd",
args: args("compute init --auto-yes --from https://github.com/fastly/compute-starter-kit-rust-default/archive/refs/heads/main.zip"),
configFile: config.File{
StarterKits: config.StarterKitLanguages{
Rust: []config.StarterKit{
{
Name: "Default",
Path: "https://github.com/fastly/compute-starter-kit-rust-default.git",
},
},
},
},
wantOutput: []string{
"Fetching package template",
"Reading fastly.toml",
"SUCCESS: Initialized package",
},
setupSteps: func() error {
file, err := os.Create("main.zip")
if file != nil {
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
defer file.Close()
}
return err
},
},
{
name: "with --from set to tar.gz archive",
args: args("compute init --from https://github.com/Integralist/devnull/files/7339887/compute-starter-kit-rust-default-main.tar.gz"),
Expand Down Expand Up @@ -375,6 +446,13 @@ func TestInit(t *testing.T) {
_ = os.Chdir(pwd)
}()

// Before running the test, run some steps to initialize the environment.
if testcase.setupSteps != nil {
if err := testcase.setupSteps(); err != nil {
t.Fatal(err)
}
}

var stdout bytes.Buffer
app.Init = func(_ []string, _ io.Reader) (*global.Data, error) {
opts := testutil.MockGlobalData(testcase.args, &stdout)
Expand Down
Loading