Skip to content

Commit

Permalink
fix: directory switching logic (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist authored Feb 15, 2024
1 parent 5cbf344 commit 8357763
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 55 deletions.
29 changes: 20 additions & 9 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type BuildCommand struct {
MetadataDisable bool
MetadataFilterEnvVars string
MetadataShow bool
SkipChangeDir bool // set by parent composite commands (e.g. serve, publish)
}

// NewBuildCommand returns a usable command registered under the parent.
Expand Down Expand Up @@ -112,15 +113,18 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
}()
manifestPath := filepath.Join(wd, manifestFilename)

projectDir, err := ChangeProjectDirectory(c.Flags.Dir)
if err != nil {
return err
}
if projectDir != "" {
if c.Globals.Verbose() {
text.Info(out, ProjectDirMsg, projectDir)
var projectDir string
if !c.SkipChangeDir {
projectDir, err = ChangeProjectDirectory(c.Flags.Dir)
if err != nil {
return err
}
if projectDir != "" {
if c.Globals.Verbose() {
text.Info(out, ProjectDirMsg, projectDir)
}
manifestPath = filepath.Join(projectDir, manifestFilename)
}
manifestPath = filepath.Join(projectDir, manifestFilename)
}

spinner, err := text.NewSpinner(out)
Expand All @@ -135,7 +139,14 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
}(c.Globals.ErrLog)

err = spinner.Process(fmt.Sprintf("Verifying %s", manifestFilename), func(_ *text.SpinnerWrapper) error {
if projectDir != "" || c.Flags.Env != "" {
// The check for c.SkipChangeDir here is because we might need to attempt
// another read of the manifest file. To explain: if we're skipping the
// change of directory, it means we were called from a composite command,
// which has already changed directory to one that contains the fastly.toml
// file. This means we should try reading the manifest file from the new
// location as the potential ReadError() would have been based on the
// initial directory the CLI was invoked from.
if c.SkipChangeDir || projectDir != "" || c.Flags.Env != "" {
err = c.Globals.Manifest.File.Read(manifestPath)
} else {
err = c.Globals.Manifest.File.ReadError()
Expand Down
79 changes: 46 additions & 33 deletions pkg/commands/compute/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type PublishCommand struct {
statusCheckOff bool
statusCheckPath string
statusCheckTimeout int

// Publish private fields
projectDir string
}

// NewPublishCommand returns a usable command registered under the parent.
Expand Down Expand Up @@ -93,6 +96,42 @@ func NewPublishCommand(parent argparser.Registerer, g *global.Data, build *Build
// non-deterministic ways. It's best to leave those nested commands to handle
// the progress indicator.
func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
defer func() {
_ = os.Chdir(wd)
}()

c.projectDir, err = ChangeProjectDirectory(c.dir.Value)
if err != nil {
return err
}
if c.projectDir != "" {
if c.Globals.Verbose() {
text.Info(out, ProjectDirMsg, c.projectDir)
}
}

err = c.Build(in, out)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Break(out)

err = c.Deploy(in, out)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
return nil
}

// Build constructs and executes the build logic.
func (c *PublishCommand) Build(in io.Reader, out io.Writer) error {
// Reset the fields on the BuildCommand based on PublishCommand values.
if c.dir.WasSet {
c.build.Flags.Dir = c.dir.Value
Expand Down Expand Up @@ -121,33 +160,14 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
if c.metadataShow.WasSet {
c.build.MetadataShow = c.metadataShow.Value
}

err = c.build.Exec(in, out)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Break(out)

wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
defer func() {
_ = os.Chdir(wd)
}()

projectDir, err := ChangeProjectDirectory(c.dir.Value)
if err != nil {
return err
}
if projectDir != "" {
if c.Globals.Verbose() {
text.Info(out, ProjectDirMsg, projectDir)
}
if c.projectDir != "" {
c.build.SkipChangeDir = true // we've already changed directory
}
return c.build.Exec(in, out)
}

// Deploy constructs and executes the deploy logic.
func (c *PublishCommand) Deploy(in io.Reader, out io.Writer) error {
// Reset the fields on the DeployCommand based on PublishCommand values.
if c.dir.WasSet {
c.deploy.Dir = c.dir.Value
Expand Down Expand Up @@ -180,12 +200,5 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
c.deploy.StatusCheckTimeout = c.statusCheckTimeout
}
c.deploy.StatusCheckPath = c.statusCheckPath

err = c.deploy.Exec(in, out)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

return nil
return c.deploy.Exec(in, out)
}
29 changes: 16 additions & 13 deletions pkg/commands/compute/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ServeCommand struct {
file string
profileGuest bool
profileGuestDir argparser.OptionalString
projectDir string
skipBuild bool
watch bool
watchDir argparser.OptionalString
Expand Down Expand Up @@ -114,14 +115,6 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
}

if !c.skipBuild {
err = c.Build(in, out)
if err != nil {
return err
}
text.Break(out)
}

manifestFilename := EnvironmentManifest(c.env.Value)
if c.env.Value != "" {
if c.Globals.Verbose() {
Expand All @@ -138,15 +131,23 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
}()
manifestPath := filepath.Join(wd, manifestFilename)

projectDir, err := ChangeProjectDirectory(c.dir.Value)
c.projectDir, err = ChangeProjectDirectory(c.dir.Value)
if err != nil {
return err
}
if projectDir != "" {
if c.projectDir != "" {
if c.Globals.Verbose() {
text.Info(out, ProjectDirMsg, projectDir)
text.Info(out, ProjectDirMsg, c.projectDir)
}
manifestPath = filepath.Join(c.projectDir, manifestFilename)
}

if !c.skipBuild {
err = c.Build(in, out)
if err != nil {
return err
}
manifestPath = filepath.Join(projectDir, manifestFilename)
text.Break(out)
}

c.setBackendsWithDefaultOverrideHostIfMissing(out)
Expand Down Expand Up @@ -268,7 +269,9 @@ func (c *ServeCommand) Build(in io.Reader, out io.Writer) error {
if c.metadataShow.WasSet {
c.build.MetadataShow = c.metadataShow.Value
}

if c.projectDir != "" {
c.build.SkipChangeDir = true // we've already changed directory
}
return c.build.Exec(in, out)
}

Expand Down

0 comments on commit 8357763

Please sign in to comment.