github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/dumpdb.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 // NewDumpDBCommand returns a fully constructed dump-db command. 18 func NewDumpDBCommand() cmd.Command { 19 return modelcmd.Wrap(&dumpDBCommand{}) 20 } 21 22 type dumpDBCommand struct { 23 modelcmd.ModelCommandBase 24 out cmd.Output 25 api DumpDBAPI 26 } 27 28 const dumpDBHelpDoc = ` 29 dump-db returns all that is stored in the database for the specified model. 30 31 Examples: 32 33 juju dump-db 34 juju dump-db -m mymodel 35 36 See also: 37 models 38 ` 39 40 // Info implements Command. 41 func (c *dumpDBCommand) Info() *cmd.Info { 42 return jujucmd.Info(&cmd.Info{ 43 Name: "dump-db", 44 Purpose: "Displays the mongo documents for of the model.", 45 Doc: dumpDBHelpDoc, 46 }) 47 } 48 49 // SetFlags implements Command. 50 func (c *dumpDBCommand) SetFlags(f *gnuflag.FlagSet) { 51 c.ModelCommandBase.SetFlags(f) 52 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 53 } 54 55 // Init implements Command. 56 func (c *dumpDBCommand) Init(args []string) error { 57 return cmd.CheckEmpty(args) 58 } 59 60 // DumpDBAPI specifies the used function calls of the ModelManager. 61 type DumpDBAPI interface { 62 Close() error 63 DumpModelDB(names.ModelTag) (map[string]interface{}, error) 64 } 65 66 func (c *dumpDBCommand) getAPI() (DumpDBAPI, error) { 67 if c.api != nil { 68 return c.api, nil 69 } 70 return c.ModelCommandBase.NewModelManagerAPIClient() 71 } 72 73 // Run implements Command. 74 func (c *dumpDBCommand) Run(ctx *cmd.Context) error { 75 client, err := c.getAPI() 76 if err != nil { 77 return errors.Trace(err) 78 } 79 defer client.Close() 80 81 _, modelDetails, err := c.ModelCommandBase.ModelDetails() 82 if err != nil { 83 return errors.Annotate(err, "getting model details") 84 } 85 86 modelTag := names.NewModelTag(modelDetails.ModelUUID) 87 results, err := client.DumpModelDB(modelTag) 88 if err != nil { 89 return err 90 } 91 92 return c.out.Write(ctx, results) 93 }