forked from NothingCw/cf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubutil.go
157 lines (140 loc) · 3.3 KB
/
pubutil.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
147
148
149
150
151
152
153
154
155
156
157
package pubutil
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/fwoawr/cf/pkg/util/global"
)
type TimestampCache struct {
TimestampType string
Timestamp int64
}
type OSSCache struct {
AccessKeyId string
SN string
Name string
BucketACL string
ObjectNumber string
ObjectSize string
Region string
BucketURL string
}
type ECSCache struct {
AccessKeyId string
SN string
InstanceId string
InstanceName string
OSName string
OSType string
Status string
PrivateIpAddress string
PublicIpAddress string
RegionId string
}
type RDSCache struct {
AccessKeyId string
SN string
DBInstanceId string
Engine string
EngineVersion string
DBInstanceStatus string
RegionId string
}
type TakeoverConsoleCache struct {
Provider string
AccessKeyId string
PrimaryAccountID string
UserName string
Password string
LoginUrl string
CreateTime string
}
func GetUserDir() string {
home, _ := os.UserHomeDir()
return home
}
func GetConfigFilePath() string {
home, _ := GetCFHomeDir()
CreateFolder(home)
configFilePath := filepath.Join(home, "cache.db")
return configFilePath
}
func GetCFHomeDir() (string, error) {
return filepath.Join(GetUserDir(), global.AppDirName), nil
}
func FileExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func CreateFolder(folder string) {
if !FileExists(folder) {
log.Tracef("创建 %s 目录 (Create %s directory): ", folder, folder)
_ = os.MkdirAll(folder, 0700)
}
}
func FormatFileSize(fileSize int64) (size string) {
if fileSize < 1024 {
return fmt.Sprintf("%.2f B", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2f KB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f MB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f GB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f TB", float64(fileSize)/float64(1024*1024*1024*1024))
} else {
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
func ReadFile(filePath string) (bool, string) {
if FileExists(filePath) {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
content, err := ioutil.ReadAll(file)
return true, string(content)
} else {
return false, ""
}
}
func StringClean(str string) string {
str = strings.TrimSpace(str)
str = strings.Replace(str, "\n", "", -1)
return str
}
func MaskAK(ak string) string {
if len(ak) > 7 {
defer func() {
err := recover()
if err != nil {
log.Error("输入的访问密钥有误 (The entered access key is incorrect.)")
os.Exit(1)
}
}()
prefix := ak[:2]
suffix := ak[len(ak)-6:]
return prefix + strings.Repeat("*", 18) + suffix
} else {
return ak
}
}
func IN(target string, str_array []string) bool {
for _, element := range str_array {
if target == element {
return true
}
}
return false
}