-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
97 lines (85 loc) · 1.83 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
package cmd
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/verifa/horizon/pkg/hz"
"github.com/verifa/horizon/pkg/hzctl"
"sigs.k8s.io/yaml"
)
type deleteCmdOptions struct {
filename string
key string
}
var deleteOpts deleteCmdOptions
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Mark Horizon objects for deletion.",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
hCtx, err := config.Context(
hzctl.WithContextCurrent(true),
hzctl.WithContextValidate(hzctl.WithValidateSession(true)),
)
if err != nil {
return fmt.Errorf(
"obtaining current context: %w",
err,
)
}
clientDeleteOpts := []hzctl.DeleteOption{}
if deleteOpts.filename != "" {
yData, err := os.ReadFile(deleteOpts.filename)
if err != nil {
return fmt.Errorf("open file: %w", err)
}
jData, err := yaml.YAMLToJSONStrict(yData)
if err != nil {
return fmt.Errorf("convert yaml to json: %w", err)
}
clientDeleteOpts = append(
clientDeleteOpts,
hzctl.WithDeleteData(jData),
)
}
if deleteOpts.key != "" {
objKey, err := hz.ObjectKeyFromString(deleteOpts.key)
if err != nil {
return fmt.Errorf("parse key: %w", err)
}
clientDeleteOpts = append(
clientDeleteOpts,
hzctl.WithDeleteKey(objKey),
)
}
client := hzctl.Client{
Server: hCtx.URL,
Session: *hCtx.Session,
Manager: "hzctl",
}
ctx := context.Background()
if err := client.Delete(ctx, clientDeleteOpts...); err != nil {
return fmt.Errorf("delete: %w", err)
}
return nil
},
}
func init() {
rootCmd.AddCommand(deleteCmd)
flags := deleteCmd.Flags()
flags.StringVarP(
&deleteOpts.filename,
"filename",
"f",
"",
"Filename to delete",
)
flags.StringVarP(
&deleteOpts.key,
"key",
"k",
"",
"Key to delete",
)
}