[TASK] ssh manager handler for string output

This commit is contained in:
Martin Geno 2017-05-06 15:09:59 +02:00
parent 50cdc796de
commit f27a058c5a
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
2 changed files with 22 additions and 9 deletions

View File

@ -10,9 +10,24 @@ import (
"github.com/FreifunkBremen/freifunkmanager/lib/log"
)
type SSHRunResultHandler func([]byte, error)
type SSHResultHandler func([]byte, error)
func (m *Manager) RunEverywhere(cmd string, handler SSHRunResultHandler) {
type SSHResultStringHandler func(string, error)
func SSHResultToString(result []byte) string {
if len(result) > 0 {
result = result[:len(result)-1]
}
return string(result)
}
func SSHResultToStringHandler(handler SSHResultStringHandler) SSHResultHandler {
return func(result []byte, err error) {
handler(SSHResultToString(result), err)
}
}
func (m *Manager) RunEverywhere(cmd string, handler SSHResultHandler) {
for host, client := range m.clients {
result, err := m.run(host, client, cmd)
handler(result, err)

View File

@ -16,18 +16,16 @@ func TestRun(t *testing.T) {
mgmt.ConnectTo(net.ParseIP("2a06:8782:ffbb:1337::127"))
mgmt.RunEverywhere("echo 13", func(result []byte, err error) {
mgmt.RunEverywhere("echo 13", SSHResultToStringHandler(func(result string, err error) {
assert.NoError(err)
result = result[:len(result)-1]
assert.Equal([]byte{'1', '3'}, result)
})
assert.Equal("13", result)
}))
result, err := mgmt.RunOn(net.ParseIP("2a06:8782:ffbb:1337::127"), "echo 16")
assert.NoError(err)
result = result[:len(result)-1]
resultInt, _ := strconv.Atoi(string(result))
str := SSHResultToString(result)
resultInt, _ := strconv.Atoi(str)
assert.Equal(16, resultInt)