forked from hetznercloud/cli
/
image_disable_protection.go
58 lines (50 loc) · 1.43 KB
/
image_disable_protection.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
package cli
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)
func newImageDisableProtectionCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]",
Short: "Disable resource protection for an image",
Args: cobra.MinimumNArgs(2),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.ensureToken,
RunE: cli.wrap(runImageDisableProtection),
}
return cmd
}
func runImageDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error {
imageID, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("invalid image ID")
}
image := &hcloud.Image{ID: imageID}
var unknown []string
opts := hcloud.ImageChangeProtectionOpts{}
for _, arg := range args[1:] {
switch strings.ToLower(arg) {
case "delete":
opts.Delete = hcloud.Bool(false)
default:
unknown = append(unknown, arg)
}
}
if len(unknown) > 0 {
return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", "))
}
action, _, err := cli.Client().Image.ChangeProtection(cli.Context, image, opts)
if err != nil {
return err
}
if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}
fmt.Printf("Resource protection disabled for image %d\n", image.ID)
return nil
}