forked from Masterminds/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrors.go
146 lines (119 loc) · 2.86 KB
/
mirrors.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
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
package action
import (
"os"
"path/filepath"
"github.com/Masterminds/glide/mirrors"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
)
// MirrorsList displays a list of currently setup mirrors.
func MirrorsList() error {
home := gpath.Home()
op := filepath.Join(home, "mirrors.yaml")
if _, err := os.Stat(op); os.IsNotExist(err) {
msg.Info("No mirrors exist. No mirrors.yaml file not found")
return nil
}
ov, err := mirrors.ReadMirrorsFile(op)
if err != nil {
msg.Die("Unable to read mirrors.yaml file: %s", err)
}
if len(ov.Repos) == 0 {
msg.Info("No mirrors found")
return nil
}
msg.Info("Mirrors...")
for _, r := range ov.Repos {
if r.Vcs == "" {
msg.Info("--> %s replaced by %s", r.Original, r.Repo)
} else {
msg.Info("--> %s replaced by %s (%s)", r.Original, r.Repo, r.Vcs)
}
}
return nil
}
// MirrorsSet sets a mirror to use
func MirrorsSet(o, r, v string) error {
if o == "" || r == "" {
msg.Err("Both the original and mirror values are required")
return nil
}
home := gpath.Home()
op := filepath.Join(home, "mirrors.yaml")
var ov *mirrors.Mirrors
if _, err := os.Stat(op); os.IsNotExist(err) {
msg.Info("No mirrors.yaml file exists. Creating new one")
ov = &mirrors.Mirrors{
Repos: make(mirrors.MirrorRepos, 0),
}
} else {
ov, err = mirrors.ReadMirrorsFile(op)
if err != nil {
msg.Die("Error reading existing mirrors.yaml file: %s", err)
}
}
found := false
for i, re := range ov.Repos {
if re.Original == o {
found = true
msg.Info("%s found in mirrors. Replacing with new settings", o)
ov.Repos[i].Repo = r
ov.Repos[i].Vcs = v
}
}
if !found {
nr := &mirrors.MirrorRepo{
Original: o,
Repo: r,
Vcs: v,
}
ov.Repos = append(ov.Repos, nr)
}
msg.Info("%s being set to %s", o, r)
err := ov.WriteFile(op)
if err != nil {
msg.Err("Error writing mirrors.yaml file: %s", err)
} else {
msg.Info("mirrors.yaml written with changes")
}
return nil
}
// MirrorsRemove removes a mirrors setting
func MirrorsRemove(k string) error {
if k == "" {
msg.Err("The mirror to remove is required")
return nil
}
home := gpath.Home()
op := filepath.Join(home, "mirrors.yaml")
if _, err := os.Stat(op); os.IsNotExist(err) {
msg.Err("mirrors.yaml file not found")
return nil
}
ov, err := mirrors.ReadMirrorsFile(op)
if err != nil {
msg.Die("Unable to read mirrors.yaml file: %s", err)
}
var nre mirrors.MirrorRepos
var found bool
for _, re := range ov.Repos {
if re.Original != k {
nre = append(nre, re)
} else {
found = true
}
}
if !found {
msg.Warn("%s was not found in mirrors", k)
} else {
msg.Info("%s was removed from mirrors", k)
ov.Repos = nre
err = ov.WriteFile(op)
if err != nil {
msg.Err("Error writing mirrors.yaml file: %s", err)
} else {
msg.Info("mirrors.yaml written with changes")
}
}
return nil
}