forked from elastic/package-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
73 lines (62 loc) · 2.01 KB
/
spec.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package spec
import (
"embed"
"fmt"
"io/fs"
"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v3"
)
//go:embed spec spec/integration/_dev spec/integration/data_stream/_dev spec/input/_dev
var content embed.FS
// FS returns an io/fs.FS for accessing the "package-spec/spec" contents.
func FS() fs.FS {
fs, err := fs.Sub(content, "spec")
if err != nil {
panic(err)
}
return fs
}
// CheckVersion checks if the given version is implemented by current spec. It returns
// the version of the spec matching with the given version.
func CheckVersion(version semver.Version) (*semver.Version, error) {
versions, err := VersionsInChangelog()
if err != nil {
return nil, err
}
for _, v := range versions {
// Ignore prereleases for comparison.
if cmpVersion, err := v.SetPrerelease(""); err != nil {
// This should never happen when setting an empty prerelease.
panic(err)
} else if cmpVersion.Equal(&version) {
return &v, nil
}
}
return nil, fmt.Errorf("spec version %q not found", version.String())
}
// VersionsInChangelog returns the list of versions defined in the changelog file.
func VersionsInChangelog() ([]semver.Version, error) {
d, err := fs.ReadFile(content, "spec/changelog.yml")
if err != nil {
return nil, fmt.Errorf("failed to read spec changelog: %w", err)
}
var entries []struct {
Version string `yaml:"version"`
}
err = yaml.Unmarshal(d, &entries)
if err != nil {
return nil, fmt.Errorf("failed to parse spec changelog: %w", err)
}
versions := make([]semver.Version, len(entries))
for i, entry := range entries {
semverVersion, err := semver.NewVersion(entry.Version)
if err != nil {
return nil, fmt.Errorf("failed to parse version (%s) in spec changelog: %w", entry.Version, err)
}
versions[i] = *semverVersion
}
return versions, nil
}