Skip to content

Commit

Permalink
Validate CD Step at Run instead of Validate (#519)
Browse files Browse the repository at this point in the history
Summary:

Because TTPForge runs validations on TTP load, and we validate our target directory in the Validate action, the CD step will validate the existence of its target directory at TTP load.  This means that if you create your target directory as part of your TTP, your CD step will fail because the directory does not exist yet.

By moving the check to the run step, we will now hold off on validating the directory's existence until the CD step is run, allowing previous steps to create the target directory.

Reviewed By: inesusvet

Differential Revision: D65831540
  • Loading branch information
RoboticPrism authored and facebook-github-bot committed Nov 18, 2024
1 parent 403f3d7 commit 20fe6b5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
api_version: 2.0
uuid: c15da914-de49-4297-a281-9c81b4fea413
name: change_directory_example_with_runtime_created_directory
description: |
This TTP shows you how to use the change_directory action type to change the
working directory for all future actions, even if the directory doesn't exist until
this TTP is ran.
args:
- name: cd_destination
description: this argument is where we will try to cd to
default: /tmp/this_doesnt_exist_yet
steps:
- name: "Initial directory"
inline: |
echo "Current working directory is: \"$(pwd)\""
- name: "Create directory"
inline: |
mkdir {{.Args.cd_destination}}
cleanup:
inline: |
rm -rf {{.Args.cd_destination}}
- name: "cd"
cd: {{.Args.cd_destination}}
cleanup: default
- name: "New directory"
inline: |
echo "Current working directory is: \"$(pwd)\""
34 changes: 19 additions & 15 deletions pkg/blocks/changedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,6 @@ func (step *ChangeDirectoryStep) Validate(_ TTPExecutionContext) error {
return err
}

// Check if cd is a valid directory
fsys := step.FileSystem
if fsys == nil {
fsys = afero.NewOsFs()
}

exists, err := afero.DirExists(fsys, step.Cd)
if err != nil {
return err
}

if !exists {
return fmt.Errorf("directory \"%s\" does not exist", step.Cd)
}

return nil
}

Expand All @@ -96,6 +81,25 @@ func (step *ChangeDirectoryStep) Execute(ctx TTPExecutionContext) (*ActResult, e
step.Cd = step.PreviousCDStep.PreviousDir
}

// Check if cd is a valid directory
fsys := step.FileSystem
if fsys == nil {
if step.PreviousCDStep != nil && step.PreviousCDStep.FileSystem != nil {
fsys = step.PreviousCDStep.FileSystem
} else {
fsys = afero.NewOsFs()
}
}

exists, err := afero.DirExists(fsys, step.Cd)
if err != nil {
return nil, err
}

if !exists {
return nil, fmt.Errorf("directory \"%s\" does not exist", step.Cd)
}

logging.L().Infof("Changing directory to %s", step.Cd)

if step.Cd == "" {
Expand Down

0 comments on commit 20fe6b5

Please sign in to comment.