-
Notifications
You must be signed in to change notification settings - Fork 24
/
regions.go
135 lines (119 loc) · 2.31 KB
/
regions.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
package regions
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/leancloud/lean-cli/utils"
)
type Region int
const (
Invalid Region = iota
ChinaNorth
USWest
ChinaEast
ChinaTDS1
APSG
)
var regionLoginStatus = make(map[Region]bool)
func init() {
regionStatus, err := ioutil.ReadFile(filepath.Join(utils.ConfigDir(), "leancloud", "logined_regions.json"))
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
} else {
if err := json.Unmarshal(regionStatus, ®ionLoginStatus); err != nil {
panic(err)
}
}
}
func (r Region) String() string {
switch r {
case ChinaNorth:
return "cn-n1"
case ChinaEast:
return "cn-e1"
case USWest:
return "us-w1"
case ChinaTDS1:
return "cn-tds1"
case APSG:
return "ap-sg"
default:
return "invalid"
}
}
func (r Region) EnvString() string {
switch r {
case ChinaNorth, ChinaEast, ChinaTDS1:
return "CN"
case USWest:
return "US"
case APSG:
return "AP"
default:
return "invalid"
}
}
func (r Region) Description() string {
switch r {
case ChinaNorth:
return "LeanCloud (China North)"
case USWest:
return "LeanCloud (International)"
case ChinaEast:
return "LeanCloud (China East)"
case ChinaTDS1:
return "TDS (China Mainland)"
case APSG:
return "TDS (Global)"
default:
return "Invalid"
}
}
func Parse(region string) Region {
switch region {
case "cn", "CN", "cn-n1":
return ChinaNorth
case "tab", "TAB", "cn-e1":
return ChinaEast
case "us", "US", "us-w1":
return USWest
case "cn-tds1":
return ChinaTDS1
case "ap-sg", "AP":
return APSG
default:
return Invalid
}
}
func (r Region) InChina() bool {
switch r {
case ChinaNorth, ChinaEast, ChinaTDS1:
return true
case USWest, APSG:
return false
}
panic("invalid region")
}
// Only return available regions
func GetLoginedRegions(availableRegions []Region) []Region {
var regions []Region
for _, region := range availableRegions {
if regionLoginStatus[region] {
regions = append(regions, region)
}
}
return regions
}
func SetRegionLoginStatus(region Region) {
regionLoginStatus[region] = true
}
func SaveRegionLoginStatus() error {
data, err := json.MarshalIndent(regionLoginStatus, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(utils.ConfigDir(), "leancloud", "logined_regions.json"), data, 0644)
}