From 376d1acc7dfdb8214b9e1162e62ea2fe01f2f801 Mon Sep 17 00:00:00 2001 From: Aaron Meihm Date: Fri, 4 Sep 2015 13:41:31 -0500 Subject: [PATCH] [minor] add -a flag for mig-cmd that can be used to print action json --- action.go | 10 ++++++++++ client/mig/main.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/action.go b/action.go index a7748727..fa168621 100644 --- a/action.go +++ b/action.go @@ -330,3 +330,13 @@ func (a Action) PrintCounters() { } fmt.Fprintf(os.Stderr, "%s\n", out) } + +// Return the an indented JSON string representing the action suitable for +// display +func (a Action) IndentedString() (string, error) { + buf, err := json.MarshalIndent(a, "", " ") + if err != nil { + return "", err + } + return string(buf), nil +} diff --git a/client/mig/main.go b/client/mig/main.go index 5c6977ea..5f5e3030 100644 --- a/client/mig/main.go +++ b/client/mig/main.go @@ -28,6 +28,7 @@ usage: %s --- Global options --- +-a display action json that would be used and exit -c path to an alternative config file. If not set, use ~/.migrc -e time after which the action expires. 60 seconds by default. example: -e 300s (5 minutes) @@ -75,6 +76,7 @@ func main() { op mig.Operation a mig.Action migrc, show, render, target, expiration, afile string + showaction bool verbose bool modargs []string run interface{} @@ -87,6 +89,7 @@ func main() { homedir := client.FindHomedir() fs := flag.NewFlagSet("mig flag", flag.ContinueOnError) fs.Usage = continueOnFlagError + fs.BoolVar(&showaction, "a", false, "display action json that would be used and exit") fs.StringVar(&migrc, "c", homedir+"/.migrc", "alternative configuration file") fs.StringVar(&show, "show", "found", "type of results to show") fs.StringVar(&render, "render", "text", "results rendering mode") @@ -121,6 +124,14 @@ func main() { panic(err) } fmt.Fprintf(os.Stderr, "[info] launching action from file, all flags are ignored\n") + if showaction { + actionstr, err := a.IndentedString() + if err != nil { + panic(err) + } + fmt.Fprintf(os.Stdout, "%v\n", actionstr) + os.Exit(0) + } goto readytolaunch } @@ -208,6 +219,15 @@ func main() { } a.Target = target + if showaction { + actionstr, err := a.IndentedString() + if err != nil { + panic(err) + } + fmt.Fprintf(os.Stdout, "%v\n", actionstr) + os.Exit(0) + } + readytolaunch: // instanciate an API client conf, err = client.ReadConfiguration(migrc)