File: README.md

package info (click to toggle)
golang-github-aquasecurity-go-version 0.0~git20210121.637058c-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 256 kB
  • sloc: makefile: 5
file content (295 lines) | stat: -rw-r--r-- 10,676 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# go-version

![Test](https://github.com/aquasecurity/go-version/workflows/Test/badge.svg?branch=main)
[![Go Report Card](https://goreportcard.com/badge/github.com/aquasecurity/go-version)](https://goreportcard.com/report/github.com/aquasecurity/go-version)
![GitHub](https://img.shields.io/github/license/aquasecurity/go-version)

go-version is a library for parsing versions and version constraints, and verifying versions against a set of constraints.
go-version can sort a collection of versions properly, handles prerelease versions, etc.

go-version provides two packages:
- [semver](./pkg/semver)
    - [Semantic Versioning](https://semver.org/)
    - MAJOR.MINOR.PATCH-PRERELEASE+BUILDMETADATA (e.g. 1.1.3-alpha+110)
- [version](./pkg/version)
    - Semantic Versioning like versioning
    - Accept more than 3 numbers (e.g. 2.3.1.4)
    
# Table of Contents
- [semver](#semver)
  * [Parsing and Comparison](#semver-parsing-and-comparison)
  * [Sorting](#semver-sorting)
  * [Constraints](#semver-constraints)
    + [Pre-release](#semver-pre-release)
    + [Missing major/minor/patch versions](#missing-majorminorpatch-versions)
- [version](#version)
  * [Parsing, Comparison, and Sorting](#version-parsing-comparison-and-sorting)
  * [Constraints](#version-constraints)
    + [Pre-release](#pre-release)
    + [Zero Padding](#zero-padding)
 

## semver
Versions used with `semver` package must follow [Semantic Versioning](https://semver.org/).

### SemVer Parsing and Comparison
When two versions are compared using functions such as Compare, LessThan, and others, it will follow the specification and always include pre-releases within the comparison.
It will provide an answer that is valid with the comparison section of [the spec](https://semver.org/#spec-item-11).

See [example](./examples/semver/main.go)

```
v1, _ := semver.Parse("1.2.0")
v2, _ := semver.Parse("1.2.1")

// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
	fmt.Printf("%s is less than %s", v1, v2)
}
```

### SemVer Sorting
It follows [the spec](https://semver.org/#spec-item-11).

See [example](./examples/semver/main.go)

```
versionsRaw := []string{"1.1.0", "0.7.1", "1.4.0", "1.4.0-alpha", "1.4.1-beta", "1.4.0-alpha.2+20130313144700"}
versions := make([]semver.Version, len(versionsRaw))
for i, raw := range versionsRaw {
	v, _ := semver.Parse(raw)
	versions[i] = v
}

// After this, the versions are properly sorted
sort.Sort(semver.Collection(versions))
```

### SemVer Constraints
Comma-separated version constraints are considered an `AND`.
For example, ">= 1.2.3, < 2.0.0" means the version needs to be greater than or equal to 1.2 and less than 3.0.0.
In addition, they can be separated by `|| (OR)`.
For example, ">= 1.2.3, < 2.0.0 || > 4.0.0" means the version needs to be greater than or equal to 1.2 and less than 3.0.0, or greater than 4.0.0.


See [example](./examples/semver/main.go)

```
v, _ := semver.Parse("2.1.0")
c, _ := semver.NewConstraints(">= 1.0, < 1.4 || > 2.0")

if c.Check(v) {
	fmt.Printf("%s satisfies constraints '%s'", v, c)
}
```

Supported operators
- `=` : you accept that exact version
- `!=` : not equal
- `>` : you accept any version higher than the one you specify
- `>=` : you accept any version equal to or higher than the one you specify
- `<` : you accept any version lower to the one you specify
- `<=` : you accept any version equal or lower to the one you specify
- `^` : it will only do updates that do not change the leftmost non-zero number.
    - e.g. `^1.2.3` := `>=1.2.3, <2.0.0`
- `~` : allows patch-level changes if a minor version is specified on the comparator. Allows minor-level changes if not.
    - e.g. `~1.2.3` := `>=1.2.3, <1.3.0`
    
`^` and `~` work like `npm`. See [here](https://docs.npmjs.com/cli/v6/using-npm/semver).

As for version constraints, there are a few caveats since the constraints are not part of [the specification](https://semver.org/).
- Pre-release
- Missing major/minor/patch versions


#### SemVer Pre-release
A pre-release version may be denoted by appending a hyphen and a series of dot separated identifiers immediately following the patch version.
Pre-release versions have a lower precedence than the associated normal version (e.g. 1.2.3-alpha < 1.2.3).
A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version.

`semver` comparisons using constraints without a prerelease comparator will skip prerelease versions.
For example, >=1.2.3 will skip pre-releases when looking at a list of releases.

In the following example, `2.1.0-alpha` looks greater than `2.0.0`, but `c.Check(v)` returns false.

```
v, _ := semver.Parse("2.1.0-alpha")
c, _ := semver.NewConstraints(">= 2.0.0")

c.Check(v) // false
```

Constraints with a prerelease comparator will include prerelease versions.

```
v, _ := semver.Parse("2.1.0-alpha")
c, _ := semver.NewConstraints(">= 2.0.0-alpha")

c.Check(v) // true
```

Note that this is different from the behavior of npm.
`>= 2.0.0-alpha` allows pre-releases in the 2.0.0 version only, if they are greater than or equal to alpha.
So, 2.0.0-beta would be allowed, while 2.1.0-alpha would not.
You can use [go-npm-version](https://github.com/aquasecurity/go-npm-version) for npm version comparion.
It strictly follows the npm rules.

If you want to include pre-releases even with no pre-releases constraint, you can pass `semver.WithPreRelease(true)` as an argument of `semver.NewConstraints`

```
v, _ := semver.Parse("2.1.0-alpha")
c, _ := semver.NewConstraints(">= 2.0.0", semver.WithPreRelease(true))

c.Check(v) // true
```

#### Missing major/minor/patch versions 
If some of major/minor/patch versions are not specified, it is treated as `*` by default.
In short, `3.1.3` satisfies `= 3` because `= 3` is converted to `= 3.*.*`.

```
v, _ := semver.Parse("2.3.4")
c, _ := semver.NewConstraints("= 2")

c.Check(v) // true
```

Then, `2.2.3` doesn't satisfy `> 2` as `> 2` is treated as `> 2.*.*` = `>= 3.0.0`

```
v, _ := semver.Parse("2.2.3")
c, _ := semver.NewConstraints("> 2")

c.Check(v) // false
```

`3.3.9` satisifies `= 3.3`, and `5.1.2` doesn't satisfy `> 5.1` likewise.

If you want to treat them as 0, you can pass `semver.WithZeroPadding(true)` as an argument of `semver.NewConstraints`

```
v, _ := semver.Parse("2.3.4")
c, _ := semver.NewConstraints("= 2", semver.WithZeroPadding(true))

c.Check(v) // false
```

## version
Versions used with `version` package follows [Semantic Versioning](https://semver.org/) like versioning.
It accepts more than 3 numbers such as `2.2.4.3`.

It works almost as well as `semver` package. It also accepts pre-release and build metadata.

### Version Parsing, Comparison, and Sorting
When two versions are compared using functions such as Compare, LessThan, and others, it will follow the specification and always include pre-releases within the comparison.

See [example](./examples/version/main.go)

```
v1, _ := version.Parse("1.2.0.9-alpha")
v2, _ := version.Parse("1.2.1.0+11")

// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
	fmt.Printf("%s is less than %s\n", v1, v2)
}
```

It also supports version sorting.

### Version Constraints
It is almost the same as `semver` package, but there are some differences.

See [example](./examples/version/main.go)

```
v, _ := version.Parse("2.1.0.1-alpha")
c, _ := version.NewConstraints(">= 1.0, < 1.4 || > 2.1")

if c.Check(v) {
	fmt.Printf("%s satisfies constraints '%s'", v, c)
}
```

Supported operators:
- `=` : you accept that exact version
- `!=` : not equal
- `>` : you accept any version higher than the one you specify
- `>=` : you accept any version equal to or higher than the one you specify
- `<` : you accept any version lower to the one you specify
- `<=` : you accept any version equal or lower to the one you specify
- `^` : it will only do updates that do not change the leftmost non-zero number.
    - e.g. `^1.2.3` := `>=1.2.3, <2.0.0`
- `~` : allows patch-level changes if a minor version is specified on the comparator. Allows minor-level changes if not.
    - e.g. `~1.2.3` := `>=1.2.3, <1.3.0`
- `~>` : you accept any version equal to or greater than in the last digit
    - e.g. `~>3.0.3` := `>= 3.0.3, < 3.1`
    
**NOTE** : `version` package doesn't support wildcards such as `x`, `X`, and `*`.

#### Pre-release
Unlike the `semver` package, `version` package always includes pre-release versions even with no pre-releases constraint.

```
v, _ := version.Parse("2.1.0.1-alpha")
c, _ := version.NewConstraints("> 2.0.0")

c.Check(v) // true
```

#### Zero Padding
Unlike the `semver` package, `version` package fills in the missing versions with 0.
In short, `3.1.3` doesn't satisfy `= 3` because `= 3` is converted to `= 3.0.0`.

```
v, _ := version.Parse("3.1.3")
c, _ := version.NewConstraints("= 3")

c.Check(v) // false
```

## Constraints

### Wildcards
The x, X, and * characters can be used as a wildcard character.
This works for all comparison operators.
When used on the = operator it falls back to the patch level comparison (see tilde below). For example,

- `2.0.x` := `>= 2.0.0, < 2.1.0`
- `>= 1.2.x` := `>= 1.2.0`
- `<= 3.x` := `< 4.0.0`
- `*` := `>= 0.0.0`

### Tilde Range
Allows patch-level changes if a minor version is specified on the comparator. Allows minor-level changes if not.
If it consists of more than 3 numbers, it allows changes that increases the last revision number. 


- `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0`
- `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0` (Same as `1.2.x`)
- `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0` (Same as `1.x`)
- `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0`
- `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0` (Same as `0.2.x`)
- `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0` (Same as `0.x`)
- `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0`
- `~0.0.0.4` := `>=0.0.0.4 <0.0.1`

### Caret Range
Allows changes that do not modify the left-most non-zero digit.

- `^1.2.3` := `>=1.2.3 <2.0.0`
- `^0.2.3` := `>=0.2.3 <0.3.0`
- `^0.0.3` := `>=0.0.3 <0.0.4`
- `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0`
- `^0.0.0.4` := `>=0.0.0.4 <0.0.0.5`

### Pessimistic Range
It means the version must be equal to or greater than in the last digit.

- `~>1.2.3` := `>=1.2.3 <1.3.0`
- `~>1.2` := `>=1.2.0 <2.0` (*different from `~`*)
- `~>1` := `>=1.0.0 <2.0.0`
- `~>1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0`
- `~>0.0.0.4` := `>=0.0.0.4 <0.0.1`