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/serve): ensure --env files are processed #1039

Merged
merged 2 commits into from
Oct 11, 2023
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
59 changes: 34 additions & 25 deletions pkg/commands/compute/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,28 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

bin, err := GetViceroy(spinner, out, c.av, c.Globals, c.viceroyBinPath, c.forceCheckViceroyLatest)
wd, err := os.Getwd()
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
filename := "fastly.toml"
if c.env.Value != "" {
filename = fmt.Sprintf("fastly.%s.toml", c.env.Value)
}
manifestPath := filepath.Join(wd, filename)
if c.env.Value != "" {
err := c.manifest.File.Read(manifestPath)
if err != nil {
return fmt.Errorf("failed to parse manifest '%s': %w", manifestPath, err)
}
c.av.SetRequestedVersion(c.manifest.File.LocalServer.ViceroyVersion)
if c.Globals.Verbose() {
text.Info(out, "Fastly manifest set to: %s\n\n", manifestPath)
}
}

bin, err := GetViceroy(spinner, out, c.av, c.Globals, manifestPath, c.viceroyBinPath, c.forceCheckViceroyLatest)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,9 +174,9 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
addr: c.addr,
bin: bin,
debug: c.debug,
env: c.env.Value,
errLog: c.Globals.ErrLog,
file: c.file,
manifestPath: manifestPath,
out: out,
profileGuest: c.profileGuest,
profileGuestDir: c.profileGuestDir,
Expand Down Expand Up @@ -245,12 +266,12 @@ func GetViceroy(
out io.Writer,
av github.AssetVersioner,
g *global.Data,
viceroyBinPath string,
manifestPath, viceroyBinPath string,
forceCheckViceroyLatest bool,
) (bin string, err error) {
if viceroyBinPath != "" {
if g.Verbose() {
text.Info(out, "Using user provided install of Viceroy via --viceroy-path flag: %s", viceroyBinPath)
text.Info(out, "Using user provided install of Viceroy via --viceroy-path flag: %s\n\n", viceroyBinPath)
}
return filepath.Abs(viceroyBinPath)
}
Expand All @@ -262,7 +283,7 @@ func GetViceroy(
return "", fmt.Errorf("failed to lookup viceroy binary in user $PATH (user has set $FASTLY_VICEROY_USE_PATH): %w", err)
}
if g.Verbose() {
text.Info(out, "Using user provided install of Viceroy via $PATH lookup: %s", path)
text.Info(out, "Using user provided install of Viceroy via $PATH lookup: %s\n\n", path)
}
return filepath.Abs(path)
}
Expand Down Expand Up @@ -307,12 +328,12 @@ func GetViceroy(
if _, err := semver.Parse(versionToInstall); err != nil {
return bin, fsterr.RemediationError{
Inner: fmt.Errorf("failed to parse configured version as a semver: %w", err),
Remediation: fmt.Sprintf("Ensure the fastly.toml `viceroy_version` value '%s' (under the [local_server] section) is a valid semver (https://semver.org/), e.g. `0.1.0`)", versionToInstall),
Remediation: fmt.Sprintf("Ensure the %s `viceroy_version` value '%s' (under the [local_server] section) is a valid semver (https://semver.org/), e.g. `0.1.0`)", manifestPath, versionToInstall),
}
}
}

err = installViceroy(installedVersion, versionToInstall, forceCheckViceroyLatest, spinner, av, bin, g)
err = installViceroy(installedVersion, versionToInstall, manifestPath, forceCheckViceroyLatest, spinner, av, bin, g)
if err != nil {
g.ErrLog.Add(err)
return bin, err
Expand Down Expand Up @@ -360,7 +381,7 @@ var InstallDir = func() string {
// 2. If so, check the latest release matches the installed version.
// 3. If not latest, check the installed version matches the expected version.
func installViceroy(
installedVersion, versionToInstall string,
installedVersion, versionToInstall, manifestPath string,
forceCheckViceroyLatest bool,
spinner text.Spinner,
av github.AssetVersioner,
Expand Down Expand Up @@ -392,12 +413,12 @@ func installViceroy(
case versionToInstall != "latest":
if installedVersion == versionToInstall {
if g.Verbose() {
text.Info(g.Output, "Viceroy is already installed, and the installed version matches the required version (%s) in the fastly.toml file.\n\n", versionToInstall)
text.Info(g.Output, "Viceroy is already installed, and the installed version matches the required version (%s) in the %s file.\n\n", versionToInstall, manifestPath)
}
return nil
}
if g.Verbose() {
text.Info(g.Output, "Viceroy is already installed, but the installed version (%s) doesn't match the required version (%s) specified in the fastly.toml file.\n\n", installedVersion, versionToInstall)
text.Info(g.Output, "Viceroy is already installed, but the installed version (%s) doesn't match the required version (%s) specified in the %s file.\n\n", installedVersion, versionToInstall, manifestPath)
}

err = spinner.Start()
Expand Down Expand Up @@ -528,9 +549,9 @@ type localOpts struct {
addr string
bin string
debug bool
env string
errLog fsterr.LogInterface
file string
manifestPath string
out io.Writer
profileGuest bool
profileGuestDir cmd.OptionalString
Expand All @@ -542,22 +563,10 @@ type localOpts struct {

// local spawns a subprocess that runs the compiled binary.
func local(opts localOpts) error {
if opts.env != "" {
opts.env = "." + opts.env
}

wd, err := os.Getwd()
if err != nil {
opts.errLog.Add(err)
return err
}

manifestPath := filepath.Join(wd, fmt.Sprintf("fastly%s.toml", opts.env))

// NOTE: Viceroy no longer displays errors unless in verbose mode.
// This can cause confusion for customers: https://github.com/fastly/cli/issues/913
// So regardless of CLI --verbose flag we'll always set verbose for Viceroy.
args := []string{"-v", "-C", manifestPath, "--addr", opts.addr, opts.file}
args := []string{"-v", "-C", opts.manifestPath, "--addr", opts.addr, opts.file}

if opts.debug {
args = append(args, "--debug")
Expand All @@ -578,7 +587,7 @@ func local(opts localOpts) error {
if opts.restarted {
text.Break(opts.out)
}
text.Output(opts.out, "%s: %s", text.BoldYellow("Manifest"), manifestPath)
text.Output(opts.out, "%s: %s", text.BoldYellow("Manifest"), opts.manifestPath)
text.Output(opts.out, "%s: %s", text.BoldYellow("Wasm binary"), opts.file)
text.Output(opts.out, "%s: %s", text.BoldYellow("Viceroy binary"), opts.bin)

Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/compute/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ func TestGetViceroy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
manifestPath := "fastly.toml"
viceroyBinPath := "" // --viceroy-path flag for overriding CLI handling the Viceroy checks/downloads.
viceroyCheck := false
_, err = compute.GetViceroy(spinner, &out, av, &g, viceroyBinPath, viceroyCheck)
_, err = compute.GetViceroy(spinner, &out, av, &g, manifestPath, viceroyBinPath, viceroyCheck)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func (g *Asset) RequestedVersion() string {
return g.versionRequested
}

// SetRequestedVersion sets the version of the asset to be downloaded.
// This is typically used by `compute serve` when an `--env` flag is set.
func (g *Asset) SetRequestedVersion(version string) {
g.versionRequested = version
}

// metadata acquires GitHub metadata.
func (g *Asset) metadata() (m Metadata, err error) {
endpoint := fmt.Sprintf(metadataURL, g.repo, runtime.GOOS, runtime.GOARCH)
Expand Down Expand Up @@ -238,6 +244,8 @@ type AssetVersioner interface {
DownloadVersion(version string) (bin string, err error)
// RequestedVersion returns the version defined in the fastly.toml file.
RequestedVersion() (version string)
// SetRequestedVersion sets the version of the asset to be downloaded.
SetRequestedVersion(version string)
// URL returns the asset URL if set, otherwise calls the API metadata endpoint.
URL() (url string, err error)
// LatestVersion returns the latest version.
Expand Down
5 changes: 5 additions & 0 deletions pkg/mock/versioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@
func (av AssetVersioner) RequestedVersion() (version string) {
return ""
}

// SetRequestedVersion implements github.Versioner interface.
func (av AssetVersioner) SetRequestedVersion(version string) {

Check failure on line 52 in pkg/mock/versioner.go

View workflow job for this annotation

GitHub Actions / lint

parameter 'version' seems to be unused, consider removing or renaming it as _
// no-op
}
Loading