-
Notifications
You must be signed in to change notification settings - Fork 63
/
remove.go
125 lines (104 loc) · 3.02 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
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
122
123
124
125
// 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 except in compliance with the License.
package remove
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/packmanager"
)
type RemoveOptions struct {
Name string `long:"name" short:"n" usage:"Specify the package name that has to be pruned" default:""`
All bool `long:"all" short:"a" usage:"Prunes all the packages available on the host machine"`
Format string `long:"format" short:"f" usage:"Set the package format." default:"any"`
}
// Remove a Unikraft component.
func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error {
if opts == nil {
opts = &RemoveOptions{}
}
return opts.Run(ctx, args)
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&RemoveOptions{}, cobra.Command{
Short: "Removes selected local packages",
Use: "remove [FLAGS] [PACKAGE]",
Args: cobra.ArbitraryArgs,
Aliases: []string{"rm"},
Long: "Remove a Unikraft component.",
Example: heredoc.Doc(`
# Remove all packages
kraft pkg remove --all
# Remove only select OCI index packages
kraft pkg remove --format=oci unikraft.org/nginx:latest
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "pkg",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
if len(args) == 0 && opts.Name == "" && !opts.All {
return fmt.Errorf("package name is not specified to remove or --all flag")
} else if opts.All && (len(args) > 0 || opts.Name != "") {
return fmt.Errorf("package name and --all flags cannot be specified at once")
}
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
umbrella, err := packmanager.PackageManagers()
if err != nil {
panic(err)
}
if opts.Format != "any" {
var available []string
found := false
for _, pm := range umbrella {
available = append(available, pm.Format().String())
if pm.Format().String() == opts.Format {
found = true
}
}
if !found {
return fmt.Errorf("unknown package format '%s' from choice of %v", opts.Format, available)
}
}
return nil
}
func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
umbrella, err := packmanager.PackageManagers()
if err != nil {
return fmt.Errorf("could not get registered package managers: %w", err)
}
for _, pm := range umbrella {
if opts.Format != "any" && opts.Format != pm.Format().String() {
continue
}
if opts.All {
if err = pm.Delete(ctx,
packmanager.WithAll(opts.All),
); err != nil {
return err
}
} else {
for _, arg := range args {
if err := pm.Delete(ctx,
packmanager.WithName(arg),
); err != nil {
return fmt.Errorf("could not complete catalog query: %w", err)
}
}
}
}
return nil
}