Skip to content

Commit

Permalink
feat(spinner): abstract common pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Oct 2, 2023
1 parent 649b55c commit 1bdc157
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 718 deletions.
147 changes: 45 additions & 102 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,77 +83,41 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
}(c.Globals.ErrLog)

err = spinner.Start()
if err != nil {
return err
}
msg := "Verifying fastly.toml"
spinner.Message(msg + "...")

err = c.Manifest.File.ReadError()
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fsterr.ErrReadingManifest
}
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
err = spinner.Process("Verifying fastly.toml", func(_ *text.SpinnerWrapper) error {
err = c.Manifest.File.ReadError()
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fsterr.ErrReadingManifest
}
c.Globals.ErrLog.Add(err)
return err
}

return err
}

spinner.StopMessage(msg)
err = spinner.Stop()
if err != nil {
return err
}

err = spinner.Start()
return nil
})
if err != nil {
return err
}
msg = "Identifying package name"
spinner.Message(msg + "...")

packageName, err := packageName(c)
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
var pkgName string
err = spinner.Process("Identifying package name", func(_ *text.SpinnerWrapper) error {
pkgName, err = packageName(c)
if err != nil {
return err
}
return err
}

spinner.StopMessage(msg)
err = spinner.Stop()
if err != nil {
return err
}

err = spinner.Start()
return nil
})
if err != nil {
return err
}
msg = "Identifying toolchain"
spinner.Message(msg + "...")

toolchain, err := toolchain(c)
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
var toolchain string
err = spinner.Process("Identifying toolchain", func(_ *text.SpinnerWrapper) error {
toolchain, err = identifyToolchain(c)
if err != nil {
return err
}
return err
}

spinner.StopMessage(msg)
err = spinner.Stop()
return nil
})
if err != nil {
return err
}
Expand All @@ -175,49 +139,28 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

err = spinner.Start()
if err != nil {
return err
}
msg = "Creating package archive"
spinner.Message(msg + "...")

dest := filepath.Join("pkg", fmt.Sprintf("%s.tar.gz", packageName))

// NOTE: The minimum package requirement is `fastly.toml` and `main.wasm`.
files := []string{
manifest.Filename,
"bin/main.wasm",
}
dest := filepath.Join("pkg", fmt.Sprintf("%s.tar.gz", pkgName))

files, err = c.includeSourceCode(files, language.SourceDirectory)
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
err = spinner.Process("Creating package archive", func(_ *text.SpinnerWrapper) error {
// NOTE: The minimum package requirement is `fastly.toml` and `main.wasm`.
files := []string{
manifest.Filename,
"bin/main.wasm",
}
return err
}

err = CreatePackageArchive(files, dest)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Files": files,
"Destination": dest,
})

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
files, err = c.includeSourceCode(files, language.SourceDirectory)
if err != nil {
return err
}

return fmt.Errorf("error creating package archive: %w", err)
}

spinner.StopMessage(msg)
err = spinner.Stop()
err = CreatePackageArchive(files, dest)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Files": files,
"Destination": dest,
})
return fmt.Errorf("error creating package archive: %w", err)
}
return nil
})
if err != nil {
return err
}
Expand Down Expand Up @@ -288,12 +231,12 @@ func packageName(c *BuildCommand) (string, error) {
return sanitize.BaseName(name), nil
}

// toolchain determines the programming language.
// identifyToolchain determines the programming language.
//
// It prioritises the --language flag over the manifest field.
// Will error if neither are provided.
// Lastly, it will normalise with a trim and lowercase.
func toolchain(c *BuildCommand) (string, error) {
func identifyToolchain(c *BuildCommand) (string, error) {
var toolchain string

switch {
Expand Down
72 changes: 23 additions & 49 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,30 +803,17 @@ func pkgCompare(client api.Interface, serviceID string, version int, filesHash s

// pkgUpload uploads the package to the specified service and version.
func pkgUpload(spinner text.Spinner, client api.Interface, serviceID string, version int, path string) error {
err := spinner.Start()
if err != nil {
return err
}
msg := "Uploading package"
spinner.Message(msg + "...")

_, err = client.UpdatePackage(&fastly.UpdatePackageInput{
ServiceID: serviceID,
ServiceVersion: version,
PackagePath: path,
})
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return spinner.Process("Uploading package", func(_ *text.SpinnerWrapper) error {
_, err := client.UpdatePackage(&fastly.UpdatePackageInput{
ServiceID: serviceID,
ServiceVersion: version,
PackagePath: path,
})
if err != nil {
return fmt.Errorf("error uploading package: %w", err)
}

return fmt.Errorf("error uploading package: %w", err)
}

spinner.StopMessage(msg)
return spinner.Stop()
return nil
})
}

// setupObjects is a collection of backend objects created during setup.
Expand Down Expand Up @@ -1162,33 +1149,20 @@ func processService(c *DeployCommand, serviceID string, serviceVersion int, spin
}
}

err := spinner.Start()
if err != nil {
return err
}
msg := fmt.Sprintf("Activating service (version %d)", serviceVersion)
spinner.Message(msg + "...")

_, err = c.Globals.APIClient.ActivateVersion(&fastly.ActivateVersionInput{
ServiceID: serviceID,
ServiceVersion: serviceVersion,
})
if err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
}

c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
"Service Version": serviceVersion,
return spinner.Process(fmt.Sprintf("Activating service (version %d)", serviceVersion), func(_ *text.SpinnerWrapper) error {
_, err := c.Globals.APIClient.ActivateVersion(&fastly.ActivateVersionInput{
ServiceID: serviceID,
ServiceVersion: serviceVersion,
})
return fmt.Errorf("error activating version: %w", err)
}

spinner.StopMessage(msg)
return spinner.Stop()
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
"Service Version": serviceVersion,
})
return fmt.Errorf("error activating version: %w", err)
}
return nil
})
}

func getServiceDomain(apiClient api.Interface, serviceID string, serviceVersion int) (string, error) {
Expand Down
Loading

0 comments on commit 1bdc157

Please sign in to comment.