зеркало из https://github.com/mozilla/mig.git
[minor] move run action from file logic into it's own function.
To make the code more easy to follow, I moved the logic for loading/running an action from a file into it's own function "loadActionFromFile"
This commit is contained in:
Родитель
f316b3d752
Коммит
e780f7151f
|
@ -59,15 +59,18 @@ func main() {
|
|||
|
||||
// parse command line argument
|
||||
// -m selects the mode {agent, filechecker, ...}
|
||||
var debug = flag.Bool("d", false, "Debug mode: run in foreground, log to stdout.")
|
||||
var mode = flag.String("m", "agent", "Module to run (eg. agent, filechecker).")
|
||||
var file = flag.String("i", "/path/to/file", "Load action from file.")
|
||||
var config = flag.String("c", "/etc/mig/mig-agent.cfg", "Load configuration from file.")
|
||||
var query = flag.String("q", "somequery", "Send query to the agent's socket, print response to stdout and exit.")
|
||||
var foreground = flag.Bool("f", false, "Agent will fork into background by default. Except if this flag is set.")
|
||||
var upgrading = flag.Bool("u", false, "Used while upgrading an agent, means that this agent is started by another agent.")
|
||||
var pretty = flag.Bool("p", false, "When running a module, pretty print the results instead of returning JSON.")
|
||||
var showversion = flag.Bool("V", false, "Print Agent version to stdout and exit.")
|
||||
var (
|
||||
debug = flag.Bool("d", false, "Debug mode: run in foreground, log to stdout.")
|
||||
mode = flag.String("m", "agent", "Module to run (eg. agent, filechecker).")
|
||||
file = flag.String("i", "/path/to/file", "Load action from file.")
|
||||
config = flag.String("c", "/etc/mig/mig-agent.cfg", "Load configuration from file.")
|
||||
query = flag.String("q", "somequery", "Send query to the agent's socket, print response to stdout and exit.")
|
||||
foreground = flag.Bool("f", false, "Agent will fork into background by default. Except if this flag is set.")
|
||||
upgrading = flag.Bool("u", false, "Used while upgrading an agent, means that this agent is started by another agent.")
|
||||
pretty = flag.Bool("p", false, "When running a module, pretty print the results instead of returning JSON.")
|
||||
showversion = flag.Bool("V", false, "Print Agent version to stdout and exit.")
|
||||
)
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *showversion {
|
||||
|
@ -92,31 +95,12 @@ func main() {
|
|||
}
|
||||
|
||||
if *file != "/path/to/file" {
|
||||
// get input data from file
|
||||
action, err := mig.ActionFromFile(*file)
|
||||
res, err := loadActionFromFile(*file, *pretty)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Println(err)
|
||||
os.Exit(10)
|
||||
}
|
||||
var cmd mig.Command
|
||||
// launch each operation consecutively
|
||||
for _, op := range action.Operations {
|
||||
args, err := json.Marshal(op.Parameters)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
out := runModuleDirectly(op.Module, args, *pretty)
|
||||
var res modules.Result
|
||||
err = json.Unmarshal([]byte(out), &res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cmd.Results = append(cmd.Results, res)
|
||||
}
|
||||
jCmd, err := json.MarshalIndent(cmd.Results, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", jCmd)
|
||||
fmt.Println(res)
|
||||
goto exit
|
||||
}
|
||||
|
||||
|
@ -156,6 +140,59 @@ func main() {
|
|||
exit:
|
||||
}
|
||||
|
||||
// loadActionFromFile loads an action from a file, runs it and returns the results encoded as a json list.
|
||||
func loadActionFromFile(file string, prettyPrint bool) (res string, err error) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
err = fmt.Errorf("loadActionFromFile() -> %v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
// get input data from file
|
||||
action, err := mig.ActionFromFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cmd, err := executeAction(action, prettyPrint)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// results to json list.
|
||||
jcmd, err := json.MarshalIndent(cmd.Results, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return string(jcmd), err
|
||||
}
|
||||
|
||||
// executeAction runs a single mig.Action
|
||||
func executeAction(action mig.Action, prettyPrint bool) (cmd mig.Command, err error) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
err = fmt.Errorf("executeAction() -> %v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
// launch each operation consecutively
|
||||
for _, op := range action.Operations {
|
||||
args, err := json.Marshal(op.Parameters)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
out := runModuleDirectly(op.Module, args, prettyPrint)
|
||||
var res modules.Result
|
||||
err = json.Unmarshal([]byte(out), &res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cmd.Results = append(cmd.Results, res)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// runModuleDirectly executes a module and displays the results on stdout
|
||||
func runModuleDirectly(mode string, args []byte, pretty bool) (out string) {
|
||||
if _, ok := modules.Available[mode]; !ok {
|
||||
|
|
Загрузка…
Ссылка в новой задаче