Skip to content

Commit

Permalink
Greater with at (#322)
Browse files Browse the repository at this point in the history
Added @>= to existing >= for version selection

---------

Co-authored-by: Sourabh Mehta <[email protected]>
  • Loading branch information
bgn42 and soumeh01 authored Jul 9, 2024
1 parent 1dcae3d commit 9ac237f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
6 changes: 5 additions & 1 deletion cmd/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ Add a pack using the following "<pack>" specification or using packs provided by
been released yet. This will install it as a local pack and
keep a reference in ".Local/local_repository.pidx".
The file can be a local file or a file hosted somewhere else on the Internet.
To select a specific version use: Vendor::[email protected]
To select the newest version of a major version use: Vendor::Pack@^x.y.z
To select any newer version use: Vendor::Pack@>=x.y.z
The file can be a local file or a file hosted somewhere else on the Internet.
If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/"
If "-f" is used, cpackget will call "cpackget pack add" on each URL specified in the <packs list> file.`,
Args: cobra.MinimumNArgs(0),
Expand Down
12 changes: 8 additions & 4 deletions cmd/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
)

var listCmdFlags struct {
// listUpdates tells whether listing all packs for which updates exist
listUpdates bool

// listPublic tells whether listing all packs in the public index
listPublic bool

Expand All @@ -21,13 +24,13 @@ var listCmdFlags struct {
}

var ListCmd = &cobra.Command{
Use: "list [--cached|--public]",
Use: "list [--cached|--public|--updates]",
Short: "List installed packs",
Long: "List all installed packs and optionally cached pack files",
Long: "List all installed packs and optionally cached packs or those for which updates are available",
Args: cobra.MaximumNArgs(0),
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {
return installer.ListInstalledPacks(listCmdFlags.listCached, listCmdFlags.listPublic, false, listCmdFlags.listFilter)
return installer.ListInstalledPacks(listCmdFlags.listCached, listCmdFlags.listPublic, listCmdFlags.listUpdates, false, listCmdFlags.listFilter)
},
}

Expand All @@ -38,13 +41,14 @@ var listRequiredCmd = &cobra.Command{
Args: cobra.MaximumNArgs(0),
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {
return installer.ListInstalledPacks(listCmdFlags.listCached, listCmdFlags.listPublic, true, listCmdFlags.listFilter)
return installer.ListInstalledPacks(listCmdFlags.listCached, listCmdFlags.listPublic, listCmdFlags.listUpdates, true, listCmdFlags.listFilter)
},
}

func init() {
ListCmd.Flags().BoolVarP(&listCmdFlags.listCached, "cached", "c", false, "list only cached packs")
ListCmd.Flags().BoolVarP(&listCmdFlags.listPublic, "public", "p", false, "list packs in the public index")
ListCmd.Flags().BoolVarP(&listCmdFlags.listUpdates, "updates", "u", false, "list packs which have newer versions")
ListCmd.Flags().StringVarP(&listCmdFlags.listFilter, "filter", "f", "", "filter results (case sensitive, accepts several expressions)")
ListCmd.AddCommand(listRequiredCmd)

Expand Down
8 changes: 4 additions & 4 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type PackType struct {
toBeRemoved bool

// exactVersion tells whether this pack identifier is specifying an exact version
// or is requesting a newer one, e.g. >=x.y.z
// or is requesting a newer one, e.g. @>=x.y.z
versionModifier int

// targetVersion is the most recent version of a pack in case exactVersion==true
Expand Down Expand Up @@ -485,7 +485,7 @@ func (p *PackType) extractEula(packPath string) error {
return os.WriteFile(eulaFileName, eulaContents, utils.FileModeRO)
}

// resolveVersionModifier takes into account eventual versionModifiers (@, @^ and >=) to determine
// resolveVersionModifier takes into account eventual versionModifiers (@, @^ and @>=) to determine
// which version of a pack should be targeted for installation
func (p *PackType) resolveVersionModifier(pdscXML *xml.PdscXML) {
log.Debugf("Resolving version modifier for \"%s\" using PDSC \"%s\"", p.path, pdscXML.FileName)
Expand All @@ -509,7 +509,7 @@ func (p *PackType) resolveVersionModifier(pdscXML *xml.PdscXML) {
log.Errorf("Tried to install at least version %s, highest available version is %s", p.Version, pdscXML.LatestVersion())
} else {
p.targetVersion = pdscXML.LatestVersion()
log.Debugf("- resolved(>=) as %s", p.targetVersion)
log.Debugf("- resolved(@>=) as %s", p.targetVersion)
}
return
}
Expand Down Expand Up @@ -638,7 +638,7 @@ func (p *PackType) PdscFileNameWithVersion() string {
}

// GetVersion makes sure to get the latest version for the pack
// after parsing possible version modifiers (@^, >=)
// after parsing possible version modifiers (@^, @>=)
func (p *PackType) GetVersion() string {
if p.versionModifier != utils.ExactVersion {
return p.targetVersion
Expand Down
20 changes: 14 additions & 6 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd
}

// ListInstalledPacks generates a list of all packs present in the pack root folder
func ListInstalledPacks(listCached, listPublic, listRequirements bool, listFilter string) error {
func ListInstalledPacks(listCached, listPublic, listUpdates, listRequirements bool, listFilter string) error {
log.Debugf("Listing packs")
if listPublic {
if listFilter != "" {
Expand Down Expand Up @@ -599,13 +599,21 @@ func ListInstalledPacks(listCached, listPublic, listRequirements bool, listFilte
}
}
} else {
if listRequirements {
log.Info("Listing installed packs with dependencies")
} else {
if listUpdates {
if listFilter != "" {
log.Infof("Listing installed packs, filtering by \"%s\"", listFilter)
log.Infof("Listing installed updateable packs, filtering by \"%s\"", listFilter)
} else {
log.Infof("Listing installed packs")
log.Infof("Listing installed updateable packs")
}
} else {
if listRequirements {
log.Info("Listing installed packs with dependencies")
} else {
if listFilter != "" {
log.Infof("Listing installed packs, filtering by \"%s\"", listFilter)
} else {
log.Infof("Listing installed packs")
}
}
}

Expand Down
27 changes: 14 additions & 13 deletions cmd/installer/root_pack_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
ListCached = true
ListFilter = ""
ListPublic = true
ListUpdates = true
ListRequirements = true
)

Expand All @@ -35,7 +36,7 @@ func ExampleListInstalledPacks() {
log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)

_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing installed packs
// I: (no packs installed)
Expand All @@ -49,7 +50,7 @@ func ExampleListInstalledPacks_emptyCache() {
log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)

_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing cached packs
// I: (no packs cached)
Expand All @@ -63,7 +64,7 @@ func ExampleListInstalledPacks_emptyPublicIndex() {
log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)

_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing packs from the public index
// I: (no packs in public index)
Expand Down Expand Up @@ -102,7 +103,7 @@ func ExampleListInstalledPacks_list() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing packs from the public index
// I: TheVendor::[email protected] (cached)
Expand Down Expand Up @@ -139,7 +140,7 @@ func ExampleListInstalledPacks_listCached() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing cached packs
// I: TheVendor::[email protected]
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestListInstalledPacks(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(io.Discard)
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, ListFilter))
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter))
stdout := buf.String()
assert.Contains(stdout, "I: Listing installed packs")
assert.Contains(stdout, fmt.Sprintf("I: TheVendor::[email protected] (installed via %s)", expectedPdscAbsPath))
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestListInstalledPacks(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(io.Discard)
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, ListFilter))
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter))
stdout := buf.String()
assert.Contains(stdout, "I: Listing installed packs")
assert.Contains(stdout, fmt.Sprintf("I: TheVendor::[email protected] (installed via %s)", expectedPdscAbsPath))
Expand All @@ -226,7 +227,7 @@ func TestListInstalledPacks(t *testing.T) {
assert.Nil(pdscXML.Read())
pdscXML.ReleasesTag.Releases[0].Version = "1.2.4"
assert.Nil(utils.WriteXML(pdscPath, pdscXML))
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, ListFilter))
assert.Nil(installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter))
stdout = buf.String()
assert.Contains(stdout, "I: Listing installed packs")
assert.Contains(stdout, fmt.Sprintf("I: TheVendor::[email protected] (installed via %s)", expectedPdscAbsPath))
Expand Down Expand Up @@ -264,7 +265,7 @@ func ExampleListInstalledPacks_listMalformedInstalledPacks() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, ListFilter)
_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, ListFilter)
// Output:
// I: Listing installed packs
// E: _TheVendor::[email protected] - error: pack version incorrect format
Expand Down Expand Up @@ -300,7 +301,7 @@ func ExampleListInstalledPacks_filter() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListRequirements, "1.2.4")
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListUpdates, !ListRequirements, "1.2.4")
// Output:
// I: Listing packs from the public index, filtering by "1.2.4"
// I: TheVendor::[email protected] (installed)
Expand Down Expand Up @@ -337,7 +338,7 @@ func ExampleListInstalledPacks_filterErrorPackages() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListRequirements, "TheVendor")
_ = installer.ListInstalledPacks(!ListCached, !ListPublic, !ListUpdates, !ListRequirements, "TheVendor")
// Output:
// I: Listing installed packs, filtering by "TheVendor"
// E: _TheVendor::[email protected] - error: pack version incorrect format
Expand Down Expand Up @@ -372,7 +373,7 @@ func ExampleListInstalledPacks_filterInvalidChars() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListRequirements, "@ :")
_ = installer.ListInstalledPacks(ListCached, ListPublic, !ListUpdates, !ListRequirements, "@ :")
// Output:
// I: Listing packs from the public index, filtering by "@ :"
}
Expand Down Expand Up @@ -406,7 +407,7 @@ func ExampleListInstalledPacks_filteradditionalMessages() {

log.SetOutput(os.Stdout)
defer log.SetOutput(io.Discard)
_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListRequirements, "(installed)")
_ = installer.ListInstalledPacks(ListCached, !ListPublic, !ListUpdates, !ListRequirements, "(installed)")
// Output:
// I: Listing cached packs, filtering by "(installed)"
}
14 changes: 8 additions & 6 deletions cmd/utils/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ var packFileNameRegex = regexp.MustCompile(packFileNamePattern)
// - Vendor::Pack
// - Vendor::[email protected]
// - Vendor::Pack@^x.y.z
// - Vendor::Pack@>=x.y.z
// - Vendor::Pack>=x.y.z
var dottedPackIDPattern = fmt.Sprintf(`^(?P<vendor>%s)\.(?P<pack>%s)(?:\.(?P<version>%s))?$`, namePattern, namePattern, versionPattern)
var legacyPackIDPattern = fmt.Sprintf(`^(?P<vendor>%s)::(?P<pack>%s)(?:(@|@\^|>=)(?P<version>%s|latest))?$`, namePattern, namePattern, versionPattern)
var legacyPackIDPattern = fmt.Sprintf(`^(?P<vendor>%s)::(?P<pack>%s)(?:(@|@\^|@>=|>=)(?P<version>%s|latest))?$`, namePattern, namePattern, versionPattern)
var packIDPattern = fmt.Sprintf(`(?:%s|%s)`, dottedPackIDPattern, legacyPackIDPattern)

// packIDRegex pre-compiles packIdPattern
Expand Down Expand Up @@ -126,7 +127,7 @@ const (
// Examples: Vendor::PackName, Vendor.PackName
AnyVersion = 2

// Example: Vendor::PackName>=x.y.z
// Example: Vendor::PackName@>=x.y.z
GreaterVersion = 3

// Example: Vendor::PackName@^x.y.z (the greatest version of the pack keeping the same major number)
Expand All @@ -137,9 +138,10 @@ const (
)

var versionModMap = map[string]int{
"@": ExactVersion,
"@^": GreatestCompatibleVersion,
">=": GreaterVersion,
"@": ExactVersion,
"@^": GreatestCompatibleVersion,
"@>=": GreaterVersion,
">=": GreaterVersion,
}

// PackInfo defines a basic pack information set
Expand Down Expand Up @@ -241,7 +243,7 @@ func ExtractPackInfo(packPath string) (PackInfo, error) {
info.VersionModifier = ExactVersion
}
} else if len(matches) == 5 {
// 5 matches: [Vendor::Pack(@|@^|>=)x.y.z, Vendor, Pack, (@|@^|>=), x.y.z] (legacy version)
// 5 matches: [Vendor::Pack(@|@^|@>=|>=)x.y.z, Vendor, Pack, (@|@^|@>=|>=), x.y.z] (legacy version)
versionModifier := matches[3]
version := matches[4]

Expand Down

0 comments on commit 9ac237f

Please sign in to comment.