Skip to content

Commit

Permalink
Merge pull request #347 from nerdalize/feature/delete-all
Browse files Browse the repository at this point in the history
Add --all option to `nerd dataset delete` and `nerd job delete`
  • Loading branch information
advdv committed Mar 5, 2018
2 parents 0a9a3c3 + d27eaa8 commit 4e1d660
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/dataset_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"strings"

flags "github.com/jessevdk/go-flags"
"github.com/mitchellh/cli"
Expand All @@ -12,6 +13,7 @@ import (
//DatasetDelete command
type DatasetDelete struct {
KubeOpts
All bool `long:"all" short:"a" description:"delete all your datasets in one command"`

*command
}
Expand All @@ -27,6 +29,9 @@ func DatasetDeleteFactory(ui cli.Ui) cli.CommandFactory {

//Execute runs the command
func (cmd *DatasetDelete) Execute(args []string) (err error) {
if cmd.All {
return cmd.deleteAll()
}
if len(args) < 1 {
return errShowUsage(fmt.Sprintf(MessageNotEnoughArguments, 1, ""))
}
Expand Down Expand Up @@ -57,6 +62,48 @@ func (cmd *DatasetDelete) Execute(args []string) (err error) {
return nil
}

func (cmd *DatasetDelete) deleteAll() error {
kopts := cmd.KubeOpts
deps, err := NewDeps(cmd.Logger(), kopts)
if err != nil {
return renderConfigError(err, "failed to configure")
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, cmd.Timeout)
defer cancel()

s, err := cmd.out.Ask("Are you sure you want to delete all your datasets? (y/N)")
if err != nil {
return err
}
if !strings.HasPrefix(strings.ToLower(s), "y") {
return nil
}

kube := svc.NewKube(deps)
datasets, err := kube.ListDatasets(ctx, &svc.ListDatasetsInput{})
if err != nil {
return renderServiceError(err, "failed to get all datasets")
}
if len(datasets.Items) == 0 {
cmd.out.Info("No dataset found.")
}
for _, ds := range datasets.Items {
in := &svc.DeleteDatasetInput{
Name: ds.Name,
}

_, err = kube.DeleteDataset(ctx, in)
if err != nil {
return renderServiceError(err, fmt.Sprintf("failed to delete dataset `%s`", in.Name))
}

cmd.out.Infof("Deleted dataset: '%s'", in.Name)
}
return nil
}

// Description returns long-form help text
func (cmd *DatasetDelete) Description() string { return cmd.Synopsis() }

Expand Down
46 changes: 46 additions & 0 deletions cmd/job_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"strings"

flags "github.com/jessevdk/go-flags"
"github.com/mitchellh/cli"
Expand All @@ -12,6 +13,7 @@ import (
//JobDelete command
type JobDelete struct {
KubeOpts
All bool `long:"all" short:"a" description:"delete all your jobs in one command"`

*command
}
Expand All @@ -27,6 +29,9 @@ func JobDeleteFactory(ui cli.Ui) cli.CommandFactory {

//Execute runs the command
func (cmd *JobDelete) Execute(args []string) (err error) {
if cmd.All {
return cmd.deleteAll()
}
if len(args) < 1 {
return errShowUsage(fmt.Sprintf(MessageNotEnoughArguments, 1, ""))
}
Expand Down Expand Up @@ -57,6 +62,47 @@ func (cmd *JobDelete) Execute(args []string) (err error) {
cmd.out.Infof("To see whats happening, use: 'nerd job list'")
return nil
}
func (cmd *JobDelete) deleteAll() error {
kopts := cmd.KubeOpts
deps, err := NewDeps(cmd.Logger(), kopts)
if err != nil {
return renderConfigError(err, "failed to configure")
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, cmd.Timeout)
defer cancel()

s, err := cmd.out.Ask("Are you sure you want to delete all your jobs? (y/N)")
if err != nil {
return err
}
if !strings.HasPrefix(strings.ToLower(s), "y") {
return nil
}

kube := svc.NewKube(deps)
jobs, err := kube.ListJobs(ctx, &svc.ListJobsInput{})
if err != nil {
return renderServiceError(err, "failed to get all jobs")
}
if len(jobs.Items) == 0 {
cmd.out.Info("No job found.")
}
for _, job := range jobs.Items {
in := &svc.DeleteJobInput{
Name: job.Name,
}

_, err = kube.DeleteJob(ctx, in)
if err != nil {
return renderServiceError(err, fmt.Sprintf("failed to delete job `%s`", in.Name))
}

cmd.out.Infof("Deleted job: '%s'", in.Name)
}
return nil
}

// Description returns long-form help text
func (cmd *JobDelete) Description() string { return cmd.Synopsis() }
Expand Down

0 comments on commit 4e1d660

Please sign in to comment.