func()

in openapi/commando.go [111:184]


func (c *Commando) main(ctx *cli.Context, args []string) error {
	// aliyun
	if len(args) == 0 {
		c.printUsage(ctx)
		return nil
	}

	// detect if in configure mode
	ctx.SetInConfigureMode(DetectInConfigureMode(ctx.Flags()))

	// update current `Profile` with flags
	var err error
	c.profile, err = config.LoadProfileWithContext(ctx)
	if err != nil {
		return cli.NewErrorWithTip(err, "Configuration failed, use `aliyun configure` to configure it")
	}
	err = c.profile.Validate()
	if err != nil {
		return cli.NewErrorWithTip(err, "Configuration failed, use `aliyun configure` to configure it.")
	}
	i18n.SetLanguage(c.profile.Language)

	// process following commands:
	//   aliyun <productCode>
	//   aliyun <productCode> <method> --param1 value1
	//   aliyun <productCode> GET <path>
	productName := args[0]
	if len(args) == 1 {
		// aliyun <productCode>
		// TODO: aliyun pluginName ...
		return c.library.PrintProductUsage(productName, true)
	} else if len(args) == 2 {
		// rpc or restful call
		// aliyun <productCode> <method> --param1 value1
		product, _ := c.library.GetProduct(args[0])
		if product.Code != "" {
			if version, _ := ctx.Flags().Get("version").GetValue(); version != "" {
				if style, ok := c.library.GetStyle(productName, version); ok {
					product.ApiStyle = style
				} else {
					return cli.NewErrorWithTip(fmt.Errorf("unchecked version %s", version),
						"Please contact the customer support to get more info about API version")
				}
			}
		}
		if product.ApiStyle == "restful" {
			api, _ := c.library.GetApi(product.Code, product.Version, args[1])
			c.CheckApiParamWithBuildInArgs(ctx, api)
			ctx.Command().Name = args[1]
			return c.processInvoke(ctx, productName, api.Method, api.PathPattern)
		} else {
			// RPC need check API parameters too
			api, _ := c.library.GetApi(product.Code, product.Version, args[1])
			c.CheckApiParamWithBuildInArgs(ctx, api)
		}

		return c.processInvoke(ctx, productName, args[1], "")
	} else if len(args) == 3 {
		// restful call
		// aliyun <productCode> {GET|PUT|POST|DELETE} <path> --
		product, _ := c.library.GetProduct(productName)
		api, find := c.library.GetApiByPath(product.Code, product.Version, args[1], args[2])
		if !find {
			// throw error, can not find api by path
			return cli.NewErrorWithTip(fmt.Errorf("can not find api by path %s", args[2]),
				"Please confirm if the API path exists")
		}
		c.CheckApiParamWithBuildInArgs(ctx, api)
		return c.processInvoke(ctx, productName, args[1], args[2])
	} else {
		return cli.NewErrorWithTip(fmt.Errorf("too many arguments"),
			"Use `aliyun --help` to show usage")
	}
}