From 9693b90772cec20ca723c7bbc720e89add68c6de Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Tue, 26 Dec 2023 15:53:09 +0800 Subject: [PATCH] lib: add description for api --- newmeta/meta.go | 52 +++++++++++++++++++++++++++++++++++++++----- newmeta/meta_test.go | 12 ++++++++++ openapi/library.go | 24 +++++++++++++++----- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/newmeta/meta.go b/newmeta/meta.go index df4069f67..251fb1cbe 100644 --- a/newmeta/meta.go +++ b/newmeta/meta.go @@ -41,12 +41,36 @@ type Product struct { } type Endpoint struct { - RegionId string `json:"regionId"` - Name string `json:"regionName"` - Version string `json:"areaId"` - EndpointType string `json:"areaName"` - Public string `json:"public"` - VPC string `json:"vpc"` + RegionId string `json:"regionId"` + Name string `json:"regionName"` + AreaId string `json:"areaId"` + AreaName string `json:"areaName"` + Public string `json:"public"` + VPC string `json:"vpc"` +} + +// { +// "version": "2017-09-12", +// "style": "rpc", +// "apis": { +// "ActiveFlowLog": { +// "title": "ActiveFlowLog", +// "summary": "Enables a flow log. After the flow log is enabled, the system collects traffic information about a specified resource.", +// "deprecated": false +// } +// } +// } + +type Version struct { + Version string `json:"version"` + Style string `json:"style"` + APIs map[string]API `json:"apis"` +} + +type API struct { + Title string `json:"title"` + Summary string `json:"summary"` + Deprecated bool `json:"deprecated"` } func GetProductName(language, code string) (name string, err error) { @@ -68,6 +92,22 @@ func GetProductName(language, code string) (name string, err error) { return } +func GetAPI(language, code, name string) (api *API, err error) { + content, err := GetMetadata(language, "/"+strings.ToLower(code)+"/version.json") + if err != nil { + return + } + + version := new(Version) + err = json.Unmarshal(content, &version) + + if found, ok := version.APIs[name]; ok { + api = &found + } + + return +} + func GetMetadataPrefix(language string) string { if language == "en" { return "en-US" diff --git a/newmeta/meta_test.go b/newmeta/meta_test.go index 035a2be44..13f134335 100644 --- a/newmeta/meta_test.go +++ b/newmeta/meta_test.go @@ -17,3 +17,15 @@ func TestGetMetadata(t *testing.T) { assert.Nil(t, err) assert.Greater(t, len(content), 100) } + +func TestGetAPI(t *testing.T) { + api, err := GetAPI("en", "ecs", "DescribeRegions") + assert.Nil(t, err) + assert.Equal(t, "DescribeRegions", api.Title) + assert.Equal(t, "Queries available Alibaba Cloud regions.", api.Summary) + assert.Equal(t, false, api.Deprecated) + + api2, err := GetAPI("en", "ecs", "Invalid") + assert.Nil(t, err) + assert.Nil(t, api2) +} diff --git a/openapi/library.go b/openapi/library.go index 349c3b1d2..4624323e9 100644 --- a/openapi/library.go +++ b/openapi/library.go @@ -79,13 +79,13 @@ func (a *Library) PrintProductUsage(productCode string, withApi bool) error { } if product.ApiStyle == "rpc" { - cli.Printf(a.writer, "\nUsage:\n aliyun %s --parameter1 value1 --parameter2 value2 ...\n", product.Code) + cli.Printf(a.writer, "\nUsage:\n aliyun %s --parameter1 value1 --parameter2 value2 ...\n", strings.ToLower(product.Code)) } else { - cli.Printf(a.writer, "\nUsage 1:\n aliyun %s [GET|PUT|POST|DELETE] --body \"...\" \n", product.Code) - cli.Printf(a.writer, "\nUsage 2 (For API with NO PARAMS in PathPattern only.):\n aliyun %s --parameter1 value1 --parameter2 value2 ... --body \"...\"\n", product.Code) + cli.Printf(a.writer, "\nUsage 1:\n aliyun %s [GET|PUT|POST|DELETE] --body \"...\" \n", strings.ToLower(product.Code)) + cli.Printf(a.writer, "\nUsage 2 (For API with NO PARAMS in PathPattern only.):\n aliyun %s --parameter1 value1 --parameter2 value2 ... --body \"...\"\n", strings.ToLower(product.Code)) } - - cli.Printf(a.writer, "\nProduct: %s (%s)\n", product.Code, product.Name[i18n.GetLanguage()]) + productName, _ := newmeta.GetProductName(i18n.GetLanguage(), product.Code) + cli.Printf(a.writer, "\nProduct: %s (%s)\n", product.Code, productName) cli.Printf(a.writer, "Version: %s \n", product.Version) if withApi { @@ -104,7 +104,19 @@ func (a *Library) PrintProductUsage(productCode string, withApi bool) error { ptn := fmt.Sprintf(" %%-%ds : %%s %%s\n", maxNameLen+1) cli.PrintfWithColor(a.writer, cli.Green, ptn, apiName, api.Method, api.PathPattern) } else { - cli.PrintfWithColor(a.writer, cli.Green, " %s\n", apiName) + api, _ := newmeta.GetAPI(i18n.GetLanguage(), productCode, apiName) + if api != nil { + // use new api metadata + if api.Deprecated { + fmt := fmt.Sprintf(" %%-%ds [Deprecated]%%s\n", maxNameLen+1) + cli.PrintfWithColor(a.writer, cli.Green, fmt, apiName, api.Summary) + } else { + fmt := fmt.Sprintf(" %%-%ds %%s\n", maxNameLen+1) + cli.PrintfWithColor(a.writer, cli.Green, fmt, apiName, api.Summary) + } + } else { + cli.PrintfWithColor(a.writer, cli.Green, " %s\n", apiName) + } } } }