-
Notifications
You must be signed in to change notification settings - Fork 13
/
gresult.go
179 lines (163 loc) · 3.79 KB
/
gresult.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package wsm
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/xiecat/wsm/lib/utils"
"strings"
)
type Mode string
const (
Raw Mode = "raw"
BasicInfo Mode = "basicInfo"
FileOpt Mode = "fileOpt"
DatabaseOpt Mode = "databaseOpt"
)
type gResult struct {
mode Mode
Raw []byte
Body map[string]string
Status bool
}
func newGResult(raw []byte, mode Mode) *gResult {
return &gResult{
Raw: raw,
mode: mode,
}
}
func (g *gResult) Parser() error {
switch g.mode {
case Raw:
result := make(map[string]string, 1)
result["raw"] = string(g.Raw)
result["status"] = "ok"
g.Body = result
case BasicInfo:
rawList := strings.Split(string(g.Raw), "\n")
result := make(map[string]string, len(rawList))
for _, r := range rawList {
if len(r) == 0 {
continue
}
rL := strings.SplitN(r, " : ", 2)
key := rL[0]
value := ""
if len(rL) == 2 {
value = rL[1]
}
result[key] = value
}
g.Body = result
case FileOpt:
jsonStr, err := parserFileOptToMap(string(g.Raw))
if err != nil {
return err
}
g.Body = jsonStr
case DatabaseOpt:
jsonStr, err := parserDatabaseOptToMap(string(g.Raw))
if err != nil {
return err
}
g.Body = jsonStr
}
return nil
}
func (g *gResult) ToMap() map[string]string {
return g.Body
}
func (g *gResult) ToString() string {
return string(g.Raw)
}
type fileInfo struct {
Name string `json:"name"`
LastModified string `json:"lastModified"`
FileType string `json:"type"`
FileSize string `json:"size"`
Perm string `json:"perm"`
}
func parserFileOptToMap(raw string) (map[string]string, error) {
var fileInfoList []string
result := make(map[string]string, 2)
rawList := strings.Split(raw, "\n")
if strings.Trim(rawList[0], " ") != "ok" {
return nil, errors.New(fmt.Sprintf("目标返回异常,无法正常格式化数据 : [%s]", raw))
}
result["status"] = rawList[0]
result["currentDir"] = rawList[1]
for i := 2; i < len(rawList); i++ {
var fi fileInfo
rawStr := strings.Split(rawList[i], "\t")
if len(rawStr) == 5 {
if rawStr[1] == "0" {
fi.FileType = "dir"
} else {
fi.FileType = "file"
}
fi.Name = rawStr[0]
fi.LastModified = rawStr[2]
fi.FileSize = rawStr[3]
fi.Perm = rawStr[4]
jsons, err := json.Marshal(fi)
if err != nil {
return nil, err
}
fileInfoList = append(fileInfoList, string(jsons))
}
}
result["fileList"] = strings.Join(fileInfoList, " , ")
return result, nil
}
func parserDatabaseOptToMap(raw string) (map[string]string, error) {
var dataInfoList []string
result := make(map[string]string, 2)
rawList := strings.Split(raw, "\n")
if strings.Trim(rawList[0], " ") != "ok" {
return nil, errors.New(fmt.Sprintf("目标返回异常,无法正常格式化数据 : [%s]", raw))
}
result["status"] = rawList[0]
titles := strings.Split(rawList[1], "\t")
var ss []string
for _, title := range titles {
if len(title) == 0 {
continue
}
deStr, err := base64.StdEncoding.DecodeString(title)
if err != nil {
return nil, err
}
x := make(map[string]string, 1)
x["name"] = string(deStr)
str, err := utils.MapToJsonStr(x)
if err != nil {
return nil, err
}
ss = append(ss, str)
}
var alls [][]string
alls = append(alls, ss)
for i := 2; i < len(rawList); i++ {
if len(rawList[i]) == 0 {
continue
}
rawStr := strings.Split(rawList[i], "\t")
var yyyy []string
for _, s := range rawStr {
if len(s) == 0 {
continue
}
deStr, _ := base64.StdEncoding.DecodeString(s)
yyyy = append(yyyy, string(deStr))
}
alls = append(alls, yyyy)
}
for _, v1 := range alls {
s := strings.Join(v1, ",")
dataInfoList = append(dataInfoList, s)
}
// 想恢复的话就先按" , "分割,再按 ","分割
s := strings.Join(dataInfoList, " , ")
result["dataList"] = s
return result, nil
}