-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
125 lines (109 loc) · 2.89 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package files
import (
"context"
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/prompt"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
)
func NewFilesDeleteCmd(f *factory.Factory) *cobra.Command {
opts := &FilesOptions{
Config: f.Config,
Out: f.IOOutWriter,
Appliance: f.Appliance,
}
deleteCmd := &cobra.Command{
Use: "delete",
Aliases: []string{"del", "remove", "rm"},
Short: docs.FilesDeleteDocs.Short,
Long: docs.FilesDeleteDocs.Long,
Example: docs.FilesDeleteDocs.ExampleString(),
ValidArgs: []string{"filename"},
Args: func(cmd *cobra.Command, args []string) error {
var errs *multierror.Error
var err error
opts.OrderBy, err = cmd.Flags().GetStringSlice("order-by")
if err != nil {
errs = multierror.Append(errs, err)
}
opts.Descending, err = cmd.Flags().GetBool("descending")
if err != nil {
errs = multierror.Append(errs, err)
}
return errs.ErrorOrNil()
},
RunE: func(cmd *cobra.Command, args []string) error {
a, err := opts.Appliance(f.Config)
if err != nil {
return err
}
ctx := context.Background()
var errs error
if len(args) > 0 {
for _, arg := range args {
if err := a.DeleteFile(ctx, arg); err != nil {
errs = multierror.Append(err, errs)
continue
}
fmt.Fprintf(opts.Out, "%s: deleted\n", arg)
}
return errs
}
allFlag, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
fileList, err := a.ListFiles(ctx, opts.OrderBy, opts.Descending)
if err != nil {
return err
}
if allFlag {
for _, file := range fileList {
if err := a.DeleteFile(ctx, file.GetName()); err != nil {
errs = multierror.Append(err, errs)
continue
}
fmt.Fprintf(opts.Out, "%s: deleted\n", file.GetName())
}
return errs
}
noInteractive, err := cmd.Flags().GetBool("no-interactive")
if err != nil {
return err
}
if !noInteractive {
fileNameList := []string{}
for _, file := range fileList {
fileNameList = append(fileNameList, file.GetName())
}
qs := &survey.MultiSelect{
PageSize: len(fileNameList),
Message: "select files to delete:",
Options: fileNameList,
}
selected := []string{}
if err := prompt.SurveyAskOne(qs, &selected); err != nil {
return err
}
if len(selected) <= 0 {
return errors.New("No files were selected for deletion")
}
for _, s := range selected {
if err := a.DeleteFile(ctx, s); err != nil {
errs = multierror.Append(err, errs)
continue
}
fmt.Fprintf(opts.Out, "%s: deleted\n", s)
}
return errs
}
return errors.New("No files were deleted")
},
}
deleteCmd.Flags().Bool("all", false, "delete all files from repository")
return deleteCmd
}