| 82 | } |
| 83 | |
| 84 | func createRun(ctx context.Context, opts *CreateOptions) error { |
| 85 | cfg, err := opts.Config() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | host, _ := cfg.Authentication().DefaultHost() |
| 90 | |
| 91 | c, err := opts.EventClient(shared.ResourceHost(opts.Resource, host)) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | opts.IO.StartProgressIndicator() |
| 97 | sub, created, err := c.CreateSubscription(ctx, opts.Resource, shared.NormalizeEventTypes(opts.Events)) |
| 98 | opts.IO.StopProgressIndicator() |
| 99 | if err != nil { |
| 100 | if errors.Is(err, client.ErrResourceNotFound) { |
| 101 | return fmt.Errorf("could not subscribe to %s; the resource was not found, or you cannot access it: %w", |
| 102 | opts.Resource, err) |
| 103 | } |
| 104 | return shared.SubscriptionLimitError(fmt.Errorf("failed to create subscription: %w", err)) |
| 105 | } |
| 106 | |
| 107 | if opts.Exporter != nil { |
| 108 | return opts.Exporter.Write(opts.IO, sub) |
| 109 | } |
| 110 | |
| 111 | if _, err := fmt.Fprintln(opts.IO.Out, sub.ID); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | if !opts.IO.IsStderrTTY() { |
| 116 | return nil |
| 117 | } |
| 118 | action := "Created subscription" |
| 119 | if !created { |
| 120 | action = "Reused existing subscription" |
| 121 | } |
| 122 | cs := opts.IO.ColorScheme() |
| 123 | _, err = fmt.Fprintf(opts.IO.ErrOut, "%s %s %s, which expires %s\n", |
| 124 | cs.SuccessIcon(), action, cs.Bold(sub.ID), text.FuzzyIn(opts.Now(), sub.ExpiresAt)) |
| 125 | return err |
| 126 | } |