Skip to content

Commit

Permalink
feat!: make delete directive non-strict by default (#3498)
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
(cherry picked from commit 7b9b9e5)
  • Loading branch information
hiddeco committed Feb 15, 2025
1 parent 2af22e4 commit ff51596
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ the [`git-clear` step](git-clear.md) instead.

| Name | Type | Required | Description |
|-----------|------|----------|------------------------------------------|
| `path` | `string` | Y | Path to the file or directory to delete. |
| `path` | `string` | Y | Path to the file or directory to delete. |
| `strict` | `bool` | N | Strict will cause the directive to fail if the path does not exist. Defaults to `false`. |

## Examples

Expand Down
23 changes: 18 additions & 5 deletions internal/directives/file_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func (f *fileDeleter) runPromotionStep(
}

if symlink {
err = os.Remove(absPath)
if err != nil {
if f.ignoreNotExist(cfg.Strict, os.Remove(absPath)) != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, err
}
} else {
Expand All @@ -86,7 +85,10 @@ func (f *fileDeleter) runPromotionStep(
fmt.Errorf("could not secure join path %q: %w", cfg.Path, err)
}

if err = removePath(pathToDelete); err != nil {
if err = f.ignoreNotExist(
cfg.Strict,
removePath(pathToDelete),
); err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored},
fmt.Errorf("failed to delete %q: %w", cfg.Path, sanitizePathError(err, stepCtx.WorkDir))
}
Expand All @@ -95,10 +97,11 @@ func (f *fileDeleter) runPromotionStep(
return PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, nil
}

// isSymlink checks if a path is a symlink.
func (f *fileDeleter) isSymlink(path string) (bool, error) {
fi, err := os.Lstat(path)
if err != nil {
// if file doesn't exist, it's not a symlink
// If file doesn't exist, it's not a symlink
if os.IsNotExist(err) {
return false, nil
}
Expand All @@ -108,7 +111,7 @@ func (f *fileDeleter) isSymlink(path string) (bool, error) {
return fi.Mode()&os.ModeSymlink != 0, nil
}

// resolveAbsPath resolves the absolute path from the workDir base path
// resolveAbsPath resolves the absolute path from the workDir base path.
func (f *fileDeleter) resolveAbsPath(workDir string, path string) (string, error) {
absBase, err := filepath.Abs(workDir)
if err != nil {
Expand Down Expand Up @@ -137,6 +140,16 @@ func (f *fileDeleter) resolveAbsPath(workDir string, path string) (string, error
return absPath, nil
}

// ignoreNotExist ignores os.IsNotExist errors depending on the strict
// flag. If strict is false and the error is os.IsNotExist, it returns
// nil.
func (f *fileDeleter) ignoreNotExist(strict bool, err error) error {
if !strict && os.IsNotExist(err) {
return nil
}
return err
}

func removePath(path string) error {
fi, err := os.Lstat(path)
if err != nil {
Expand Down
19 changes: 17 additions & 2 deletions internal/directives/file_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,33 @@ func Test_fileDeleter_runPromotionStep(t *testing.T) {
},
},
{
name: "fails for non-existent path",
name: "fails for non-existent path when strict is true",
setupFiles: func(t *testing.T) string {
return t.TempDir()
},
cfg: DeleteConfig{
Path: "nonExistentFile.txt",
Path: "nonExistentFile.txt",
Strict: true,
},
assertions: func(t *testing.T, _ string, result PromotionStepResult, err error) {
assert.Error(t, err)
assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, result)
},
},
{
name: "succeeds for non-existent path when strict is false",
setupFiles: func(t *testing.T) string {
return t.TempDir()
},
cfg: DeleteConfig{
Path: "nonExistentFile.txt",
Strict: false,
},
assertions: func(t *testing.T, _ string, result PromotionStepResult, err error) {
assert.NoError(t, err)
assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, result)
},
},
{
name: "removes symlink only",
setupFiles: func(t *testing.T) string {
Expand Down
5 changes: 5 additions & 0 deletions internal/directives/schemas/delete-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "string",
"description": "Path is the path to the file or directory to delete.",
"minLength": 1
},
"strict": {
"type": "boolean",
"description": "Strict will cause the directive to fail if the path does not exist.",
"default": false
}
}
}
2 changes: 2 additions & 0 deletions internal/directives/zz_config_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions ui/src/gen/directives/delete-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"type": "string",
"description": "Path is the path to the file or directory to delete.",
"minLength": 1
},
"strict": {
"type": "boolean",
"description": "Strict will cause the directive to fail if the path does not exist.",
"default": false
}
}
}

0 comments on commit ff51596

Please sign in to comment.