Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(backport release-1.2): feat!: make delete directive non-strict by default #3502

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
Loading