forked from paketo-buildpacks/bundler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemfile_lock_parser.go
More file actions
43 lines (34 loc) · 835 Bytes
/
gemfile_lock_parser.go
File metadata and controls
43 lines (34 loc) · 835 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
package bundler
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/Masterminds/semver"
)
type GemfileLockParser struct{}
func NewGemfileLockParser() GemfileLockParser {
return GemfileLockParser{}
}
func (p GemfileLockParser) ParseVersion(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("failed to parse Gemfile.lock: %w", err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == "BUNDLED WITH" {
if scanner.Scan() {
version, err := semver.NewVersion(strings.TrimSpace(scanner.Text()))
if err != nil {
return "", fmt.Errorf("failed to parse Gemfile.lock: %w", err)
}
return fmt.Sprintf("%d.*.*", version.Major()), nil
}
}
}
return "", nil
}