-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
146 lines (136 loc) · 3.16 KB
/
main.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
package main
import (
"archive/tar"
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"path"
"path/filepath"
"strings"
)
const splitChar = ","
var dir, host, skipDir, suffix string
var errorFlag bool
func Init() {
flag.StringVar(&dir, "dir", "", "Compressed folder,[/var/www]")
flag.StringVar(&host, "host", "", "Server IP port,[192.168.1.1:8888]")
flag.StringVar(&skipDir, "skip", "", "skip folder")
flag.StringVar(&suffix, "suffix", "", "file type")
flag.BoolVar(&errorFlag, "err", true, "Exit in case of exception,Default true")
}
func main() {
Init()
flag.Parse()
if dir == "" || host == "" {
fmt.Println("upfile -dir /var/www -host 192.168.1.1:8888")
fmt.Println("upfile -dir /var/www -host 192.168.1.1:8888 -err flase")
fmt.Println("upfile -dir /var/www -host 192.168.1.1:8888 -skip \"/var/www/log,/var/www/upload\"")
flag.PrintDefaults()
os.Exit(0)
}
_, err := os.Stat(dir)
if err != nil {
log.Fatalln("-dir error,", err)
}
start(host, dir, "nc")
}
func start(host, dir, module string) {
switch module {
case "nc":
conn, err := net.Dial("tcp", host)
if err != nil {
log.Fatalln("Connect "+host+"error,", err)
}
defer conn.Close()
err = sendData(dir, conn)
if err != nil {
log.Fatalln("sendData error,", err)
}
default:
log.Fatalln("功能还未开发")
}
}
//发送压缩文件
func sendData(dir string, conn net.Conn) error {
gw := gzip.NewWriter(conn)
tw := tar.NewWriter(gw)
defer gw.Close()
defer tw.Close()
return filepath.Walk(dir, func(fileName string, fileInfo os.FileInfo, err error) error {
if err != nil {
return ProcessError(err)
}
//添加排除文件夹
if excludeFile(fileName, fileInfo, skipDir) {
return filepath.SkipDir
}
//添加要打包的文件类型
if suffix != "" && path.Ext(fileName) != suffix {
return nil
}
fileName = filepath.ToSlash(filepath.Clean(fileName))
fileHeader, err := tar.FileInfoHeader(fileInfo, "")
if err != nil {
return ProcessError(err)
}
//替换绝对路径
if filepath.IsAbs(fileName) {
fileHeader.Name = strings.TrimPrefix(fileName, filepath.ToSlash(filepath.Dir(filepath.Clean(dir))))
} else {
fileHeader.Name = fileName
}
fileHeader.Format = tar.FormatGNU
if err := tw.WriteHeader(fileHeader); err != nil {
return ProcessError(err)
}
//忽略不是普通的文件
if !fileInfo.Mode().IsRegular() {
return nil
}
fileRead, err := os.Open(fileName)
defer fileRead.Close()
if err != nil {
return ProcessError(err)
}
_, err = io.Copy(tw, fileRead)
if err != nil {
return err
}
return nil
})
}
//排除文件夹或文件类型
func excludeFile(filename string, fileInfo os.FileInfo, skip string) bool {
if skip == "" {
return false
}
skips := strings.Split(skip, splitChar)
//排除文件夹
if fileInfo.IsDir() {
for _, str := range skips {
if filepath.Dir(filename) == str || filename == str {
return true
}
}
} else { //排除文件
for _, str := range skips {
if !strings.HasPrefix(str, ".") {
continue
}
if strings.HasSuffix(fileInfo.Name(), str) {
return true
}
}
}
return false
}
func ProcessError(err error) error {
if errorFlag {
return err
}
return nil
}