@@ -61,7 +61,7 @@ func (t *DeviceProcessor) connect() (*ssh.Client, error) {
61
61
62
62
client , err := ssh .Dial ("tcp" , t .device .Address , sshClientConfig )
63
63
if err != nil {
64
- return nil , errors . Errorf ( "Failed to connect to Device(%s): %s" , t . device . Name , err )
64
+ return nil , err
65
65
}
66
66
67
67
return client , nil
@@ -90,13 +90,11 @@ func (t *DeviceProcessor) startShell(client *ssh.Client) (*ssh.Session, io.Write
90
90
ssh .TTY_OP_OSPEED : 38400 , // output speed = 14.4kbaud
91
91
}
92
92
93
- err = session .RequestPty ("xterm" , 0 , 200 , modes )
94
- if err != nil {
93
+ if err = session .RequestPty ("xterm" , 0 , 200 , modes ); err != nil {
95
94
return nil , nil , nil , errors .Errorf ("Request for pty failed: %s" , err )
96
95
}
97
96
98
- err = session .Shell ()
99
- if err != nil {
97
+ if err = session .Shell (); err != nil {
100
98
return nil , nil , nil , errors .Errorf ("Request for shell failed: %s" , err )
101
99
}
102
100
@@ -149,8 +147,7 @@ func (t *DeviceProcessor) initVM(stdIn io.WriteCloser, stdOut io.Reader, ctx vmC
149
147
if err = vm .Set ("_ctxRaw" , ctxRaw ); err != nil {
150
148
return nil , errors .Errorf ("Failed to set _ctxRaw variable: %s" , err )
151
149
}
152
- _ , err = vm .Run (`ctx = JSON.parse(_ctxRaw);` )
153
- if err != nil {
150
+ if _ , err = vm .Run (`ctx = JSON.parse(_ctxRaw);` ); err != nil {
154
151
return nil , errors .Errorf ("Failed to unserialize context variable: %s" , err )
155
152
}
156
153
@@ -159,20 +156,18 @@ func (t *DeviceProcessor) initVM(stdIn io.WriteCloser, stdOut io.Reader, ctx vmC
159
156
160
157
func (t * DeviceProcessor ) saveFile (backupTarget * devices.DeviceClassTarget , file * ReceivedFile ) error {
161
158
162
- dstPath := path .Join (t .configDir , t .device .Name , fmt .Sprintf ("%s.conf" , backupTarget .Name ))
163
-
164
- dirPath , _ := path .Split (dstPath )
159
+ dirPath := path .Join (t .configDir , t .device .Name )
165
160
166
161
// Create the parent directory structure if needed
167
162
if _ , err := os .Stat (dirPath ); os .IsNotExist (err ) {
168
- err = os .MkdirAll (dirPath , os .ModePerm )
169
- if err != nil {
170
- return errors .Errorf ("Parent directory '%s' doesn't exist and an error occurred while trying to create it: %s" , dirPath , err )
163
+ if err := os .MkdirAll (dirPath , os .ModePerm ); err != nil {
164
+ return errors .Errorf ("Unable to create directory '%s': %s" , dirPath , err )
171
165
}
172
166
}
173
167
174
- err := ioutil .WriteFile (dstPath , file .Data .Bytes (), 0644 )
175
- if err != nil {
168
+ dstPath := path .Join (dirPath , fmt .Sprintf ("%s.conf" , backupTarget .Name ))
169
+
170
+ if err := ioutil .WriteFile (dstPath , file .Data .Bytes (), 0644 ); err != nil {
176
171
return errors .Errorf ("Unable to write to file '%s': %s" , dstPath , err )
177
172
}
178
173
@@ -183,8 +178,7 @@ func (t *DeviceProcessor) Process(reciever *TFTPReceiver) error {
183
178
for target_name , _ := range t .device .Class .Targets {
184
179
log .Printf ("Processing backup target '%s':'%s'" , t .device .Name , target_name )
185
180
186
- err := t .ProcessTarget (target_name , reciever )
187
- if err != nil {
181
+ if err := t .ProcessTarget (target_name , reciever ); err != nil {
188
182
return errors .Errorf ("target %s: %s" , target_name , err )
189
183
}
190
184
}
@@ -230,8 +224,7 @@ func (t *DeviceProcessor) ProcessTarget(target_name string, reciever *TFTPReceiv
230
224
return errors .Errorf ("Failed to init JavaScript VM: %s" , err )
231
225
}
232
226
233
- _ , err = vm .Run (backupTarget .Macro )
234
- if err != nil {
227
+ if _ , err := vm .Run (backupTarget .Macro ); err != nil {
235
228
return errors .Errorf ("JavaScript VM Runtime Error: %s" , err )
236
229
}
237
230
@@ -240,7 +233,7 @@ func (t *DeviceProcessor) ProcessTarget(target_name string, reciever *TFTPReceiv
240
233
// Wait for a maximum of 60 seconds for the file on the receive channel
241
234
select {
242
235
case err = <- reciever .GetErrorChannel ():
243
- log .Printf ("TFTP Receiver error: %s \n " , err )
236
+ log .Fatalln ("TFTP Receiver error:" , err )
244
237
case recvdFile = <- recvChan :
245
238
case <- time .After (60 * time .Second ):
246
239
return errors .Errorf ("Timed out waiting to receive file over TFTP" )
@@ -251,36 +244,32 @@ func (t *DeviceProcessor) ProcessTarget(target_name string, reciever *TFTPReceiv
251
244
return err
252
245
}
253
246
247
+ log .Printf ("Completed backup target: '%s':'%s'\n " , t .device .Name , backupTarget .Name )
248
+
254
249
return nil
255
250
}
256
251
257
252
func ottoExpect (vm * otto.Otto , expect * gexpect.ExpectIO ) error {
258
253
259
- var err error
260
-
261
- err = vm .Set ("dbgDump" , func (call otto.FunctionCall ) otto.Value {
254
+ if err := vm .Set ("dbgDump" , func (call otto.FunctionCall ) otto.Value {
262
255
263
256
v , _ := call .Argument (0 ).Export ()
264
257
fmt .Printf (">>> dbgDump >>>:\n %v<<< dbgDump <<<\n " , spew .Sdump (v ))
265
258
266
259
return otto.Value {}
267
- })
268
- if err != nil {
260
+ }); err != nil {
269
261
return err
270
262
}
271
263
272
- err = vm .Set ("dbgLog" , func (call otto.FunctionCall ) otto.Value {
273
-
264
+ if err := vm .Set ("dbgLog" , func (call otto.FunctionCall ) otto.Value {
274
265
fmt .Printf ("dbgLog: %s\n " , call .Argument (0 ).String ())
275
-
276
266
return otto.Value {}
277
- })
278
- if err != nil {
267
+ }); err != nil {
279
268
return err
280
269
}
281
270
282
271
// function expect(val string) string {}
283
- err = vm .Set ("expect" , func (call otto.FunctionCall ) otto.Value {
272
+ if err : = vm .Set ("expect" , func (call otto.FunctionCall ) otto.Value {
284
273
285
274
// TODO: Make timeout configurable
286
275
err := expect .ExpectTimeout (call .Argument (0 ).String (), 15 * time .Second )
@@ -290,12 +279,11 @@ func ottoExpect(vm *otto.Otto, expect *gexpect.ExpectIO) error {
290
279
}
291
280
292
281
return otto.Value {}
293
- })
294
- if err != nil {
282
+ }); err != nil {
295
283
return err
296
284
}
297
285
298
- err = vm .Set ("readLine" , func (call otto.FunctionCall ) otto.Value {
286
+ if err : = vm .Set ("readLine" , func (call otto.FunctionCall ) otto.Value {
299
287
300
288
line , err := expect .ReadLine ()
301
289
if err != nil {
@@ -308,21 +296,19 @@ func ottoExpect(vm *otto.Otto, expect *gexpect.ExpectIO) error {
308
296
}
309
297
310
298
return v
311
- })
312
- if err != nil {
299
+ }); err != nil {
313
300
return err
314
301
}
315
302
316
- err = vm .Set ("sendLine" , func (call otto.FunctionCall ) otto.Value {
303
+ if err : = vm .Set ("sendLine" , func (call otto.FunctionCall ) otto.Value {
317
304
318
305
err := expect .SendLine (call .Argument (0 ).String ())
319
306
if err != nil {
320
307
panic (vm .MakeCustomError ("ExpectError" , err .Error ()))
321
308
}
322
309
323
310
return otto.Value {}
324
- })
325
- if err != nil {
311
+ }); err != nil {
326
312
return err
327
313
}
328
314
0 commit comments