forked from paketo-buildpacks/bundle-install
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby_version_file_parser.go
More file actions
34 lines (27 loc) · 933 Bytes
/
ruby_version_file_parser.go
File metadata and controls
34 lines (27 loc) · 933 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
package bundleinstall
import (
"fmt"
"os"
"regexp"
"strings"
)
// RubyVersionFileParser parses the .ruby-version file to determine the
// version of Ruby used by the application.
type RubyVersionFileParser struct{}
// NewGemfileParser initializes an instance of RubyVersionFileParser.
func NewRubyVersionFileParser() RubyVersionFileParser {
return RubyVersionFileParser{}
}
// ParseVersion scans the .ruby-version file for a Ruby version specification.
func (p RubyVersionFileParser) ParseVersion(path string) (string, error) {
rVersion, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read .ruby-version file: %w", err)
}
re := regexp.MustCompile(versionNumberExpression)
rubyVersion := re.FindString(strings.TrimSpace(string(rVersion)))
if len(rubyVersion) == 0 {
return "", fmt.Errorf("no valid ruby version found in .ruby-version file: %s", rVersion)
}
return rubyVersion, nil
}