github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/dump.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 "gopkg.in/juju/names.v2" 11 12 jujucmd "github.com/juju/juju/cmd" 13 "github.com/juju/juju/cmd/modelcmd" 14 "github.com/juju/juju/cmd/output" 15 ) 16 17 // NewDumpCommand returns a fully constructed dump-model command. 18 func NewDumpCommand() cmd.Command { 19 return modelcmd.Wrap(&dumpCommand{}) 20 } 21 22 type dumpCommand struct { 23 modelcmd.ModelCommandBase 24 out cmd.Output 25 api DumpModelAPI 26 27 simplified bool 28 } 29 30 const dumpModelHelpDoc = ` 31 Calls export on the model's database representation and writes the 32 resulting YAML to stdout. 33 34 Examples: 35 36 juju dump-model 37 juju dump-model -m mymodel 38 39 See also: 40 models 41 ` 42 43 // Info implements Command. 44 func (c *dumpCommand) Info() *cmd.Info { 45 return jujucmd.Info(&cmd.Info{ 46 Name: "dump-model", 47 Purpose: "Displays the database agnostic representation of the model.", 48 Doc: dumpModelHelpDoc, 49 }) 50 } 51 52 // SetFlags implements Command. 53 func (c *dumpCommand) SetFlags(f *gnuflag.FlagSet) { 54 c.ModelCommandBase.SetFlags(f) 55 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 56 f.BoolVar(&c.simplified, "simplified", false, "Dump a simplified partial model") 57 } 58 59 // Init implements Command. 60 func (c *dumpCommand) Init(args []string) error { 61 return cmd.CheckEmpty(args) 62 } 63 64 // DumpModelAPI specifies the used function calls of the ModelManager. 65 type DumpModelAPI interface { 66 Close() error 67 DumpModel(names.ModelTag, bool) (map[string]interface{}, error) 68 } 69 70 func (c *dumpCommand) getAPI() (DumpModelAPI, error) { 71 if c.api != nil { 72 return c.api, nil 73 } 74 return c.ModelCommandBase.NewModelManagerAPIClient() 75 } 76 77 // Run implements Command. 78 func (c *dumpCommand) Run(ctx *cmd.Context) error { 79 client, err := c.getAPI() 80 if err != nil { 81 return err 82 } 83 defer client.Close() 84 85 _, modelDetails, err := c.ModelCommandBase.ModelDetails() 86 if err != nil { 87 return errors.Annotate(err, "getting model details") 88 } 89 90 modelTag := names.NewModelTag(modelDetails.ModelUUID) 91 results, err := client.DumpModel(modelTag, c.simplified) 92 if err != nil { 93 return err 94 } 95 96 return c.out.Write(ctx, results) 97 }