Skip to content

Commit

Permalink
add error message for deletion when args=0 and no --all flag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and tekton-robot committed Feb 27, 2020
1 parent bbd0371 commit 1ed211c
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a/go.mod h1:ccL7Eh7zubPUSh9
k8s.io/apiserver v0.17.0/go.mod h1:ABM+9x/prjINN6iiffRVNCBR2Wk7uY4z+EtEGZD48cg=
k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f h1:vJOrMsZe+RD884n+WQ5So2oOp7SajI0Op3oOBg64ZsY=
k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f/go.mod h1:qWnH3/b8sp/l7EvlDh7ulDU3UWA4P4N1NFbEEP791tM=
k8s.io/cli-runtime v0.17.3 h1:0ZlDdJgJBKsu77trRUynNiWsRuAvAVPBNaQfnt/1qtc=
k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7 h1:WyPHgjjXvF4zVVwKGZKKiJGBUW45AuN44uSOuH8euuE=
k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/cloud-provider v0.17.0/go.mod h1:Ze4c3w2C0bRsjkBUoHpFi+qWe3ob1wI2/7cUn+YQIDE=
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/clustertask/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ func TestClusterTaskDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using clustertask delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide clustertask name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/condition/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ func TestConditionDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using condition delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide condition name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/eventlistener/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func TestEventListenerDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using eventlistener delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide eventlistener name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/pipelineresource/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ func TestPipelineResourceDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using resource delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide pipelineresource name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestPipelineRunDelete(t *testing.T) {
input: seeds[2],
inputStream: nil,
wantError: true,
want: "must provide pipelineruns to delete or --pipeline flag",
want: "must provide pipelinerun name(s) or use --pipeline flag or --all flag to use delete",
},
{
name: "Remove pipelineruns of a pipeline",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestTaskRunDelete(t *testing.T) {
input: seeds[2],
inputStream: nil,
wantError: true,
want: "must provide taskruns to delete or --task flag",
want: "must provide taskrun name(s) or use --task flag or --all flag to use delete",
},
{
name: "Remove taskruns of a task",
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/triggerbinding/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func TestTriggerBindingDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using triggerbinding delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide triggerbinding name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/triggertemplate/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func TestTriggerTemplateDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from using triggertemplate delete with no names or --all",
command: []string{"delete"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "must provide triggertemplate name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down
17 changes: 13 additions & 4 deletions pkg/helper/options/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,29 @@ type DeleteOptions struct {
}

func (o *DeleteOptions) CheckOptions(s *cli.Stream, resourceNames []string, ns string) error {
// make sure no resource names are provided when using --all flag
if len(resourceNames) > 0 && (o.DeleteAllNs || o.DeleteAll) {
return fmt.Errorf("--all flag should not have any arguments or flags specified with it")
}

// make sure either resource names are provided, name of related resource,
// or --all specified if deleting PipelineRuns or TaskRuns
if len(resourceNames) == 0 && o.ParentResource != "" && o.ParentResourceName == "" && !o.DeleteAllNs {
return fmt.Errorf("must provide %s name(s) or use --%s flag or --all flag to use delete", o.Resource, o.ParentResource)
}

// make sure that resource name or --all flag is specified to use delete
// in non PipelineRun or TaskRun deletions
if len(resourceNames) == 0 && o.ParentResource == "" && o.ParentResourceName == "" && !o.DeleteAllNs && !o.DeleteAll {
return fmt.Errorf("must provide %s name(s) or use --all flag with delete", o.Resource)
}

if o.ForceDelete {
return nil
}

formattedNames := names.QuotedList(resourceNames)

if len(resourceNames) == 0 && o.ParentResource != "" && o.ParentResourceName == "" && !o.DeleteAllNs {
return fmt.Errorf("must provide %ss to delete or --%s flag", o.Resource, o.ParentResource)
}

switch {
case o.DeleteAllNs:
fmt.Fprintf(s.Out, "Are you sure you want to delete all %ss in namespace %q (y/n): ", o.Resource, ns)
Expand Down
12 changes: 8 additions & 4 deletions pkg/helper/options/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,13 @@ func TestDeleteOptions(t *testing.T) {
{
name: "Error when all defaults specified with ParentResource",
opt: &DeleteOptions{Resource: "TaskRun", ParentResource: "task", ForceDelete: false, DeleteRelated: false, DeleteAllNs: false},
stream: &cli.Stream{In: strings.NewReader("y"), Out: os.Stdout},
resourcesNames: []string{},
wantError: true,
want: "must provide TaskRuns to delete or --task flag",
want: "must provide TaskRun name(s) or use --task flag or --all flag to use delete",
},
{
name: "Error when resource name provided with DeleteAllNs",
opt: &DeleteOptions{DeleteAllNs: true},
stream: &cli.Stream{In: strings.NewReader("y"), Out: os.Stdout},
resourcesNames: []string{"test1"},
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
Expand All @@ -129,11 +127,17 @@ func TestDeleteOptions(t *testing.T) {
{
name: "Error when resource name provided with DeleteAll",
opt: &DeleteOptions{DeleteAll: true},
stream: &cli.Stream{In: strings.NewReader("y"), Out: os.Stdout},
resourcesNames: []string{"test1"},
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error when not specifying resource name or --all flag in non PipelineRun/TaskRun deletion",
opt: &DeleteOptions{Resource: "Condition", ParentResource: "", ParentResourceName: "", DeleteRelated: false, DeleteAllNs: false},
resourcesNames: []string{},
wantError: true,
want: "must provide Condition name(s) or use --all flag with delete",
},
}

for _, tp := range testParams {
Expand Down

0 comments on commit 1ed211c

Please sign in to comment.