/
paths.go
138 lines (121 loc) · 4.61 KB
/
paths.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
package utils
import (
"fmt"
"os"
"runtime"
"strings"
)
// GetSpaceCloudDirectory gets the root space cloud directory
func GetSpaceCloudDirectory() string {
return fmt.Sprintf("%s/.space-cloud", getHomeDirectory())
}
// GetSpaceCloudClusterDirectory gets the root space cloud directory
func GetSpaceCloudClusterDirectory(clusterID string) string {
return fmt.Sprintf("%s/.space-cloud/%s", getHomeDirectory(), clusterID)
}
// GetSpaceCloudHostsFilePath returns the path of the hosts files used in space cloud
func GetSpaceCloudHostsFilePath(id string) string {
if id == "default" {
return fmt.Sprintf("%s/hosts", GetSpaceCloudDirectory())
}
return fmt.Sprintf("%s/%s/hosts", GetSpaceCloudDirectory(), id)
}
// GetSpaceCloudRoutingConfigPath returns the path of the file storing the service routing config
func GetSpaceCloudRoutingConfigPath(id string) string {
if id == "default" {
return fmt.Sprintf("%s/routing-config.json", GetSpaceCloudDirectory())
}
return fmt.Sprintf("%s/%s/routing-config.json", GetSpaceCloudDirectory(), id)
}
// GetSpaceCloudConfigFilePath returns the path of the file storing the config
func GetSpaceCloudConfigFilePath(id string) string {
if id == "default" {
return fmt.Sprintf("%s/config.yaml", GetSpaceCloudDirectory())
}
return fmt.Sprintf("%s/%s/config.yaml", GetSpaceCloudDirectory(), id)
}
// GetSecretsDir returns the path of the directory storing all the secrets
func GetSecretsDir(id string) string {
if id == "default" {
return fmt.Sprintf("%s/secrets", GetSpaceCloudDirectory())
}
return fmt.Sprintf("%s/%s/secrets", GetSpaceCloudDirectory(), id)
}
// GetTempSecretsDir gets the path of the directory storing all the temp secrets
func GetTempSecretsDir(id string) string {
if id == "default" {
return fmt.Sprintf("%s/secrets/temp-secrets", GetSpaceCloudDirectory())
}
return fmt.Sprintf("%s/%s/secrets/temp-secrets", GetSpaceCloudDirectory(), id)
}
func getHomeDirectory() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
// GetAccountConfigPath get the path to account config yaml file
func getAccountConfigPath() string {
return fmt.Sprintf("%s/accounts.yaml", GetSpaceCloudDirectory())
}
// GetMountHostsFilePath returns the path of the hosts files to be mounted in in space cloud
func GetMountHostsFilePath(id string) string {
if runtime.GOOS == "windows" {
if id == "default" {
return fmt.Sprintf("%s/.space-cloud/hosts", getWindowsUserDirectory())
}
return fmt.Sprintf("%s/.space-cloud/%s/hosts", getWindowsUserDirectory(), id)
}
return GetSpaceCloudHostsFilePath(id)
}
// GetMountConfigFilePath returns the path of the config files to be mounted in space cloud
func GetMountConfigFilePath(id string) string {
if runtime.GOOS == "windows" {
if id == "default" {
return fmt.Sprintf("%s/.space-cloud/config.yaml", getWindowsUserDirectory())
}
return fmt.Sprintf("%s/.space-cloud/%s/config.yaml", getWindowsUserDirectory(), id)
}
return GetSpaceCloudConfigFilePath(id)
}
// GetMountSecretsDir returns the path of the secret dir to be mounted in space cloud
func GetMountSecretsDir(id string) string {
if runtime.GOOS == "windows" {
if id == "default" {
return fmt.Sprintf("%s/.space-cloud/secrets", getWindowsUserDirectory())
}
return fmt.Sprintf("%s/.space-cloud/%s/secrets", getWindowsUserDirectory(), id)
}
return GetSecretsDir(id)
}
// GetMountTempSecretsDir returns the path of the temp secret dir to be mounted in space cloud
func GetMountTempSecretsDir(id string) string {
if runtime.GOOS == "windows" {
if id == "default" {
return fmt.Sprintf("%s/.space-cloud/secrets/temp-secrets", getWindowsUserDirectory())
}
return fmt.Sprintf("%s/.space-cloud/%s/secrets/temp-secrets", getWindowsUserDirectory(), id)
}
return GetTempSecretsDir(id)
}
// GetMountRoutingConfigPath returns the path of the routing config to be mounted in space cloud
func GetMountRoutingConfigPath(id string) string {
if runtime.GOOS == "windows" {
if id == "default" {
return fmt.Sprintf("%s/.space-cloud/routing-config.json", getWindowsUserDirectory())
}
return fmt.Sprintf("%s/.space-cloud/%s/routing-config.json", getWindowsUserDirectory(), id)
}
return GetSpaceCloudRoutingConfigPath(id)
}
// getWindowsUserDirectory gets home directory to support setup on window
func getWindowsUserDirectory() string {
// eg. HOMEDRIVE = "C:" and HOMEPATH = "\User\username "
homeDrive := strings.ToLower(strings.Split(os.Getenv("HOMEDRIVE"), ":")[0])
homePath := strings.ReplaceAll(os.Getenv("HOMEPATH"), "\\", "/")
return "/" + homeDrive + homePath
}