Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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: 1 addition & 1 deletion pkg/commands/backend/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
input.Port = fastly.Int(443)
}

if !c.overrideHost.WasSet && !c.sslCertHostname.WasSet && !c.sslSNIHostname.WasSet {
if input.Address != nil && !c.overrideHost.WasSet && !c.sslCertHostname.WasSet && !c.sslSNIHostname.WasSet {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing a nil pointer dereference bug

overrideHost, sslSNIHostname, sslCertHostname := SetBackendHostDefaults(*input.Address)
input.OverrideHost = &overrideHost
input.SSLSNIHostname = &sslSNIHostname
Expand Down
84 changes: 25 additions & 59 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

language, err := language(toolchain, c, out)
language, err := language(toolchain, c, in, out, spinner)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was related to simplifying the Toolchain interface's Build method (as we already pass a bunch of things into each language constructor, and some of the data was overlapping).

if err != nil {
return err
}
Expand All @@ -167,17 +167,7 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

postBuildCallback := func() error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix the order of rendering I needed to move this logic (and associated prompt functionality) inside the toolchain implementation.

if !c.Globals.Flags.AutoYes && !c.Globals.Flags.NonInteractive {
err := promptForBuildContinue(CustomPostBuildScriptMessage, c.Manifest.File.Scripts.PostBuild, out, in, c.Globals.Verbose())
if err != nil {
return err
}
}
return nil
}

if err := language.Build(out, spinner, c.Globals.Flags.Verbose, postBuildCallback); err != nil {
if err := language.Build(); err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Language": language.Name,
})
Expand Down Expand Up @@ -318,7 +308,7 @@ func toolchain(c *BuildCommand) (string, error) {
}

// language returns a pointer to a supported language.
func language(toolchain string, c *BuildCommand, out io.Writer) (*Language, error) {
func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, spinner text.Spinner) (*Language, error) {
var language *Language
switch toolchain {
case "assemblyscript":
Expand All @@ -327,10 +317,11 @@ func language(toolchain string, c *BuildCommand, out io.Writer) (*Language, erro
SourceDirectory: AsSourceDirectory,
Toolchain: NewAssemblyScript(
&c.Manifest.File,
c.Globals.ErrLog,
c.Flags.Timeout,
c.Globals,
c.Flags,
in,
out,
c.Globals.Verbose(),
spinner,
),
})
case "go":
Expand All @@ -339,11 +330,11 @@ func language(toolchain string, c *BuildCommand, out io.Writer) (*Language, erro
SourceDirectory: GoSourceDirectory,
Toolchain: NewGo(
&c.Manifest.File,
c.Globals.ErrLog,
c.Flags.Timeout,
c.Globals.Config.Language.Go,
c.Globals,
c.Flags,
in,
out,
c.Globals.Verbose(),
spinner,
),
})
case "javascript":
Expand All @@ -352,10 +343,11 @@ func language(toolchain string, c *BuildCommand, out io.Writer) (*Language, erro
SourceDirectory: JsSourceDirectory,
Toolchain: NewJavaScript(
&c.Manifest.File,
c.Globals.ErrLog,
c.Flags.Timeout,
c.Globals,
c.Flags,
in,
out,
c.Globals.Verbose(),
spinner,
),
})
case "rust":
Expand All @@ -364,20 +356,23 @@ func language(toolchain string, c *BuildCommand, out io.Writer) (*Language, erro
SourceDirectory: RustSourceDirectory,
Toolchain: NewRust(
&c.Manifest.File,
c.Globals.ErrLog,
c.Flags.Timeout,
c.Globals.Config.Language.Rust,
c.Globals,
c.Flags,
in,
out,
c.Globals.Verbose(),
spinner,
),
})
case "other":
language = NewLanguage(&LanguageOptions{
Name: "other",
Toolchain: NewOther(
c.Manifest.File.Scripts,
c.Globals.ErrLog,
c.Flags.Timeout,
&c.Manifest.File,
c.Globals,
c.Flags,
in,
out,
spinner,
),
})
default:
Expand Down Expand Up @@ -406,35 +401,6 @@ func binDir(c *BuildCommand) error {
return nil
}

// promptForBuildContinue ensures the user is happy to continue with the build
// when there is either a custom build or post build in the fastly.toml
// manifest file.
func promptForBuildContinue(msg, script string, out io.Writer, in io.Reader, verbose bool) error {
text.Info(out, "%s:\n", msg)
text.Break(out)
text.Indent(out, 4, "%s", script)

var post string
if msg == CustomPostBuildScriptMessage {
post = "post "
}

label := fmt.Sprintf("\nAre you sure you want to continue with the %sbuild step? [y/N] ", post)
answer, err := text.AskYesNo(out, label, in)
if err != nil {
return err
}
if !answer {
text.Info(out, "Stopping the %sbuild process.", post)
if !verbose {
text.Break(out)
}
return fsterr.ErrBuildStopped
}
text.Break(out)
return nil
}

// CreatePackageArchive packages build artifacts as a Fastly package.
// The package must be a GZipped Tar archive.
//
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/compute/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ func TestBuildOther(t *testing.T) {
wantOutput: []string{
"echo doing a post build",
"Are you sure you want to continue with the post build step?",
"Stopping the post build process",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed a redundant message that was duplicating the final error message displayed.

},
wantError: "build process stopped by user",
},
// NOTE: All following tests pass --verbose so we can see post_build output.
{
Expand Down Expand Up @@ -772,14 +772,14 @@ func TestBuildOther(t *testing.T) {
name = "test"
[scripts]
build = "touch ./bin/main.wasm"
post_build = "echo doing a post build with no confirmation prompt"`,
post_build = "echo doing a post build with no confirmation prompt && exit 1"`, // force an error so post_build is displayed to validate it was run.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now only show build/post_build output when there is an error (no need to show it if the project compiles successfully) and so we needed to fix the test to force an error so we could validate the post_build script was run.

wantOutput: []string{
"doing a post build with no confirmation prompt",
"Built package",
},
dontWantOutput: []string{
"Are you sure you want to continue with the build step?",
},
wantError: "exit status 1", // because we have to trigger an error to see the post_build output
},
} {
t.Run(testcase.name, func(t *testing.T) {
Expand Down
53 changes: 34 additions & 19 deletions pkg/commands/compute/language_assemblyscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
)
Expand Down Expand Up @@ -37,43 +38,55 @@ const AsSourceDirectory = "assembly"
// NewAssemblyScript constructs a new AssemblyScript toolchain.
func NewAssemblyScript(
fastlyManifest *manifest.File,
errlog fsterr.LogInterface,
timeout int,
globals *global.Data,
flags Flags,
in io.Reader,
out io.Writer,
verbose bool,
spinner text.Spinner,
) *AssemblyScript {
return &AssemblyScript{
Shell: Shell{},
Shell: Shell{},

build: fastlyManifest.Scripts.Build,
errlog: errlog,
errlog: globals.ErrLog,
input: in,
output: out,
postBuild: fastlyManifest.Scripts.PostBuild,
timeout: timeout,
verbose: verbose,
spinner: spinner,
timeout: flags.Timeout,
verbose: globals.Verbose(),
}
}

// AssemblyScript implements a Toolchain for the AssemblyScript language.
type AssemblyScript struct {
Shell

// autoYes is the --auto-yes flag.
autoYes bool
// build is a shell command defined in fastly.toml using [scripts.build].
build string
// errlog is an abstraction for recording errors to disk.
errlog fsterr.LogInterface
// input is the user's terminal stdin stream
input io.Reader
// nonInteractive is the --non-interactive flag.
nonInteractive bool
// output is the users terminal stdout stream
output io.Writer
// postBuild is a custom script executed after the build but before the Wasm
// binary is added to the .tar.gz archive.
postBuild string
// spinner is a terminal progress status indicator.
spinner text.Spinner
// timeout is the build execution threshold.
timeout int
// verbose indicates if the user set --verbose
verbose bool
}

// Build compiles the user's source code into a Wasm binary.
func (a *AssemblyScript) Build(out io.Writer, spinner text.Spinner, verbose bool, callback func() error) error {
func (a *AssemblyScript) Build() error {
var noBuildScript bool
if a.build == "" {
a.build = AsDefaultBuildCommand
Expand All @@ -89,20 +102,22 @@ func (a *AssemblyScript) Build(out io.Writer, spinner text.Spinner, verbose bool
}

if noBuildScript && a.verbose {
text.Info(out, "No [scripts.build] found in fastly.toml. The following default build command for AssemblyScript will be used: `%s`\n", a.build)
text.Break(out)
text.Info(a.output, "No [scripts.build] found in fastly.toml. The following default build command for AssemblyScript will be used: `%s`\n", a.build)
text.Break(a.output)
}

bt := BuildToolchain{
buildFn: a.Shell.Build,
buildScript: a.build,
errlog: a.errlog,
postBuild: a.postBuild,
timeout: a.timeout,
out: out,
postBuildCallback: callback,
spinner: spinner,
verbose: verbose,
autoYes: a.autoYes,
buildFn: a.Shell.Build,
buildScript: a.build,
errlog: a.errlog,
in: a.input,
nonInteractive: a.nonInteractive,
out: a.output,
postBuild: a.postBuild,
spinner: a.spinner,
timeout: a.timeout,
verbose: a.verbose,
}

return bt.Build()
Expand Down
Loading