func()

in pkg/cmd/bind.go [179:273]


func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error {
	client, err := o.GetCmdClient()
	if err != nil {
		return err
	}

	source, err := o.decode(args[0], sourceKey)
	if err != nil {
		return err
	}

	sink, err := o.decode(args[1], sinkKey)
	if err != nil {
		return err
	}

	name := o.nameFor(source, sink)

	binding := v1.Pipe{
		ObjectMeta: metav1.ObjectMeta{
			Namespace: o.Namespace,
			Name:      name,
		},
		Spec: v1.PipeSpec{
			Source: source,
			Sink:   sink,
		},
	}

	if o.ErrorHandler != "" {
		if errorHandler, err := o.parseErrorHandler(); err == nil {
			binding.Spec.ErrorHandler = errorHandler
		} else {
			return err
		}
	}

	if len(o.Steps) > 0 {
		binding.Spec.Steps = make([]v1.Endpoint, 0)
		for idx, stepDesc := range o.Steps {
			stepIndex := idx + 1
			stepKey := fmt.Sprintf("%s%d", stepKeyPrefix, stepIndex)
			step, err := o.decode(stepDesc, stepKey)
			if err != nil {
				return err
			}
			binding.Spec.Steps = append(binding.Spec.Steps, step)
		}
	}

	if len(o.Traits) > 0 {
		if binding.Annotations == nil {
			binding.Annotations = make(map[string]string)
		}

		for _, t := range o.Traits {
			kv := strings.SplitN(t, "=", 2)
			if len(kv) != 2 {
				return fmt.Errorf("could not parse trait configuration %s, expected format 'trait.property=value'", t)
			}
			value := maybeBuildArrayNotation(binding.Annotations[v1.TraitAnnotationPrefix+kv[0]], kv[1])
			binding.Annotations[v1.TraitAnnotationPrefix+kv[0]] = value
		}
	}

	if o.ServiceAccount != "" {
		binding.Spec.ServiceAccountName = o.ServiceAccount
	}

	// --operator-id={id} is a syntax sugar for '--annotation camel.apache.org/operator.id={id}'
	binding.SetOperatorID(strings.TrimSpace(o.OperatorID))

	for _, annotation := range o.Annotations {
		parts := strings.SplitN(annotation, "=", 2)
		if len(parts) == 2 {
			binding.Annotations[parts[0]] = parts[1]
		}
	}

	if o.OutputFormat != "" {
		return showPipeOutput(cmd, &binding, o.OutputFormat, client.GetScheme())
	}

	replaced, err := kubernetes.ReplaceResource(o.Context, client, &binding)
	if err != nil {
		return err
	}

	if !replaced {
		fmt.Fprintln(cmd.OutOrStdout(), `binding "`+name+`" created`)
	} else {
		fmt.Fprintln(cmd.OutOrStdout(), `binding "`+name+`" updated`)
	}
	return nil
}