-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
annotate.go
128 lines (113 loc) · 5.05 KB
/
annotate.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
126
127
128
package manifest
import (
"errors"
"fmt"
"strings"
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v5/cmd/podman/common"
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/spf13/cobra"
)
// manifestAnnotateOptsWrapper wraps entities.ManifestAnnotateOptions and
// prevents us from having to add CLI-only fields to the API types.
type manifestAnnotateOptsWrapper struct {
entities.ManifestAnnotateOptions
annotations []string
index bool
}
var (
manifestAnnotateOpts = manifestAnnotateOptsWrapper{}
annotateCmd = &cobra.Command{
Use: "annotate [options] LIST IMAGEORARTIFACT",
Short: "Add or update information about an entry in a manifest list or image index",
Long: "Adds or updates information about an entry in a manifest list or image index.",
RunE: annotate,
Args: cobra.RangeArgs(1, 2),
Example: `podman manifest annotate --annotation left=right mylist:v1.11 sha256:15352d97781ffdf357bf3459c037be3efac4133dc9070c2dce7eca7c05c3e736`,
ValidArgsFunction: common.AutocompleteImages,
}
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: annotateCmd,
Parent: manifestCmd,
})
flags := annotateCmd.Flags()
annotationFlagName := "annotation"
flags.StringArrayVar(&manifestAnnotateOpts.annotations, annotationFlagName, nil, "set an `annotation` for the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(annotationFlagName, completion.AutocompleteNone)
archFlagName := "arch"
flags.StringVar(&manifestAnnotateOpts.Arch, archFlagName, "", "override the `architecture` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(archFlagName, completion.AutocompleteArch)
featuresFlagName := "features"
flags.StringSliceVar(&manifestAnnotateOpts.Features, featuresFlagName, nil, "override the `features` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(featuresFlagName, completion.AutocompleteNone)
indexFlagName := "index"
flags.BoolVar(&manifestAnnotateOpts.index, indexFlagName, false, "apply --"+annotationFlagName+" values to the image index itself")
osFlagName := "os"
flags.StringVar(&manifestAnnotateOpts.OS, osFlagName, "", "override the `OS` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(osFlagName, completion.AutocompleteOS)
osFeaturesFlagName := "os-features"
flags.StringSliceVar(&manifestAnnotateOpts.OSFeatures, osFeaturesFlagName, nil, "override the OS `features` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(osFeaturesFlagName, completion.AutocompleteNone)
osVersionFlagName := "os-version"
flags.StringVar(&manifestAnnotateOpts.OSVersion, osVersionFlagName, "", "override the OS `version` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(osVersionFlagName, completion.AutocompleteNone)
variantFlagName := "variant"
flags.StringVar(&manifestAnnotateOpts.Variant, variantFlagName, "", "override the `Variant` of the specified image or artifact")
_ = annotateCmd.RegisterFlagCompletionFunc(variantFlagName, completion.AutocompleteNone)
subjectFlagName := "subject"
flags.StringVar(&manifestAnnotateOpts.IndexSubject, subjectFlagName, "", "set the `subject` to which the image index refers")
_ = annotateCmd.RegisterFlagCompletionFunc(subjectFlagName, completion.AutocompleteNone)
}
func annotate(cmd *cobra.Command, args []string) error {
var listImageSpec, instanceSpec string
switch len(args) {
case 1:
listImageSpec = args[0]
if listImageSpec == "" {
return fmt.Errorf(`invalid image name "%s"`, args[0])
}
if !manifestAnnotateOpts.index {
return errors.New(`expected an instance digest, image name, or artifact name`)
}
case 2:
listImageSpec = args[0]
if listImageSpec == "" {
return fmt.Errorf(`invalid image name "%s"`, args[0])
}
if manifestAnnotateOpts.index {
return fmt.Errorf(`did not expect image or artifact name "%s" when modifying the entire index`, args[1])
}
instanceSpec = args[1]
if instanceSpec == "" {
return fmt.Errorf(`invalid instance digest, image name, or artifact name "%s"`, instanceSpec)
}
default:
return errors.New("expected either a list name and --index or a list name and an image digest or image name or artifact name")
}
opts := manifestAnnotateOpts.ManifestAnnotateOptions
var annotations map[string]string
for _, annotation := range manifestAnnotateOpts.annotations {
k, v, parsed := strings.Cut(annotation, "=")
if !parsed {
return fmt.Errorf("expected --annotation %q to be in key=value format", annotation)
}
if annotations == nil {
annotations = make(map[string]string)
}
annotations[k] = v
}
if manifestAnnotateOpts.index {
opts.IndexAnnotations = annotations
} else {
opts.Annotations = annotations
}
id, err := registry.ImageEngine().ManifestAnnotate(registry.Context(), args[0], args[1], opts)
if err != nil {
return err
}
fmt.Println(id)
return nil
}