func()

in commands/trigger.go [432:520]


func (t *Trigger) Create(Client *whisk.Client, args []string) error {
	if whiskErr := CheckArgs(args, 1, 1, "Trigger create",
		wski18n.T("A trigger name is required.")); whiskErr != nil {
		return whiskErr
	}

	//1. if the command line arguments user provides contains only --param flags
	//2. if the command line arguments user provides contains no --param flags at all
	//we should process the trigger create command in the old way.
	if userIndicatesToUseOldTriggerCommand() {
		triggerName, err := NewQualifiedName(args[0])
		if err != nil {
			return NewQualifiedNameError(args[0], err)
		}

		//if user also issued new trigger command then we stop execution
		if triggerUsageErr := userIssuedNewTriggerCmd(); triggerUsageErr != nil {
			return triggerUsageErr
		}

		annotationArray := Flags.common.annotation
		authToken := Client.Config.AuthToken

		// if a feed is specified, create additional parameters which must be passed to the feed
		feedQualifiedName, additionalFeedParams := feedParameters(Flags.common.feed, FEED_CREATE, triggerName, authToken)

		// if a feed is specified, add feed annotation the annotations declared on the command line
		// TODO: add test to ensure that generated annotation has precedence
		if feedQualifiedName != nil {
			annotationArray = append(annotationArray, getFormattedJSON("feed", feedQualifiedName.GetFullQualifiedName()))
		}
		annotations := getParameters(annotationArray, true, true)

		// the feed receives all the parameters that are specified on the command line so we merge
		// the feed lifecycle parameters with the command line ones
		parameters := getParameters(append(Flags.common.param, additionalFeedParams...), feedQualifiedName == nil, false)

		trigger := &whisk.Trigger{
			Name:        triggerName.GetEntityName(),
			Annotations: annotations.(whisk.KeyValueArr),
		}

		if feedQualifiedName == nil {
			// parameters are only attached to the trigger in there is no feed, otherwise
			// parameters are passed to the feed instead
			trigger.Parameters = parameters.(whisk.KeyValueArr)
		}

		createOrUpdate(Client, triggerName, trigger, false)

		// Invoke the specified feed action to configure the trigger feed
		if feedQualifiedName != nil {
			res, err := invokeAction(*feedQualifiedName, parameters, true, false)
			if err != nil {
				whisk.Debug(whisk.DbgError, "Failed configuring feed '%s' failed: %s\n", feedQualifiedName.GetFullQualifiedName(), err)

				// TODO: should we do this at all? Keeping for now.
				printFailedBlockingInvocationResponse(*feedQualifiedName, false, res, err)

				reason := wski18n.T(FEED_CONFIGURATION_FAILURE, map[string]interface{}{"feedname": feedQualifiedName.GetFullQualifiedName(), "err": err})
				errStr := wski18n.T("Unable to create trigger '{{.name}}': {{.err}}",
					map[string]interface{}{"name": trigger.Name, "err": reason})
				werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)

				// Delete trigger that was created for this feed
				err = deleteTrigger(triggerName.GetEntityName())
				if err != nil {
					whisk.Debug(whisk.DbgWarn, "Ignoring deleteTrigger(%s) failure: %s\n", triggerName.GetEntityName(), err)
				}

				return werr
			} else {
				whisk.Debug(whisk.DbgInfo, "Successfully configured trigger feed via feed action '%s'\n", Flags.common.feed)

				// preserve existing behavior where output of feed activation is emitted to console
				printInvocationMsg(*feedQualifiedName, true, true, res, color.Output)
			}
		}

		fmt.Fprintf(color.Output,
			wski18n.T("{{.ok}} created trigger {{.name}}\n",
				map[string]interface{}{"ok": color.GreenString("ok:"), "name": boldString(trigger.Name)}))
		return nil
	}
	//1. if user's input command line argument contains either --feed-param or --trigger-param
	//2. if user's input command line argument contains both --feed-param and --trigger-param
	//then we process trigger create command in a different way
	return CreateExtendedVersion(Client, args)
}