-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
106 lines (93 loc) · 2.81 KB
/
version.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
// Copyright © 2019 tinyzimmer
//
// This file is part of protobuf-registry
//
// protobuf-registry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// protobuf-registry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with protobuf-registry. If not, see <https://www.gnu.org/licenses/>.
package common
import (
"fmt"
"sort"
version "github.com/hashicorp/go-version"
"github.com/tinyzimmer/protobuf-registry/pkg/protobuf"
)
func GetVersionFromProtoSlice(protos []*protobuf.Protobuf, version string) (*protobuf.Protobuf, error) {
var protoInstance *protobuf.Protobuf
for _, x := range protos {
if *x.Version == version {
protoInstance = x
}
}
if protoInstance == nil {
return nil, fmt.Errorf("version %s not found", version)
}
return protoInstance, nil
}
func GetLatestVersion(protos []*protobuf.Protobuf) (latest *protobuf.Protobuf) {
// if there is only one, return it
if len(protos) == 1 {
latest = protos[0]
return
}
var latestVersion *version.Version
for _, proto := range protos {
// if latest isn't set yet - set it to this instance and continue
if latest == nil {
var err error
if latestVersion, err = version.NewVersion(*proto.Version); err != nil {
continue
}
latest = proto
continue
}
// check if this instance is newer than currently set latest
if v, err := version.NewVersion(*proto.Version); err != nil {
continue
} else {
if v.GreaterThan(latestVersion) {
latestVersion = v
latest = proto
}
}
}
return
}
func SortVersions(protos []*protobuf.Protobuf) (sorted []*protobuf.Protobuf) {
// make slices to hold raw versions and invalid objects
invalid := make([]*protobuf.Protobuf, 0)
versions := make([]*version.Version, 0)
// populate the versions and invalid slices
for _, proto := range protos {
v, err := version.NewVersion(*proto.Version)
if err != nil {
invalid = append(invalid, proto)
} else {
versions = append(versions, v)
}
}
// sort the versions slice in descending order
sort.Sort(sort.Reverse(version.Collection(versions)))
// make a new slice for sorted results
sorted = make([]*protobuf.Protobuf, 0)
// rebuild proto slice from sorted version slice
for _, v := range versions {
for _, x := range protos {
if v.Original() == *x.Version {
sorted = append(sorted, x)
}
}
}
// add invalid versions to the end of the list
sorted = append(sorted, invalid...)
return
}