-
Notifications
You must be signed in to change notification settings - Fork 63
/
remove.go
71 lines (57 loc) · 1.5 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package remove
import (
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
networkapi "kraftkit.sh/api/network/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/iostreams"
"kraftkit.sh/machine/network"
)
type Rm struct {
driver string
}
func New() *cobra.Command {
cmd, err := cmdfactory.New(&Rm{}, cobra.Command{
Short: "Remove a network",
Use: "rm",
Aliases: []string{"remove", "delete", "del"},
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "net",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *Rm) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
return nil
}
func (opts *Rm) Run(cmd *cobra.Command, args []string) error {
var err error
ctx := cmd.Context()
strategy, ok := network.Strategies()[opts.driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.driver)
}
controller, err := strategy.NewNetworkV1alpha1(ctx)
if err != nil {
return err
}
if _, err := controller.Delete(ctx, &networkapi.Network{
ObjectMeta: metav1.ObjectMeta{
Name: args[0],
},
}); err != nil {
return err
}
fmt.Fprintln(iostreams.G(ctx).Out, args[0])
return nil
}