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 25180bb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
49 changes: 43 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,19 @@ 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)

api = version.APIs[name]
return
}

func GetMetadataPrefix(language string) string {
if language == "en" {
return "en-US"
Expand Down
19 changes: 13 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,14 @@ 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.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)
}
}
}
}
Expand Down

0 comments on commit 25180bb

Please sign in to comment.