/
project.go
121 lines (101 loc) · 3.06 KB
/
project.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
package delete
import (
"context"
"errors"
"fmt"
"slices"
"connectrpc.com/connect"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/cli/client"
"github.com/akuity/kargo/internal/cli/config"
"github.com/akuity/kargo/internal/cli/io"
"github.com/akuity/kargo/internal/cli/kubernetes"
"github.com/akuity/kargo/internal/cli/option"
"github.com/akuity/kargo/internal/cli/templates"
v1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)
type deleteProjectOptions struct {
genericiooptions.IOStreams
*genericclioptions.PrintFlags
Config config.CLIConfig
ClientOptions client.Options
Names []string
}
func newProjectCommand(cfg config.CLIConfig, streams genericiooptions.IOStreams) *cobra.Command {
cmdOpts := &deleteProjectOptions{
Config: cfg,
IOStreams: streams,
PrintFlags: genericclioptions.NewPrintFlags("deleted").WithTypeSetter(kubernetes.GetScheme()),
}
cmd := &cobra.Command{
Use: "project (NAME ...)",
Short: "Delete project by name",
Args: option.MinimumNArgs(1),
Example: templates.Example(`
# Delete a project
kargo delete project my-project
# Delete multiple projects
kargo delete project my-project1 my-project2
`),
RunE: func(cmd *cobra.Command, args []string) error {
cmdOpts.complete(args)
if err := cmdOpts.validate(); err != nil {
return err
}
return cmdOpts.run(cmd.Context())
},
}
// Register the option flags on the command.
cmdOpts.addFlags(cmd)
// Set the input/output streams for the command.
io.SetIOStreams(cmd, cmdOpts.IOStreams)
return cmd
}
// addFlags adds the flags for the delete project options to the provided
// command.
func (o *deleteProjectOptions) addFlags(cmd *cobra.Command) {
o.ClientOptions.AddFlags(cmd.PersistentFlags())
o.PrintFlags.AddFlags(cmd)
}
// complete sets the options from the command arguments.
func (o *deleteProjectOptions) complete(args []string) {
o.Names = slices.Compact(args)
}
// validate performs validation of the options. If the options are invalid, an
// error is returned.
func (o *deleteProjectOptions) validate() error {
if len(o.Names) == 0 {
return errors.New("name is required")
}
return nil
}
// run removes the project(s) based on the options.
func (o *deleteProjectOptions) run(ctx context.Context) error {
kargoSvcCli, err := client.GetClientFromConfig(ctx, o.Config, o.ClientOptions)
if err != nil {
return fmt.Errorf("get client from config: %w", err)
}
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return fmt.Errorf("create printer: %w", err)
}
var errs []error
for _, name := range o.Names {
if _, err := kargoSvcCli.DeleteProject(ctx, connect.NewRequest(&v1alpha1.DeleteProjectRequest{
Name: name,
})); err != nil {
errs = append(errs, err)
continue
}
_ = printer.PrintObj(&kargoapi.Project{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}, o.IOStreams.Out)
}
return errors.Join(errs...)
}