From ff4f5e99f93b104e6a1314bd8152ee64ed105ee1 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 18 Dec 2021 19:41:24 +0100 Subject: [PATCH] add reflexion method details --- .../piwigocli/reflexion_method_details.go | 77 ++++++++++++++++--- 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/internal/piwigocli/reflexion_method_details.go b/internal/piwigocli/reflexion_method_details.go index c7f08d9..fc5176a 100644 --- a/internal/piwigocli/reflexion_method_details.go +++ b/internal/piwigocli/reflexion_method_details.go @@ -1,6 +1,8 @@ package piwigocli import ( + "encoding/json" + "fmt" "net/url" "os" @@ -13,8 +15,40 @@ type ReflexionMethodDetailsCommand struct { MethodName string `short:"m" long:"method-name" description:"Method name to details"` } +type ReflexionMethodDetailsParams struct { + Name string `json:"name"` + Optional bool `json:"optional"` + Type string `json:"type"` + AcceptArray bool `json:"acceptArray"` + DefaultValue interface{} `json:"defaultValue"` + MaxValue interface{} `json:"maxValue"` + Info string `json:"info"` +} + +type ReflexionMethodDetailsOptions struct { + Admin bool `json:"admin_only"` + PostOnly bool `json:"post_only"` +} + type ReflexionMethodDetailsResult struct { - Description string `json:"description"` + Name string `json:"name"` + Description string `json:"description"` + Options ReflexionMethodDetailsOptions `json:"options"` + Parameters []ReflexionMethodDetailsParams `json:"params"` +} + +func (j *ReflexionMethodDetailsOptions) UnmarshalJSON(data []byte) error { + var r interface{} + if err := json.Unmarshal(data, &r); err != nil { + return err + } + + switch r := r.(type) { + case map[string]interface{}: + j.Admin, _ = r["admin_only"].(bool) + j.PostOnly, _ = r["post_only"].(bool) + } + return nil } func (c *ReflexionMethodDetailsCommand) Execute(args []string) error { @@ -36,17 +70,38 @@ func (c *ReflexionMethodDetailsCommand) Execute(args []string) error { return err } - desc := strip.StripTags(result.Description) + t1 := table.NewWriter() + t1.AppendRow(table.Row{"Name", result.Name}) + t1.AppendSeparator() + t1.AppendRow(table.Row{"Description", strip.StripTags(result.Description)}) + t1.AppendRow(table.Row{"Admin", result.Options.Admin}) + t1.AppendRow(table.Row{"Post Only", result.Options.PostOnly}) + t1.SetOutputMirror(os.Stdout) + t1.SetStyle(table.StyleLight) + t1.Render() - t := table.NewWriter() - t.AppendRow(table.Row{"Method", c.MethodName}) - t.AppendSeparator() - t.AppendRow(table.Row{"Description", desc}) - t.AppendSeparator() - t.AppendRow(table.Row{"Parameters"}) - t.SetOutputMirror(os.Stdout) - t.SetStyle(table.StyleLight) - t.Render() + if len(result.Parameters) > 0 { + fmt.Println("Parameters:") + t2 := table.NewWriter() + t2.AppendHeader(table.Row{"Name", "Type", "Optional", "Accept Array", "Default", "Max", "Info"}) + t2.AppendSeparator() + for _, param := range result.Parameters { + t2.AppendRow(table.Row{ + param.Name, + param.Type, + param.Optional, + param.AcceptArray, + fmt.Sprintf("%v", param.DefaultValue), + fmt.Sprintf("%v", param.MaxValue), + param.Info, + }) + } + t2.SetOutputMirror(os.Stdout) + t2.SetStyle(table.StyleLight) + t2.Style().Options.SeparateHeader = true + t2.Style().Options.DrawBorder = true + t2.Render() + } return nil }