-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmain.go
186 lines (150 loc) · 4.61 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
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
180
181
182
183
184
185
186
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/mylxsw/remote-tail/command"
"github.com/mylxsw/remote-tail/console"
)
var mossSep = ".--. --- .-- . .-. . -.. -... -.-- -- -.-- .-.. -..- ... .-- \n"
var welcomeMessage = getWelcomeMessage() + console.ColorfulText(console.TextMagenta, mossSep)
var filePath = flag.String("file", "", "-file=\"/var/log/*.log\"")
var hostStr = flag.String("hosts", "", "[email protected],[email protected]")
var configFile = flag.String("conf", "", "-conf=example.toml")
var tailFlags = flag.String("tail-flags", "--retry --follow=name", "flags for tail command, you can use -f instead if your server does't support `--retry --follow=name` flags")
var slient = flag.Bool("slient", false, "-slient=false")
var Version = ""
var GitCommit = ""
func usageAndExit(message string) {
if message != "" {
fmt.Fprintln(os.Stderr, message)
}
flag.Usage()
fmt.Fprint(os.Stderr, "\n")
os.Exit(1)
}
func printWelcomeMessage(config command.Config) {
fmt.Println(welcomeMessage)
for _, server := range config.Servers {
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
serverInfo := fmt.Sprintf("%s@%s:%s", server.User, server.Hostname, server.TailFile)
fmt.Println(console.ColorfulText(console.TextMagenta, serverInfo))
}
fmt.Printf("\n%s\n", console.ColorfulText(console.TextCyan, mossSep))
}
func parseConfig(filePath string, hostStr string, configFile string, slient bool, tailFlags string) (config command.Config) {
if configFile != "" {
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
}
} else {
hosts := strings.Split(hostStr, ",")
config = command.Config{}
config.TailFile = filePath
config.Servers = make(map[string]command.Server, len(hosts))
config.Slient = slient
config.TailFlags = tailFlags
for index, hostname := range hosts {
hostInfo := strings.Split(strings.Replace(hostname, ":", "@", -1), "@")
var port int
if len(hostInfo) > 2 {
port, _ = strconv.Atoi(hostInfo[2])
}
config.Servers["server_"+string(index)] = command.Server{
ServerName: "server_" + string(index),
Hostname: hostInfo[1],
User: hostInfo[0],
Port: port,
}
}
}
if config.TailFlags == "" {
config.TailFlags = "--retry --follow=name"
}
return
}
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, welcomeMessage)
fmt.Fprint(os.Stderr, "Options:\n\n")
flag.PrintDefaults()
}
flag.Parse()
if (*filePath == "" || *hostStr == "") && *configFile == "" {
usageAndExit("")
}
config := parseConfig(*filePath, *hostStr, *configFile, *slient, *tailFlags)
if !config.Slient {
printWelcomeMessage(config)
}
outputs := make(chan command.Message, 255)
var wg sync.WaitGroup
for _, server := range config.Servers {
wg.Add(1)
go func(server command.Server) {
defer func() {
if err := recover(); err != nil {
fmt.Printf(console.ColorfulText(console.TextRed, "Error: %s\n"), err)
}
}()
defer wg.Done()
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
if server.TailFlags == "" {
server.TailFlags = config.TailFlags
}
// If the service configuration does not have a port, the default value of 22 is used
if server.Port == 0 {
server.Port = 22
}
cmd := command.NewCommand(server)
cmd.Execute(outputs)
}(server)
}
if len(config.Servers) > 0 {
go func() {
for output := range outputs {
content := strings.Trim(output.Content, "\r\n")
// 去掉文件名称输出
if content == "" || (strings.HasPrefix(content, "==>") && strings.HasSuffix(content, "<==")) {
continue
}
if config.Slient {
fmt.Printf("%s -> %s\n", output.Host, content)
} else {
fmt.Printf(
"%s %s %s\n",
console.ColorfulText(console.TextGreen, output.Host),
console.ColorfulText(console.TextYellow, "->"),
content,
)
}
}
}()
} else {
fmt.Println(console.ColorfulText(console.TextRed, "No target host is available"))
}
wg.Wait()
}
func getWelcomeMessage() string {
return `
____ _ _____ _ _
| _ \ ___ _ __ ___ ___ | |_ __|_ _|_ _(_) |
| |_) / _ \ '_ ' _ \ / _ \| __/ _ \| |/ _' | | |
| _ < __/ | | | | | (_) | || __/| | (_| | | |
|_| \_\___|_| |_| |_|\___/ \__\___||_|\__,_|_|_|
Author: mylxsw
Homepage: github.com/mylxsw/remote-tail
Version: ` + Version + "(" + GitCommit + ")" + `
`
}