Repository navigation
Expand file tree
/
Copy pathyaml.go
More file actions
196 lines (168 loc) · 4.91 KB
/
Copy pathyaml.go
File metadata and controls
196 lines (168 loc) · 4.91 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/util"
)
// ParseYaml parses the glide.yaml format and returns a Configuration object.
//
// Params:
// - filename (string): YAML filename as a string
//
// Returns:
// - *cfg.Config: The configuration.
func ParseYaml(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
fname := p.Get("filename", "glide.yaml").(string)
//conf := new(Config)
yml, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
conf, err := cfg.ConfigFromYaml(yml)
if err != nil {
return nil, err
}
return conf, nil
}
// ParseYamlString parses a YAML string. This is similar but different to
// ParseYaml that parses an external file.
//
// Params:
// - yaml (string): YAML as a string.
//
// Returns:
// - *cfg.Config: The configuration.
func ParseYamlString(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
yamlString := p.Get("yaml", "").(string)
conf, err := cfg.ConfigFromYaml([]byte(yamlString))
if err != nil {
return nil, err
}
return conf, nil
}
<
8000
div>
// GuardYaml protects the glide yaml file from being overwritten.
func GuardYaml(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
fname := p.Get("filename", "glide.yaml").(string)
if _, err := os.Stat(fname); err == nil {
cwd, _ := os.Getwd()
return false, fmt.Errorf("Cowardly refusing to overwrite %s in %s", fname, cwd)
}
return true, nil
}
// WriteYaml writes the config as YAML.
//
// Params:
// - conf: A *cfg.Config to render.
// - out (io.Writer): An output stream to write to. Default is os.Stdout.
// - filename (string): If set, the file will be opened and the content will be written to it.
func WriteYaml(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
conf := p.Get("conf", nil).(*cfg.Config)
toStdout := p.Get("toStdout", true).(bool)
data, err := conf.Marshal()
if err != nil {
return nil, err
}
var out io.Writer
if nn, ok := p.Has("filename"); ok && len(nn.(string)) > 0 {
file, err := os.Create(nn.(string))
if err != nil {
}
defer file.Close()
out = io.Writer(file)
//fmt.Fprint(out, yml)
out.Write(data)
} else if toStdout {
out = p.Get("out", os.Stdout).(io.Writer)
//fmt.Fprint(out, yml)
out.Write(data)
}
// Otherwise we supress output.
return true, nil
}
// WriteLock writes the lock as YAML.
//
// Params:
// - lockfile: A *cfg.Lockfile to render.
// - out (io.Writer): An output stream to write to. Default is os.Stdout.
func WriteLock(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
skip := p.Get("skip", false).(bool)
if skip {
return false, nil
}
lockfile := p.Get("lockfile", nil).(*cfg.Lockfile)
Info("Writing glide.lock file")
data, err := lockfile.Marshal()
if err != nil {
return nil, err
}
var out io.Writer
file, err := os.Create("glide.lock")
if err != nil {
return false, err
}
defer file.Close()
out = io.Writer(file)
out.Write(data)
return true, nil
}
// AddDependencies adds a list of *Dependency objects to the given *cfg.Config.
//
// This is used to merge in packages from other sources or config files.
func AddDependencies(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
deps := p.Get("dependencies", []*cfg.Dependency{}).([]*cfg.Dependency)
config := p.Get("conf", nil).(*cfg.Config)
// Make a set of existing package names for quick comparison.
pkgSet := make(map[string]bool, len(config.Imports))
for _, p := range config.Imports {
pkgSet[p.Name] = true
}
// If a dep is not already present, add it.
for _, dep := range deps {
if _, ok := pkgSet[dep.Name]; ok {
Warn("Package %s is already in glide.yaml. Skipping.\n", dep.Name)
continue
}
config.Imports = append(config.Imports, dep)
}
return true, nil
}
// NormalizeName takes a package name and normalizes it to the top level package.
//
// For example, golang.org/x/crypto/ssh becomes golang.org/x/crypto. 'ssh' is
// returned as extra data.
func NormalizeName(name string) (string, string) {
// Fastpath check if a name in the GOROOT. There is an issue when a pkg
// is in the GOROOT and GetRootFromPackage tries to look it up because it
// expects remote names.
b, err := util.GetBuildContext()
if err == nil {
p := filepath.Join(b.GOROOT, "src", filepath.FromSlash(name))
if _, err := os.Stat(p); err == nil {
return name, ""
}
}
root := util.GetRootFromPackage(name)
extra := strings.TrimPrefix(name, root)
if len(extra) > 0 && extra != "/" {
extra = strings.TrimPrefix(extra, "/")
} else {
// If extra is / (which is what it would be here) we want to return ""
extra = ""
}
return root, extra
// parts := strings.SplitN(name, "/", 4)
// extra := ""
// if len(parts) < 3 {
// return name, extra
// }
// if len(parts) == 4 {
// extra = parts[3]
// }
// return strings.Join(parts[0:3], "/"), extra
}