forked from Masterminds/glide
/
gpm.go
110 lines (95 loc) · 2.5 KB
/
gpm.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
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
package cmd
import (
"bufio"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/glide/cfg"
)
// This file contains commands for working with GPM/GVP.
// HasGPMGodeps indicates whether a Godeps file exists.
func HasGPMGodeps(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
dir := cookoo.GetString("dir", "", p)
path := filepath.Join(dir, "Godeps")
_, err := os.Stat(path)
return err == nil, nil
}
// GPMGodeps parses a GPM-flavored Godeps file.
//
// Params
// - dir (string): Directory root.
//
// Returns an []*cfg.Dependency
func GPMGodeps(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
dir := cookoo.GetString("dir", "", p)
return parseGPMGodeps(dir)
}
func parseGPMGodeps(dir string) ([]*cfg.Dependency, error) {
path := filepath.Join(dir, "Godeps")
if i, err := os.Stat(path); err != nil {
return []*cfg.Dependency{}, nil
} else if i.IsDir() {
Info("Godeps is a directory. This is probably a Godep project.\n")
return []*cfg.Dependency{}, nil
}
Info("Found Godeps file.\n")
buf := []*cfg.Dependency{}
file, err := os.Open(path)
if err != nil {
return buf, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
parts, ok := parseGodepsLine(scanner.Text())
if ok {
dep := &cfg.Dependency{Name: parts[0]}
if len(parts) > 1 {
dep.Reference = parts[1]
}
buf = append(buf, dep)
}
}
if err := scanner.Err(); err != nil {
Warn("Scan failed: %s\n", err)
return buf, err
}
return buf, nil
}
// GPMGodepsGit reads a Godeps-Git file for gpm-git.
func GPMGodepsGit(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
dir := cookoo.GetString("dir", "", p)
path := filepath.Join(dir, "Godeps-Git")
if _, err := os.Stat(path); err != nil {
return []*cfg.Dependency{}, nil
}
Info("Found Godeps-Git file.\n")
buf := []*cfg.Dependency{}
file, err := os.Open(path)
if err != nil {
return buf, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
parts, ok := parseGodepsLine(scanner.Text())
if ok {
dep := &cfg.Dependency{Name: parts[1], Repository: parts[0]}
if len(parts) > 2 {
dep.Reference = parts[2]
}
buf = append(buf, dep)
}
}
if err := scanner.Err(); err != nil {
Warn("Scan failed: %s\n", err)
return buf, err
}
return buf, nil
}
func parseGodepsLine(line string) ([]string, bool) {
line = strings.TrimSpace(line)
if len(line) == 0 || strings.HasPrefix(line, "#") {
return []string{}, false
}
return strings.Fields(line), true
}