Skip to content

Commit

Permalink
Merge pull request #85 from zhouwenb/master
Browse files Browse the repository at this point in the history
Add help text and allow calling by name for restful API.
  • Loading branch information
jxyowen authored Nov 1, 2018
2 parents a102d71 + 6d192b0 commit e519173
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions meta/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Api struct {
Name string `json:"name"`
Protocol string `json:"protocol"`
Method string `json:"method"`
PathPattern string `json:"pathPattern"`
Description map[string]string `json:"descriptions,omitempty"`
Parameters []Parameter `json:"parameters"`
Product *Product `json:"-"`
Expand Down
9 changes: 8 additions & 1 deletion openapi/commando.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ func (c *Commando) main(ctx *cli.Context, args []string) error {
// TODO: aliyun pluginName ...
return c.library.PrintProductUsage(productName, true)
} else if len(args) == 2 {
// rpc call
// rpc or restful call
// aliyun <productCode> <method> --param1 value1
product, _ := c.library.GetProduct(args[0])

if product.ApiStyle=="restful" {
api, _ := c.library.GetApi(product.Code, product.Version, args[1])
return c.processInvoke(ctx, productName, api.Method, api.PathPattern)
}

return c.processInvoke(ctx, productName, args[1], "")
} else if len(args) == 3 {
// restful call
Expand Down
37 changes: 31 additions & 6 deletions openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ 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)
} else {
withApi = false
cli.Printf(a.writer, "\nUsage:\n aliyun %s [GET|PUT|POST|DELETE] <PathPattern> --body \"...\" \n", product.Code)
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, "\nProduct: %s (%s)\n", product.Code, product.Name[i18n.GetLanguage()])
Expand All @@ -75,8 +75,23 @@ func (a *Library) PrintProductUsage(productCode string, withApi bool) error {

if withApi {
cli.PrintfWithColor(a.writer, cli.ColorOff,"\nAvailable Api List: \n")
maxNameLen := 0

for _, apiName := range product.ApiNames {
cli.PrintfWithColor(a.writer, cli.Green," %s\n", apiName)
if len(apiName) > maxNameLen {
maxNameLen = len(apiName)
}
}

for _, apiName := range product.ApiNames {
if product.ApiStyle == "restful" {
api,_ := a.GetApi(productCode, product.Version, apiName)
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)
}

}
// TODO some ApiName is too long, two column not seems good
//w := tabwriter.NewWriter(cli.GetOutputWriter(), 8, 0, 1, ' ', 0)
Expand Down Expand Up @@ -105,9 +120,19 @@ func (a *Library) PrintApiUsage(productCode string, apiName string) error {
return &InvalidApiError{Name: apiName, product: &product}
}

cli.Printf(a.writer, "\nProduct: %s (%s)\n", product.Code, product.Name[i18n.GetLanguage()])
// cli.Printf("Api: %s %s\n", api.Name, api.Description[i18n.GetLanguage()])
cli.Printf(a.writer, "Link: %s\n", api.GetDocumentLink())

if product.ApiStyle == "restful" {
cli.Printf(a.writer, "\nProduct: %s (%s)\n", product.Code, product.Name[i18n.GetLanguage()])
// cli.Printf("Api: %s %s\n", api.Name, api.Description[i18n.GetLanguage()])
cli.Printf(a.writer, "Link: %s\n", api.GetDocumentLink())
cli.Printf(a.writer, "Method: %s\n", api.Method)
cli.Printf(a.writer, "PathPattern: %s\n", api.PathPattern)
} else {
cli.Printf(a.writer, "\nProduct: %s (%s)\n", product.Code, product.Name[i18n.GetLanguage()])
// cli.Printf("Api: %s %s\n", api.Name, api.Description[i18n.GetLanguage()])
cli.Printf(a.writer, "Link: %s\n", api.GetDocumentLink())
}

cli.Printf(a.writer, "\nParameters:\n")

w := tabwriter.NewWriter(a.writer, 8, 0, 1, ' ', 0)
Expand Down

0 comments on commit e519173

Please sign in to comment.