-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.go
65 lines (60 loc) · 1.06 KB
/
common.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
package sshproxy
import (
"encoding/base64"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func getQuery(datas []string, files []string) ([][]byte, error) {
d, err := openDatas(datas)
if err != nil {
return nil, err
}
f, err := openFiles(files)
if err != nil {
return nil, err
}
ret := append(d, f...)
return ret, nil
}
func openDatas(datas []string) ([][]byte, error) {
if len(datas) == 0 {
return [][]byte{}, nil
}
var ret [][]byte
for _, s := range datas {
if s == "" {
continue
}
data, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return nil, err
}
ret = append(ret, data)
}
return ret, nil
}
func openFiles(files []string) ([][]byte, error) {
if len(files) == 0 {
return [][]byte{}, nil
}
var ret [][]byte
for _, f := range files {
if f == "" {
continue
}
if strings.HasPrefix(f, "~") {
home, err := os.UserHomeDir()
if err == nil {
f = filepath.Join(home, f[1:])
}
}
data, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
ret = append(ret, data)
}
return ret, nil
}