/
app_remove.go
51 lines (44 loc) · 1.37 KB
/
app_remove.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
package main
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
ketchv1 "github.com/theketchio/ketch/internal/api/v1beta1"
"github.com/theketchio/ketch/internal/validation"
)
const appRemoveHelp = `
Remove an application.
`
type appRemoveFn func(context.Context, config, string, io.Writer) error
func newAppRemoveCmd(cfg config, out io.Writer, appRemove appRemoveFn) *cobra.Command {
cmd := &cobra.Command{
Use: "remove APPNAME",
Short: "Remove an application.",
Args: cobra.ExactValidArgs(1),
Long: appRemoveHelp,
RunE: func(cmd *cobra.Command, args []string) error {
appName := args[0]
if !validation.ValidateName(appName) {
return ErrInvalidAppName
}
return appRemove(cmd.Context(), cfg, appName, out)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return autoCompleteAppNames(cfg, toComplete)
},
}
return cmd
}
func appRemove(ctx context.Context, cfg config, appName string, out io.Writer) error {
app := ketchv1.App{}
if err := cfg.Client().Get(ctx, types.NamespacedName{Name: appName}, &app); err != nil {
return fmt.Errorf("failed to get app: %w", err)
}
if err := cfg.Client().Delete(ctx, &app); err != nil {
return fmt.Errorf("failed to delete app: %w", err)
}
fmt.Fprintln(out, "Successfully removed!")
return nil
}