From a650f4ef687bf159ae8affa0f45ebae4b941fec4 Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Mon, 22 Jan 2024 11:01:59 -0800 Subject: [PATCH] feat: use the filename when present #374 --- cmd/registry/apis/versions/specs/crtspec.go | 8 +++---- .../client/registry/apis/versions/specs.go | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/registry/apis/versions/specs/crtspec.go b/cmd/registry/apis/versions/specs/crtspec.go index db5bc7c75..160316e0e 100644 --- a/cmd/registry/apis/versions/specs/crtspec.go +++ b/cmd/registry/apis/versions/specs/crtspec.go @@ -16,6 +16,7 @@ package specs import ( "encoding/base64" + "path/filepath" "internal/apiclient" @@ -40,6 +41,7 @@ var CreateSpecCmd = &cobra.Command{ if err != nil { return err } + fileName := filepath.Base(filePath) contents := base64.URLEncoding.EncodeToString(fileContents) _, err = versions.CreateSpec(apiName, apiVersion, apiSpecID, name, fileName, description, sourceURI, contents, labels, annotations) @@ -48,8 +50,8 @@ var CreateSpecCmd = &cobra.Command{ } var ( - labels, annotations map[string]string - apiSpecID, fileName, description, sourceURI, filePath string + labels, annotations map[string]string + apiSpecID, description, sourceURI, filePath string ) func init() { @@ -61,8 +63,6 @@ func init() { "", "API Spec Id") CreateSpecCmd.Flags().StringVarP(&name, "name", "n", "", "API Version Spec name") - CreateSpecCmd.Flags().StringVarP(&fileName, "file-name", "", - "", "A possibly-hierarchical name used to refer to the spec from other specs") CreateSpecCmd.Flags().StringVarP(&description, "desc", "d", "", "A detailed description for the spec") CreateSpecCmd.Flags().StringVarP(&filePath, "file-path", "f", diff --git a/internal/client/registry/apis/versions/specs.go b/internal/client/registry/apis/versions/specs.go index 89203bb26..9b234dd73 100644 --- a/internal/client/registry/apis/versions/specs.go +++ b/internal/client/registry/apis/versions/specs.go @@ -15,6 +15,8 @@ package versions import ( + "encoding/json" + "fmt" "net/url" "path" "strconv" @@ -114,9 +116,26 @@ func GetSpec(apiName string, apiVersion string, name string) (respBody []byte, e // GetSpecContents func GetSpecContents(apiName string, apiVersion string, name string) (err error) { + apiclient.ClientPrintHttpResponse.Set(false) + var specMap map[string]interface{} + var specFileName string + respBody, err := GetSpec(apiName, apiVersion, name) + if err != nil { + return err + } + err = json.Unmarshal(respBody, &specMap) + if err != nil { + return err + } + if specMap["filename"] != "" { + specFileName = fmt.Sprintf("%s", specMap["filename"]) + } else { + specFileName = name + ".txt" + } + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) u, _ := url.Parse(apiclient.GetApigeeRegistryURL()) u.Path = path.Join(u.Path, "apis", apiName, "versions", apiVersion, "specs", name+":getContents") - return apiclient.DownloadResource(u.String(), name+".txt", "", true) + return apiclient.DownloadResource(u.String(), specFileName, "", true) } // ListSpecs