Repository navigation
Expand file tree
/
Copy pathyaml.go
More file actions
125 lines (110 loc) · 3.18 KB
/
Copy pathyaml.go
File metadata and controls
125 lines (110 loc) · 3.18 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
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/glide/yaml"
)
// ParseYaml parses the glide.yaml format and returns a Configuration object.
//
// Params:
// - filename (string): YAML filename as a string
//
// Returns:
// - *yaml.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
}
cfg, err := yaml.FromYaml(string(yml))
if err != nil {
return nil, err
}
return cfg, 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:
// - *yaml.Config: The configuration.
func ParseYamlString(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
yamlString := p.Get("yaml", "").(string)
cfg, err := yaml.FromYaml(string(yamlString))
if err != nil {
return nil, err
}
return cfg, nil
}
// WriteYaml writes a yaml.Node to the console as a string.
//
// Params:
// - conf: A *yaml.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) {
cfg := p.Get("conf", nil).(*yaml.Config)
toStdout := p.Get("toStdout", true).(bool)
yml, err := yaml.ToYaml(cfg)
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)
} else if toStdout {
out = p.Get("out", os.Stdout).(io.Writer)
fmt.Fprint(out, yml)
}
// Otherwise we supress output.
return true, nil
}
// AddDependencies adds a list of *Dependency objects to the given *yaml.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", []*yaml.Dependency{}).([]*yaml.Dependency)
config := p.Get("conf", nil).(*yaml.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) {
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
}