forked from driskell/log-courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc-admin.go
219 lines (178 loc) · 5.16 KB
/
lc-admin.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2014-2015 Jason Woods.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"flag"
"fmt"
"net/url"
"os"
"strings"
"github.com/driskell/log-courier/lc-lib/admin"
"github.com/driskell/log-courier/lc-lib/config"
"github.com/driskell/log-courier/lc-lib/core"
)
type commandProcessor interface {
ProcessCommand(string) bool
}
type lcAdmin struct {
quiet bool
watch bool
legacy bool
adminConnect string
configFile string
client *admin.Client
}
func main() {
(&lcAdmin{}).Run()
}
func (a *lcAdmin) printHelp() {
fmt.Printf("Available commands:\n")
fmt.Printf(" help\n")
fmt.Printf(" Show this information\n")
fmt.Printf(" status\n")
fmt.Printf(" Get a full status snapshot of all Log Courier internals\n")
fmt.Printf(" prospector [status | files [id]]\n")
fmt.Printf(" Get information on prospector state and running harvesters\n")
fmt.Printf(" publisher [status | endpoints [id]]\n")
fmt.Printf(" Get information on connectivity and endpoints\n")
fmt.Printf(" reload\n")
fmt.Printf(" Signals Log Courier to reload its configuration\n")
fmt.Printf(" version\n")
fmt.Printf(" Get the remote version\n")
fmt.Printf(" debug\n")
fmt.Printf(" Get a live goroutine trace for debugging purposes\n")
fmt.Printf(" exit\n")
fmt.Printf(" Exit\n")
}
func (a *lcAdmin) startUp() {
var version bool
flag.BoolVar(&version, "version", false, "display the Log Courier client version")
flag.BoolVar(&a.quiet, "quiet", false, "quietly execute the command line argument and output only the result")
flag.BoolVar(&a.watch, "watch", false, "repeat the command specified on the command line every second")
flag.BoolVar(&a.legacy, "legacy", false, "connect to version 1.x Log Courier instances")
flag.StringVar(&a.adminConnect, "connect", "", "the Log Courier instance to connect to")
flag.StringVar(&a.configFile, "config", config.DefaultConfigurationFile, "read the Log Courier connection address from the given configuration file (ignored if connect specified)")
flag.Parse()
if version {
fmt.Printf("Log Courier client version %s\n", core.LogCourierVersion)
os.Exit(0)
}
if !a.quiet {
fmt.Printf("Log Courier client version %s\n", core.LogCourierVersion)
}
a.loadConfig()
fmt.Println("")
}
func (a *lcAdmin) loadConfig() {
if a.configFile == "" && a.adminConnect == "" {
if admin.DefaultAdminBind == "" {
fmt.Printf("Either -connect or -config must be specified\n")
flag.PrintDefaults()
os.Exit(1)
} else {
a.adminConnect = admin.DefaultAdminBind
}
return
}
if a.adminConnect == "" {
fmt.Printf("Loading configuration: %s\n", a.configFile)
// Load admin connect address from the configuration file
config := config.NewConfig()
if err := config.Load(a.configFile, false); err != nil {
fmt.Printf("Configuration error: %s\n", err)
os.Exit(1)
}
a.adminConnect = config.Get("admin").(*admin.Config).Bind
}
}
func (a *lcAdmin) Run() {
a.startUp()
admin, err := a.newCommandProcessor()
if err != nil {
fmt.Printf("Failed to initialise: %s\n", err)
os.Exit(1)
return
}
prompt := &prompt{commandProcessor: admin}
args := flag.Args()
if len(args) != 0 {
if prompt.argsCommand(args, a.watch) {
os.Exit(0)
}
os.Exit(1)
}
if a.quiet {
fmt.Printf("No command specified on the command line for quiet execution\n")
os.Exit(1)
}
if a.watch {
fmt.Printf("No command specified on the command line to watch\n")
os.Exit(1)
}
prompt.run()
}
func (a *lcAdmin) newCommandProcessor() (commandProcessor, error) {
if a.legacy {
// Create the old V1 legacy processor
return newV1LcAdmin(a.quiet, a.adminConnect)
}
if !a.quiet {
fmt.Printf("Attempting connection to %s...\n", a.adminConnect)
}
client, err := admin.NewClient(a.adminConnect)
if err != nil {
return nil, err
}
a.client = client
if !a.quiet {
fmt.Printf("Connected to Log Courier version %s\n\n", client.RemoteVersion())
}
return a, nil
}
func (a *lcAdmin) ProcessCommand(command string) bool {
if command == "help" {
a.printHelp()
return true
}
if command == "status" {
// Simulate empty command so we grab full status
command = ""
}
command = url.QueryEscape(command)
path := strings.Map(func(r rune) rune {
if r == '+' {
return '/'
}
return r
}, command)
resp, err := a.client.Request(path)
if err != nil {
switch err {
case admin.ErrNotFound:
fmt.Printf("Unknown command\n")
return false
}
switch err.(type) {
case admin.ErrUnknown:
fmt.Printf("Log Courier returned an error: %s\n", err.(admin.ErrUnknown).Error())
return false
}
fmt.Printf("The API request failed: %s\n", err)
return false
}
fmt.Println(resp)
return true
}