/
command_meshage.go
60 lines (49 loc) · 1.48 KB
/
command_meshage.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
// Copyright (2012) Sandia Corporation.
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
package main
import (
"minicli"
log "minilog"
"sync"
)
// SourceMeshage is used to prevent some commands running via meshage such as
// `read` and `mesh send`.
const SourceMeshage = "meshage"
var meshageCommandLock sync.Mutex
func meshageHandler() {
for {
m := <-meshageCommandChan
go func() {
mCmd := m.Body.(meshageCommand)
cmd, err := minicli.Compile(mCmd.Original)
if err != nil {
log.Error("invalid command from mesh: `%s`", mCmd.Original)
return
}
// Copy the flags at each level of nested command
for c, c2 := cmd, &mCmd.Command; c != nil && c2 != nil; {
c.Record = c2.Record
c.Source = c2.Source
c.Preprocess = c2.Preprocess
c, c2 = c.Subcommand, c2.Subcommand
}
resps := []minicli.Responses{}
for resp := range RunCommands(cmd) {
resps = append(resps, resp)
}
if len(resps) > 1 || len(resps[0]) > 1 {
// This should never happen because the only commands that
// return multiple responses are `read` and `mesh send` which
// aren't supposed to be sent across meshage.
log.Error("unsure how to process multiple responses!!")
}
resp := meshageResponse{Response: *resps[0][0], TID: mCmd.TID}
recipient := []string{m.Source}
_, err = meshageNode.Set(recipient, resp)
if err != nil {
log.Errorln(err)
}
}()
}
}