/
cmd.go
173 lines (148 loc) · 3.92 KB
/
cmd.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package show
import (
"github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
ocmcommon "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/repooption"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/out"
"github.com/open-component-model/ocm/pkg/semverutils"
)
var (
Names = names.Versions
Verb = verbs.Show
)
type Command struct {
utils.BaseCommand
Latest bool
Semantic bool
Ref string
Constraints []*semver.Constraints
}
// NewCommand creates a new ocm command.
func NewCommand(ctx clictx.Context, names ...string) *cobra.Command {
return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx,
repooption.New(),
)}, utils.Names(Names, names...)...)
}
func (o *Command) ForName(name string) *cobra.Command {
return &cobra.Command{
Use: "[<options>] <component> {<version pattern>}",
Args: cobra.MinimumNArgs(1),
Short: "show dedicated versions (semver compliant)",
Long: `
Match versions of a component against some patterns.
`,
Example: `
$ ocm show versions ghcr.io/mandelsoft/cnudie//github.com/mandelsoft/playground
`,
}
}
func (o *Command) AddFlags(fs *pflag.FlagSet) {
o.BaseCommand.AddFlags(fs)
fs.BoolVarP(&o.Latest, "latest", "l", false, "show only latest version")
fs.BoolVarP(&o.Semantic, "semantic", "s", false, "show semantic version")
}
func (o *Command) Complete(args []string) error {
o.Ref = args[0]
for _, v := range args[1:] {
c, err := semver.NewConstraint(v)
if err != nil {
return err
}
o.Constraints = append(o.Constraints, c)
}
return nil
}
func (o *Command) Run() error {
session := ocm.NewSession(nil)
defer session.Close()
err := o.ProcessOnOptions(ocmcommon.CompleteOptionsWithSession(o, session))
if err != nil {
return err
}
versions := Versions{}
repo := repooption.From(o)
var cv ocm.ComponentVersionAccess
var comp ocm.ComponentAccess
// determine version source
if repo.Repository != nil {
cr, err := ocm.ParseComp(o.Ref)
if err != nil {
return err
}
comp, err = session.LookupComponent(repo.Repository, cr.Component)
if err != nil {
return err
}
if cr.IsVersion() {
cv, err = session.GetComponentVersion(comp, *cr.Version)
if err != nil {
return err
}
}
} else {
r, err := session.EvaluateVersionRef(o.Context.OCMContext(), o.Ref)
if err != nil {
return err
}
if r.Component == nil {
return errors.Newf("no component specified")
}
comp = r.Component
cv = r.Version
}
// determine version base set
if cv != nil {
v, err := semver.NewVersion(cv.GetVersion())
if err != nil {
return err
}
versions = append(versions, v)
} else {
vers, err := comp.ListVersions()
if err != nil {
return err
}
for _, vn := range vers {
v, err := semver.NewVersion(vn)
if err == nil {
versions = append(versions, v)
}
}
}
versions = semverutils.MatchVersions(versions, o.Constraints...)
if len(versions) > 1 && o.Latest {
versions = versions[len(versions)-1:]
}
for _, r := range versions {
if o.Semantic {
out.Outf(o, "%s\n", r)
} else {
out.Outf(o, "%s\n", r.Original())
}
}
return nil
}
type Versions = semver.Collection
/*
var _ sort.Interface = (Versions)(nil)
func (v Versions) Len() int {
return len(v)
}
func (v Versions) Less(i, j int) bool {
return v[i].Compare(v[j])<0
}
func (v Versions) Swap(i, j int) {
v[i],v[j]=v[j],v[i]
}
*/