[TASK] ssh manager handler for string output
This commit is contained in:
parent
50cdc796de
commit
f27a058c5a
19
ssh/run.go
19
ssh/run.go
|
@ -10,9 +10,24 @@ import (
|
||||||
"github.com/FreifunkBremen/freifunkmanager/lib/log"
|
"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 {
|
for host, client := range m.clients {
|
||||||
result, err := m.run(host, client, cmd)
|
result, err := m.run(host, client, cmd)
|
||||||
handler(result, err)
|
handler(result, err)
|
||||||
|
|
|
@ -16,18 +16,16 @@ func TestRun(t *testing.T) {
|
||||||
|
|
||||||
mgmt.ConnectTo(net.ParseIP("2a06:8782:ffbb:1337::127"))
|
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)
|
assert.NoError(err)
|
||||||
|
|
||||||
result = result[:len(result)-1]
|
assert.Equal("13", result)
|
||||||
|
}))
|
||||||
assert.Equal([]byte{'1', '3'}, result)
|
|
||||||
})
|
|
||||||
result, err := mgmt.RunOn(net.ParseIP("2a06:8782:ffbb:1337::127"), "echo 16")
|
result, err := mgmt.RunOn(net.ParseIP("2a06:8782:ffbb:1337::127"), "echo 16")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
result = result[:len(result)-1]
|
str := SSHResultToString(result)
|
||||||
resultInt, _ := strconv.Atoi(string(result))
|
resultInt, _ := strconv.Atoi(str)
|
||||||
|
|
||||||
assert.Equal(16, resultInt)
|
assert.Equal(16, resultInt)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue