-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorlist.go
105 lines (88 loc) · 2.08 KB
/
mirrorlist.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/markbates/pkger"
"gopkg.in/yaml.v2"
)
type (
Source interface {
EnvVars() map[string]string
}
Bundler struct {
Rubygems string `yaml:"rubygems_org"`
}
Pipenv struct {
PypiMirror string `yaml:"pypi_mirror"`
}
Pypi struct {
IndexUrl string `yaml:"index_url"`
ExtraIndexUrl string `yaml:"extra_index_url"`
TrustedHost string `yaml:"trusted_host"`
}
Mirrorlist struct {
Bundler *Bundler `yaml:"bundler"`
Pipenv *Pipenv `yaml:"pipenv"`
Pypi *Pypi `yaml:"pypi"`
}
)
func GetMirrorlist(location string) (*Mirrorlist, error) {
var locationCode string
switch strings.ToLower(location) {
case "kr", "korea", "south_korea":
locationCode = "kr"
default:
locationCode = "default"
}
f, err := pkger.Open(fmt.Sprintf("/mirrorlist/%s.yaml", locationCode))
if err != nil {
return nil, errors.New("failed to load mirrorlist")
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, errors.New("failed to read mirrorlist")
}
var mirrorlist Mirrorlist
if yaml.Unmarshal(bytes, &mirrorlist) != nil {
return nil, errors.New("failed to parse mirrorlist")
}
return &mirrorlist, nil
}
func (b *Bundler) EnvVars() map[string]string {
return compactMap(map[string]string{
"BUNDLE_MIRROR__RUBYGEMS__ORG": b.Rubygems,
})
}
func (p *Pipenv) EnvVars() map[string]string {
return compactMap(map[string]string{
"PIPENV_PYPI_MIRROR": p.PypiMirror,
})
}
func (p *Pypi) EnvVars() map[string]string {
return compactMap(map[string]string{
"PIP_INDEX_URL": p.IndexUrl,
"PIP_EXTRA_INDEX_URL": p.ExtraIndexUrl,
"PIP_TRUSTED_HOST": p.TrustedHost,
})
}
func (ml *Mirrorlist) EnvVarsAll() map[string]string {
sources := []Source{ml.Bundler, ml.Pipenv, ml.Pypi}
result := map[string]string{}
for _, source := range sources {
for k, v := range source.EnvVars() {
result[k] = v
}
}
return result
}
func compactMap(src map[string]string) map[string]string {
result := map[string]string{}
for k, v := range src {
if v != "" {
result[k] = v
}
}
return result
}