|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/docker/infrakit/pkg/discovery" |
| 8 | + "github.com/docker/infrakit/pkg/plugin" |
| 9 | + "github.com/docker/infrakit/pkg/rpc/client" |
| 10 | + "github.com/docker/infrakit/pkg/template" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// infoCommand creates a cobra Command that prints build version information. |
| 15 | +func infoCommand(plugins func() discovery.Plugins) *cobra.Command { |
| 16 | + cmd := &cobra.Command{ |
| 17 | + Use: "info", |
| 18 | + Short: "print plugin info", |
| 19 | + PersistentPreRunE: func(c *cobra.Command, args []string) error { |
| 20 | + // Cobra doesn't go past one level of PersistentRunE |
| 21 | + return upTree(c, func(x *cobra.Command, argv []string) error { |
| 22 | + if x.PersistentPreRunE != nil { |
| 23 | + return x.PersistentPreRunE(x, argv) |
| 24 | + } |
| 25 | + return nil |
| 26 | + }) |
| 27 | + }, |
| 28 | + } |
| 29 | + name := cmd.PersistentFlags().String("name", "", "Name of plugin") |
| 30 | + raw := cmd.PersistentFlags().Bool("raw", false, "True to show raw data") |
| 31 | + |
| 32 | + api := &cobra.Command{ |
| 33 | + Use: "api", |
| 34 | + Short: "Show api / RPC interface supported by the plugin of the given name", |
| 35 | + } |
| 36 | + |
| 37 | + templateFuncs := &cobra.Command{ |
| 38 | + Use: "template", |
| 39 | + Short: "Show template functions supported by the plugin, if the plugin uses template for configuration.", |
| 40 | + } |
| 41 | + cmd.AddCommand(api, templateFuncs) |
| 42 | + |
| 43 | + api.RunE = func(cmd *cobra.Command, args []string) error { |
| 44 | + endpoint, err := plugins().Find(plugin.Name(*name)) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + infoClient := client.NewPluginInfoClient(endpoint.Address) |
| 50 | + info, err := infoClient.GetInfo() |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + if *raw { |
| 56 | + buff, err := json.MarshalIndent(info, "", " ") |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + fmt.Println(string(buff)) |
| 62 | + return nil |
| 63 | + } |
| 64 | + // render a view using template |
| 65 | + renderer, err := template.NewTemplate("str://"+apiViewTemplate, template.Options{}) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + view, err := renderer.AddDef("plugin", *name).Render(info) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + fmt.Print(view) |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + templateFuncs.RunE = func(cmd *cobra.Command, args []string) error { |
| 80 | + endpoint, err := plugins().Find(plugin.Name(*name)) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + infoClient := client.NewPluginInfoClient(endpoint.Address) |
| 86 | + info, err := infoClient.GetFunctions() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + if *raw { |
| 92 | + buff, err := json.MarshalIndent(info, "", " ") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Println(string(buff)) |
| 98 | + return nil |
| 99 | + } |
| 100 | + // render a view using template |
| 101 | + renderer, err := template.NewTemplate("str://"+funcsViewTemplate, template.Options{}) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + view, err := renderer.AddDef("plugin", *name).Render(info) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Print(view) |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + return cmd |
| 116 | +} |
| 117 | + |
| 118 | +const ( |
| 119 | + apiViewTemplate = ` |
| 120 | +Plugin: {{ref "plugin"}} |
| 121 | +Implements: {{range $spi := .Implements}}{{$spi.Name}}/{{$spi.Version}} {{end}} |
| 122 | +Interfaces: {{range $iface := .Interfaces}} |
| 123 | + SPI: {{$iface.Name}}/{{$iface.Version}} |
| 124 | + RPC: {{range $method := $iface.Methods}} |
| 125 | + Method: {{$method.Request | q "method" }} |
| 126 | + Request: |
| 127 | + {{$method.Request | to_json_format " " " "}} |
| 128 | +
|
| 129 | + Response: |
| 130 | + {{$method.Response | to_json_format " " " "}} |
| 131 | +
|
| 132 | + ------------------------- |
| 133 | + {{end}} |
| 134 | +{{end}} |
| 135 | +` |
| 136 | + |
| 137 | + funcsViewTemplate = ` |
| 138 | +{{range $category, $functions := .}} |
| 139 | +{{ref "plugin"}}/{{$category}} _________________________________________________________________________________________ |
| 140 | + {{range $f := $functions}} |
| 141 | + Name: {{$f.Name}} |
| 142 | + Description: {{join "\n " $f.Description}} |
| 143 | + Function: {{$f.Function}} |
| 144 | + Usage: {{$f.Usage}} |
| 145 | +
|
| 146 | + ------------------------- |
| 147 | + {{end}} |
| 148 | +
|
| 149 | +{{end}} |
| 150 | +` |
| 151 | +) |
0 commit comments