Skip to content

Commit

Permalink
Merge pull request #30 from vsoch/consolidate-update-fuction
Browse files Browse the repository at this point in the history
consolidating update function
  • Loading branch information
vsoch committed Sep 5, 2021
2 parents 6b81953 + 94ac1e3 commit c5ac7da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 83 deletions.
48 changes: 48 additions & 0 deletions parsers/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package parsers

import (
"github.com/vsoch/uptodate/utils"
"regexp"
"strings"
)

// A Result object will store a path to some file that was changed, and
// an identifier for the parser, and some identifier for the changed file

Expand Down Expand Up @@ -28,3 +34,45 @@ type BuildVariable struct {

// VersionRegex matches a major and minor, optional third group (not semver)
var VersionRegex = "[0-9]+[.][0-9]+(?:[.][0-9]+)?"

func GetVersions(contenders []string, filters []string, startAtVersion string, skipVersions []string, includeVersions []string) []string {

// Final list of versions we will provide
versions := []string{}

// We look for tags based on filters (this is an OR between them)
filter := "(" + strings.Join(filters, "|") + ")"
isVersionRegex, _ := regexp.Compile(filter)

// Also don't add until we hit the start at version, given defined
doAdd := true
if startAtVersion != "" {
doAdd = false
}

// The tags should already be sorted
for _, version := range contenders {

// If it's in the list to include, include no matter what
if utils.IncludesString(version, includeVersions) {
versions = append(versions, version)
continue
}

// Have we hit the requested start version, and can add now?
if startAtVersion != "" && startAtVersion == version && !doAdd {
doAdd = true
}

// Is the tag in the list to skip?
if utils.IncludesString(version, skipVersions) {
continue
}

// If we are adding, great! Add here to our list
if doAdd && isVersionRegex.MatchString(version) {
versions = append(versions, version)
}
}
return versions
}
41 changes: 2 additions & 39 deletions parsers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package docker

import (
"fmt"
"regexp"
"strings"

lookout "github.com/alecbcs/lookout/update"
"github.com/vsoch/uptodate/parsers"
"github.com/vsoch/uptodate/utils"
)

Expand All @@ -19,44 +19,7 @@ func GetVersions(container string, filters []string, startAtVersion string, skip
response := utils.GetRequest(tagsUrl)
tags := strings.Split(response, "\n")

// We look for tags based on filters (this is an OR between them)
filter := "(" + strings.Join(filters, "|") + ")"
isVersionRegex, _ := regexp.Compile(filter)

// Derive list of those that match minimally a minor, major
versions := []string{}

// Also don't add until we hit the start at version, given defined
doAdd := true
if startAtVersion != "" {
doAdd = false
}

// The tags should already be sorted
for _, text := range tags {

// If it's in the list to include, include no matter what
if utils.IncludesString(text, includeVersions) {
versions = append(versions, text)
continue
}

// Have we hit the requested start version, and can add now?
if startAtVersion != "" && startAtVersion == text && !doAdd {
doAdd = true
}

// Is the tag in the list to skip?
if utils.IncludesString(text, skipVersions) {
continue
}

// If we are adding, great! Add here to our list
if doAdd && isVersionRegex.MatchString(text) {
versions = append(versions, text)
}
}
return versions
return parsers.GetVersions(tags, filters, startAtVersion, skipVersions, includeVersions)
}

// UpdateFrom updates a single From, and returns an Update
Expand Down
47 changes: 3 additions & 44 deletions parsers/spack/spack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package spack
// The spack Parser can parse Json from the spack packages repository

import (
"github.com/vsoch/uptodate/utils"
"regexp"
"github.com/vsoch/uptodate/parsers"
"sort"
"strings"
)

// A SpackAlias has a name and alias_for
Expand Down Expand Up @@ -64,54 +62,15 @@ type SpackDependency struct {
}

// Get Versions of a spack package relevant to a set of user preferences
// TODO this logic is too similar to the docker equivalent of GetVersions - should be one function
func (s *SpackPackage) GetVersions(filters []string, startAtVersion string, skipVersions []string, includeVersions []string) []string {

// Final list of versions we will provide
versions := []string{}
// Sort versions from earliest to latest
contenders := []string{}

// We look for tags based on filters (this is an OR between them)
filter := "(" + strings.Join(filters, "|") + ")"
isVersionRegex, _ := regexp.Compile(filter)

// Also don't add until we hit the start at version, given defined
doAdd := true
if startAtVersion != "" {
doAdd = false
}

// We will need to sort from earliest to latest
for _, version := range s.Versions {
contenders = append(contenders, version.Name)
}

// Sort from least to greatest
sort.Sort(sort.Reverse(sort.StringSlice(contenders)))

// The tags should already be sorted
for _, version := range contenders {

// If it's in the list to include, include no matter what
if utils.IncludesString(version, includeVersions) {
versions = append(versions, version)
continue
}

// Have we hit the requested start version, and can add now?
if startAtVersion != "" && startAtVersion == version && !doAdd {
doAdd = true
}

// Is the tag in the list to skip?
if utils.IncludesString(version, skipVersions) {
continue
}

// If we are adding, great! Add here to our list
if doAdd && isVersionRegex.MatchString(version) {
versions = append(versions, version)
}
}
return versions
return parsers.GetVersions(contenders, filters, startAtVersion, skipVersions, includeVersions)
}

0 comments on commit c5ac7da

Please sign in to comment.