Skip to content

Commit

Permalink
Fix edit_step backup working directory handling (#418)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #418

X-link: facebookincubator/ForgeArmory#97

Fix `edit_step` to make `backup_file` also use `FetchAbs` just like the target file.

Update example in `ForgeArmory` to reflect new behavior

I think this was actually ok once we switched back to doing a top-level directory change in RunSteps, but either way it is better to have this redundancy.

Reviewed By: cedowens

Differential Revision: D51307671

fbshipit-source-id: 2873deaa792922e63899ec79d7cb1af19c9d2ede
  • Loading branch information
d3sch41n authored and facebook-github-bot committed Nov 14, 2023
1 parent 4171171 commit fb58f05
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/blocks/editstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,20 @@ func (s *EditStep) Validate(execCtx TTPExecutionContext) error {
func (s *EditStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) {
fileSystem := s.FileSystem
targetPath := s.FileToEdit
backupPath := s.BackupFile
if fileSystem == nil {
fileSystem = afero.NewOsFs()
var err error
targetPath, err = FetchAbs(targetPath, execCtx.WorkDir)
if err != nil {
return nil, err
}
if backupPath != "" {
backupPath, err = FetchAbs(backupPath, execCtx.WorkDir)
if err != nil {
return nil, err
}
}
}
rawContents, err := afero.ReadFile(fileSystem, targetPath)
if err != nil {
Expand All @@ -105,8 +112,8 @@ func (s *EditStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) {

contents := string(rawContents)

if s.BackupFile != "" {
err = afero.WriteFile(fileSystem, s.BackupFile, []byte(contents), 0644)
if backupPath != "" {
err = afero.WriteFile(fileSystem, backupPath, []byte(contents), 0644)
if err != nil {
return nil, fmt.Errorf("could not write backup file %v: %v", s.BackupFile, err)
}
Expand Down

0 comments on commit fb58f05

Please sign in to comment.