From fb58f058bf82efdf7205861b234e33d677afa81e Mon Sep 17 00:00:00 2001 From: Samuel Manzer Date: Tue, 14 Nov 2023 08:52:22 -0800 Subject: [PATCH] Fix edit_step backup working directory handling (#418) Summary: Pull Request resolved: https://github.com/facebookincubator/TTPForge/pull/418 X-link: https://github.com/facebookincubator/ForgeArmory/pull/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 --- pkg/blocks/editstep.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/blocks/editstep.go b/pkg/blocks/editstep.go index 7741fb58..277412e1 100755 --- a/pkg/blocks/editstep.go +++ b/pkg/blocks/editstep.go @@ -90,6 +90,7 @@ 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 @@ -97,6 +98,12 @@ func (s *EditStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) { 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 { @@ -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) }