diff --git a/cmd/apicategories/get.go b/cmd/apicategories/get.go index ce5cb8f1c..3f19b223d 100644 --- a/cmd/apicategories/get.go +++ b/cmd/apicategories/get.go @@ -27,14 +27,27 @@ import ( // GetCmd to get a catalog items var GetCmd = &cobra.Command{ Use: "get", - Short: "Gets an API Category by ID", - Long: "Gets an API Category by ID", + Short: "Gets an API Category by ID or name", + Long: "Gets an API Category by ID or name", Args: func(cmd *cobra.Command, args []string) (err error) { + if siteid == "" { + return fmt.Errorf("siteid is a mandatory parameter") + } + if name == "" && id == "" { + return fmt.Errorf("name or id must be set as a parameter") + } + if name != "" && id != "" { + return fmt.Errorf("name and id cannot be set as a parameter") + } return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - if siteid == "" { - return fmt.Errorf("siteid is a mandatory parameter") + if name != "" { + var payload []byte + if payload, err = apicategories.GetByName(siteid, name); err != nil { + return err + } + return apiclient.PrettyPrint("application/json", payload) } _, err = apicategories.Get(siteid, id) return @@ -44,5 +57,6 @@ var GetCmd = &cobra.Command{ func init() { GetCmd.Flags().StringVarP(&id, "id", "i", "", "API Category ID") - _ = GetCmd.MarkFlagRequired("id") + GetCmd.Flags().StringVarP(&name, "name", "n", + "", "API Catalog Name") } diff --git a/cmd/apidocs/get.go b/cmd/apidocs/get.go index b380cccec..c974f22bf 100644 --- a/cmd/apidocs/get.go +++ b/cmd/apidocs/get.go @@ -32,9 +32,22 @@ var GetCmd = &cobra.Command{ if siteid == "" { return fmt.Errorf("siteid is a mandatory parameter") } + if name == "" && id == "" { + return fmt.Errorf("title or id must be set as a parameter") + } + if name != "" && id != "" { + return fmt.Errorf("title and id cannot be set as a parameter") + } return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if name != "" { + var payload []byte + if payload, err = apidocs.GetByTitle(siteid, name); err != nil { + return err + } + return apiclient.PrettyPrint("application/json", payload) + } _, err = apidocs.Get(siteid, id) return }, @@ -43,5 +56,6 @@ var GetCmd = &cobra.Command{ func init() { GetCmd.Flags().StringVarP(&id, "id", "i", "", "Catalog ID") - _ = GetCmd.MarkFlagRequired("id") + GetCmd.Flags().StringVarP(&name, "title", "", + "", "Catalog Title") } diff --git a/internal/client/apicategories/apicategories.go b/internal/client/apicategories/apicategories.go index cc01a0137..db9a96eba 100644 --- a/internal/client/apicategories/apicategories.go +++ b/internal/client/apicategories/apicategories.go @@ -22,11 +22,15 @@ import ( "net/url" "os" "path" + "reflect" "strings" "internal/apiclient" + "internal/client/sites" "internal/clilog" + + "github.com/thedevsaddam/gojsonq" ) type listapicategories struct { @@ -93,6 +97,37 @@ func Update(siteid string, name string) (respBody []byte, err error) { return respBody, err } +// GetByName +func GetByName(siteid string, name string) (respBody []byte, err error) { + apiclient.ClientPrintHttpResponse.Set(false) + defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + listRespBytes, err := List(siteid) + if err != nil { + return nil, fmt.Errorf("failed to fetch apidocs: %w", err) + } + out := gojsonq.New().JSONString(string(listRespBytes)).From("data").Where("name", "eq", name).First() + if isNil(out) { + return nil, fmt.Errorf("unable to find category with name %s", name) + } + outBytes, err := json.Marshal(out) + if err != nil { + return outBytes, err + } + return outBytes, nil +} + +// from: https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 +func isNil(i interface{}) bool { + if i == nil { + return true + } + switch reflect.TypeOf(i).Kind() { + case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice: + return reflect.ValueOf(i).IsNil() + } + return false +} + // Export func Export(folder string) (err error) { apiclient.ClientPrintHttpResponse.Set(false) diff --git a/internal/client/apidocs/apidocs.go b/internal/client/apidocs/apidocs.go index f9db022ee..95b6d0d48 100644 --- a/internal/client/apidocs/apidocs.go +++ b/internal/client/apidocs/apidocs.go @@ -185,6 +185,39 @@ func Get(siteid string, id string) (respBody []byte, err error) { return respBody, err } +// GetByTitle +func GetByTitle(siteid string, title string) (respBody []byte, err error) { + apiclient.ClientPrintHttpResponse.Set(false) + defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + fullList := listapidocs{} + pageToken := "" + for { + l := listapidocs{} + listRespBytes, err := List(siteid, maxPageSize, pageToken) + if err != nil { + return nil, fmt.Errorf("failed to fetch apidocs: %w", err) + } + err = json.Unmarshal(listRespBytes, &l) + if err != nil { + return nil, fmt.Errorf("failed to unmarshall: %w", err) + } + fullList.Data = append(fullList.Data, l.Data...) + pageToken = l.NextPageToken + if l.NextPageToken == "" { + break + } + } + for _, data := range fullList.Data { + if data.Title == title { + if respBody, err = json.Marshal(data); err != nil { + return nil, err + } + return respBody, nil + } + } + return nil, fmt.Errorf("unable to find apidocs with title %s", title) +} + // List func List(siteid string, pageSize int, pageToken string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL)