Skip to content

Commit

Permalink
lib: add description for api
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Dec 26, 2023
1 parent 75b4869 commit e99a49d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
52 changes: 46 additions & 6 deletions newmeta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions newmeta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 18 additions & 6 deletions openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ApiName> --parameter1 value1 --parameter2 value2 ...\n", product.Code)
cli.Printf(a.writer, "\nUsage:\n aliyun %s <ApiName> --parameter1 value1 --parameter2 value2 ...\n", strings.ToLower(product.Code))
} else {
cli.Printf(a.writer, "\nUsage 1:\n aliyun %s [GET|PUT|POST|DELETE] <PathPattern> --body \"...\" \n", product.Code)
cli.Printf(a.writer, "\nUsage 2 (For API with NO PARAMS in PathPattern only.):\n aliyun %s <ApiName> --parameter1 value1 --parameter2 value2 ... --body \"...\"\n", product.Code)
cli.Printf(a.writer, "\nUsage 1:\n aliyun %s [GET|PUT|POST|DELETE] <PathPattern> --body \"...\" \n", strings.ToLower(product.Code))
cli.Printf(a.writer, "\nUsage 2 (For API with NO PARAMS in PathPattern only.):\n aliyun %s <ApiName> --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 {
Expand All @@ -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)
}
}
}
}
Expand Down

0 comments on commit e99a49d

Please sign in to comment.