func parseApi()

in commands/api.go [829:940]


func parseApi(cmd *cobra.Command, args []string) (*whisk.Api, *QualifiedName, error) {
	var err error
	var basepath string = "/"
	var apiname string
	var basepathArgIsApiName = false

	api := new(whisk.Api)

	if len(args) > 3 {
		// Is the argument a basepath (must start with /) or an API name
		if _, ok := isValidBasepath(args[0]); !ok {
			whisk.Debug(whisk.DbgInfo, "Treating '%s' as an API name; as it does not begin with '/'\n", args[0])
			basepathArgIsApiName = true
		}
		if err, _ := isBasepathParameterized(args[0]); err != nil {
			return nil, nil, err
		}
		basepath = args[0]

		// Shift the args so the remaining code works with or without the explicit base path arg
		args = args[1:]
	}

	// Is the API path valid?
	if len(args) > 0 {
		if whiskErr, ok := isValidRelpath(args[0]); !ok {
			return nil, nil, whiskErr
		}
		api.GatewayRelPath = args[0] // Maintain case as URLs may be case-sensitive
	}

	// Attempting to use path parameters, lets validate that they provided them correctly.
	hasPathParams, err := hasPathParameters(api.GatewayRelPath)
	if err != nil {
		return nil, nil, err
	}
	// If they provided path Parameters, the response type better be http as its the only one that supports path parameters right now.
	if hasPathParams && Flags.api.resptype != "http" {
		errMsg := wski18n.T("A response type of 'http' is required when using path parameters.")
		whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_GENERAL,
			whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
		return nil, nil, whiskErr
	}

	// Is the API verb valid?
	if len(args) > 1 {
		if whiskErr, ok := IsValidApiVerb(args[1]); !ok {
			return nil, nil, whiskErr
		}
		api.GatewayMethod = strings.ToUpper(args[1])
	}

	// Is the specified action name valid?
	var qName = new(QualifiedName)
	if len(args) == 3 {
		qName, err = NewQualifiedName(args[2])
		if err != nil {
			whisk.Debug(whisk.DbgError, "NewQualifiedName(%s) failed: %s\n", args[2], err)
			errMsg := wski18n.T("'{{.name}}' is not a valid action name: {{.err}}",
				map[string]interface{}{"name": args[2], "err": err})
			whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_GENERAL,
				whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
			return nil, nil, whiskErr
		}
		if qName.GetEntityName() == "" {
			whisk.Debug(whisk.DbgError, "Action name '%s' is invalid\n", args[2])
			errMsg := wski18n.T("'{{.name}}' is not a valid action name.", map[string]interface{}{"name": args[2]})
			whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_GENERAL,
				whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
			return nil, nil, whiskErr
		}
	}

	if len(Flags.api.apiname) > 0 {
		if basepathArgIsApiName {
			// Specifying API name as argument AND as a --apiname option value is invalid
			whisk.Debug(whisk.DbgError, "API is specified as an argument '%s' and as a flag '%s'\n", basepath, Flags.api.apiname)
			errMsg := wski18n.T("An API name can only be specified once.")
			whiskErr := whisk.MakeWskError(errors.New(errMsg), whisk.EXIT_CODE_ERR_GENERAL,
				whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
			return nil, nil, whiskErr
		}
		apiname = Flags.api.apiname
	}

	api.Namespace = Client.Config.Namespace
	api.Action = new(whisk.ApiAction)
	var urlActionPackage string
	if len(qName.GetPackageName()) > 0 {
		urlActionPackage = qName.GetPackageName()
	} else {
		urlActionPackage = "default"
	}
	backendUrl := Client.Config.Host + "/api/v1/web/" + qName.GetNamespace() + "/" + urlActionPackage + "/" + qName.GetEntity() + ".http"
	if !strings.HasPrefix(backendUrl, "http") {
		backendUrl = "https://" + backendUrl
	}
	api.Action.BackendUrl = backendUrl
	api.Action.BackendMethod = api.GatewayMethod
	api.Action.Name = qName.GetEntityName()
	api.Action.Namespace = qName.GetNamespace()
	api.Action.Auth = Client.Config.AuthToken
	api.ApiName = apiname
	api.GatewayBasePath = basepath
	if !basepathArgIsApiName {
		api.Id = "API:" + api.Namespace + ":" + api.GatewayBasePath
	}
	api.PathParameters, err = generatePathParameters(api.GatewayRelPath)

	whisk.Debug(whisk.DbgInfo, "Parsed api struct: %#v\n", api)
	return api, qName, err
}