-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_version_parser.go
More file actions
44 lines (33 loc) · 876 Bytes
/
node_version_parser.go
File metadata and controls
44 lines (33 loc) · 876 Bytes
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
package nodeengine
import (
"fmt"
"os"
"strings"
"github.com/Masterminds/semver/v3"
)
type NodeVersionParser struct{}
func NewNodeVersionParser() NodeVersionParser {
return NodeVersionParser{}
}
func (p NodeVersionParser) ParseVersion(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
version, err := p.validateNodeVersion(string(content))
if err != nil {
return "", err
}
return version, nil
}
func (p NodeVersionParser) validateNodeVersion(content string) (string, error) {
content = strings.TrimSpace(strings.ToLower(content))
content = strings.TrimPrefix(content, "v")
if _, err := semver.NewConstraint(content); err != nil {
return "", fmt.Errorf("invalid version constraint specified in .node-version: %q", content)
}
return content, nil
}