Skip to content

Commit 6c0b475

Browse files
joshdkappleboy
authored andcommitted
Customization of logger output destination (#117)
* Customization of logger output destination * Tests to verify output correctness
1 parent 60993a7 commit 6c0b475

File tree

3 files changed

+138
-5
lines changed

3 files changed

+138
-5
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func run(c *cli.Context) error {
211211
Timeout: c.Duration("proxy.timeout"),
212212
},
213213
},
214+
Writer: os.Stdout,
214215
}
215216

216217
return plugin.Exec()

plugin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/appleboy/easyssh-proxy"
12+
"io"
1213
)
1314

1415
const (
@@ -40,6 +41,7 @@ type (
4041
// Plugin structure
4142
Plugin struct {
4243
Config Config
44+
Writer io.Writer
4345
}
4446
)
4547

@@ -122,10 +124,13 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
122124
}
123125

124126
func (p Plugin) log(host string, message ...interface{}) {
127+
if p.Writer == nil {
128+
p.Writer = os.Stdout
129+
}
125130
if count := len(p.Config.Host); count == 1 {
126-
fmt.Printf("%s", fmt.Sprintln(message...))
131+
fmt.Fprintf(p.Writer, "%s", fmt.Sprintln(message...))
127132
} else {
128-
fmt.Printf("%s: %s", host, fmt.Sprintln(message...))
133+
fmt.Fprintf(p.Writer, "%s: %s", host, fmt.Sprintln(message...))
129134
}
130135
}
131136

plugin_test.go

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"os"
55
"testing"
66

7+
"bytes"
78
"github.com/appleboy/easyssh-proxy"
89
"github.com/stretchr/testify/assert"
10+
"strings"
911
)
1012

1113
func TestMissingHostOrUser(t *testing.T) {
@@ -23,6 +25,7 @@ func TestMissingKeyOrPassword(t *testing.T) {
2325
Host: []string{"localhost"},
2426
UserName: "ubuntu",
2527
},
28+
os.Stdout,
2629
}
2730

2831
err := plugin.Exec()
@@ -39,6 +42,7 @@ func TestSetPasswordAndKey(t *testing.T) {
3942
Password: "1234",
4043
Key: "1234",
4144
},
45+
os.Stdout,
4246
}
4347

4448
err := plugin.Exec()
@@ -327,9 +331,132 @@ func Test_escapeArg(t *testing.T) {
327331
}
328332
for _, tt := range tests {
329333
t.Run(tt.name, func(t *testing.T) {
330-
if got := escapeArg(tt.args.arg); got != tt.want {
331-
t.Errorf("escapeArg() = %v, want %v", got, tt.want)
332-
}
334+
got := escapeArg(tt.args.arg)
335+
assert.Equal(t, tt.want, got)
333336
})
334337
}
335338
}
339+
340+
func TestCommandOutput(t *testing.T) {
341+
var (
342+
buffer bytes.Buffer
343+
expected = `
344+
localhost: ======CMD======
345+
localhost: pwd
346+
whoami
347+
uname
348+
localhost: ======END======
349+
localhost: out: /home/drone-scp
350+
localhost: out: drone-scp
351+
localhost: out: Linux
352+
127.0.0.1: ======CMD======
353+
127.0.0.1: pwd
354+
whoami
355+
uname
356+
127.0.0.1: ======END======
357+
127.0.0.1: out: /home/drone-scp
358+
127.0.0.1: out: drone-scp
359+
127.0.0.1: out: Linux
360+
`
361+
)
362+
363+
plugin := Plugin{
364+
Config: Config{
365+
Host: []string{"localhost", "127.0.0.1"},
366+
UserName: "drone-scp",
367+
Port: 22,
368+
KeyPath: "./tests/.ssh/id_rsa",
369+
Script: []string{
370+
"pwd",
371+
"whoami",
372+
"uname",
373+
},
374+
CommandTimeout: 60,
375+
Sync: true,
376+
},
377+
Writer: &buffer,
378+
}
379+
380+
err := plugin.Exec()
381+
assert.Nil(t, err)
382+
383+
assert.Equal(t, unindent(expected), unindent(buffer.String()))
384+
}
385+
386+
func TestEnvOutput(t *testing.T) {
387+
var (
388+
buffer bytes.Buffer
389+
expected = `
390+
======CMD======
391+
echo "[${ENV_1}]"
392+
echo "[${ENV_2}]"
393+
echo "[${ENV_3}]"
394+
echo "[${ENV_4}]"
395+
echo "[${ENV_5}]"
396+
echo "[${ENV_6}]"
397+
echo "[${ENV_7}]"
398+
======END======
399+
======ENV======
400+
ENV_1='test'
401+
ENV_2='test test'
402+
ENV_3='test '
403+
ENV_4=' test test '
404+
ENV_5='test'\'''
405+
ENV_6='test"'
406+
ENV_7='test,!#;?.@$~'\''"'
407+
======END======
408+
out: [test]
409+
out: [test test]
410+
out: [test ]
411+
out: [ test test ]
412+
out: [test']
413+
out: [test"]
414+
out: [test,!#;?.@$~'"]
415+
`
416+
)
417+
418+
os.Setenv("ENV_1", `test`)
419+
os.Setenv("ENV_2", `test test`)
420+
os.Setenv("ENV_3", `test `)
421+
os.Setenv("ENV_4", ` test test `)
422+
os.Setenv("ENV_5", `test'`)
423+
os.Setenv("ENV_6", `test"`)
424+
os.Setenv("ENV_7", `test,!#;?.@$~'"`)
425+
426+
plugin := Plugin{
427+
Config: Config{
428+
Host: []string{"localhost"},
429+
UserName: "drone-scp",
430+
Port: 22,
431+
KeyPath: "./tests/.ssh/id_rsa",
432+
Envs: []string{"env_1", "env_2", "env_3", "env_4", "env_5", "env_6", "env_7"},
433+
Debug: true,
434+
Script: []string{
435+
`echo "[${ENV_1}]"`,
436+
`echo "[${ENV_2}]"`,
437+
`echo "[${ENV_3}]"`,
438+
`echo "[${ENV_4}]"`,
439+
`echo "[${ENV_5}]"`,
440+
`echo "[${ENV_6}]"`,
441+
`echo "[${ENV_7}]"`,
442+
},
443+
CommandTimeout: 10,
444+
Proxy: easyssh.DefaultConfig{
445+
Server: "localhost",
446+
User: "drone-scp",
447+
Port: "22",
448+
KeyPath: "./tests/.ssh/id_rsa",
449+
},
450+
},
451+
Writer: &buffer,
452+
}
453+
454+
err := plugin.Exec()
455+
assert.Nil(t, err)
456+
457+
assert.Equal(t, unindent(expected), unindent(buffer.String()))
458+
}
459+
460+
func unindent(text string) string {
461+
return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
462+
}

0 commit comments

Comments
 (0)