|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "golang.org/x/xerrors" |
| 10 | + |
| 11 | + "github.com/coder/coder/v2/codersdk" |
| 12 | + "github.com/coder/serpent" |
| 13 | +) |
| 14 | + |
| 15 | +const maxInterceptionsLimit = 1000 |
| 16 | + |
| 17 | +func (r *RootCmd) aibridge() *serpent.Command { |
| 18 | + cmd := &serpent.Command{ |
| 19 | + Use: "aibridge", |
| 20 | + Short: "Manage AIBridge.", |
| 21 | + Handler: func(inv *serpent.Invocation) error { |
| 22 | + return inv.Command.HelpHandler(inv) |
| 23 | + }, |
| 24 | + Children: []*serpent.Command{ |
| 25 | + r.aibridgeInterceptions(), |
| 26 | + }, |
| 27 | + } |
| 28 | + return cmd |
| 29 | +} |
| 30 | + |
| 31 | +func (r *RootCmd) aibridgeInterceptions() *serpent.Command { |
| 32 | + cmd := &serpent.Command{ |
| 33 | + Use: "interceptions", |
| 34 | + Short: "Manage AIBridge interceptions.", |
| 35 | + Handler: func(inv *serpent.Invocation) error { |
| 36 | + return inv.Command.HelpHandler(inv) |
| 37 | + }, |
| 38 | + Children: []*serpent.Command{ |
| 39 | + r.aibridgeInterceptionsList(), |
| 40 | + }, |
| 41 | + } |
| 42 | + return cmd |
| 43 | +} |
| 44 | + |
| 45 | +func (r *RootCmd) aibridgeInterceptionsList() *serpent.Command { |
| 46 | + var ( |
| 47 | + initiator string |
| 48 | + startedBeforeRaw string |
| 49 | + startedAfterRaw string |
| 50 | + provider string |
| 51 | + model string |
| 52 | + afterIDRaw string |
| 53 | + limit int64 |
| 54 | + ) |
| 55 | + |
| 56 | + return &serpent.Command{ |
| 57 | + Use: "list", |
| 58 | + Short: "List AIBridge interceptions as JSON.", |
| 59 | + Options: serpent.OptionSet{ |
| 60 | + { |
| 61 | + Flag: "initiator", |
| 62 | + Description: `Only return interceptions initiated by this user. Accepts a user ID, username, or "me".`, |
| 63 | + Default: "", |
| 64 | + Value: serpent.StringOf(&initiator), |
| 65 | + }, |
| 66 | + { |
| 67 | + Flag: "started-before", |
| 68 | + Description: fmt.Sprintf("Only return interceptions started before this time. Must be after 'started-after' if set. Accepts a time in the RFC 3339 format, e.g. %q.", time.RFC3339), |
| 69 | + Default: "", |
| 70 | + Value: serpent.StringOf(&startedBeforeRaw), |
| 71 | + }, |
| 72 | + { |
| 73 | + Flag: "started-after", |
| 74 | + Description: fmt.Sprintf("Only return interceptions started after this time. Must be before 'started-before' if set. Accepts a time in the RFC 3339 format, e.g. %q.", time.RFC3339), |
| 75 | + Default: "", |
| 76 | + Value: serpent.StringOf(&startedAfterRaw), |
| 77 | + }, |
| 78 | + { |
| 79 | + Flag: "provider", |
| 80 | + Description: `Only return interceptions from this provider.`, |
| 81 | + Default: "", |
| 82 | + Value: serpent.StringOf(&provider), |
| 83 | + }, |
| 84 | + { |
| 85 | + Flag: "model", |
| 86 | + Description: `Only return interceptions from this model.`, |
| 87 | + Default: "", |
| 88 | + Value: serpent.StringOf(&model), |
| 89 | + }, |
| 90 | + { |
| 91 | + Flag: "after-id", |
| 92 | + Description: "The ID of the last result on the previous page to use as a pagination cursor.", |
| 93 | + Default: "", |
| 94 | + Value: serpent.StringOf(&afterIDRaw), |
| 95 | + }, |
| 96 | + { |
| 97 | + Flag: "limit", |
| 98 | + Description: fmt.Sprintf(`The limit of results to return. Must be between 1 and %d.`, maxInterceptionsLimit), |
| 99 | + Default: "100", |
| 100 | + Value: serpent.Int64Of(&limit), |
| 101 | + }, |
| 102 | + }, |
| 103 | + Handler: func(inv *serpent.Invocation) error { |
| 104 | + client, err := r.InitClient(inv) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + startedBefore := time.Time{} |
| 110 | + if startedBeforeRaw != "" { |
| 111 | + startedBefore, err = time.Parse(time.RFC3339, startedBeforeRaw) |
| 112 | + if err != nil { |
| 113 | + return xerrors.Errorf("parse started before filter value %q: %w", startedBeforeRaw, err) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + startedAfter := time.Time{} |
| 118 | + if startedAfterRaw != "" { |
| 119 | + startedAfter, err = time.Parse(time.RFC3339, startedAfterRaw) |
| 120 | + if err != nil { |
| 121 | + return xerrors.Errorf("parse started after filter value %q: %w", startedAfterRaw, err) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + afterID := uuid.Nil |
| 126 | + if afterIDRaw != "" { |
| 127 | + afterID, err = uuid.Parse(afterIDRaw) |
| 128 | + if err != nil { |
| 129 | + return xerrors.Errorf("parse after_id filter value %q: %w", afterIDRaw, err) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if limit < 1 || limit > maxInterceptionsLimit { |
| 134 | + return xerrors.Errorf("limit value must be between 1 and %d", maxInterceptionsLimit) |
| 135 | + } |
| 136 | + |
| 137 | + expCli := codersdk.NewExperimentalClient(client) |
| 138 | + resp, err := expCli.AIBridgeListInterceptions(inv.Context(), codersdk.AIBridgeListInterceptionsFilter{ |
| 139 | + Pagination: codersdk.Pagination{ |
| 140 | + AfterID: afterID, |
| 141 | + // #nosec G115 - Checked above. |
| 142 | + Limit: int(limit), |
| 143 | + }, |
| 144 | + Initiator: initiator, |
| 145 | + StartedBefore: startedBefore, |
| 146 | + StartedAfter: startedAfter, |
| 147 | + Provider: provider, |
| 148 | + Model: model, |
| 149 | + }) |
| 150 | + if err != nil { |
| 151 | + return xerrors.Errorf("list interceptions: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + // We currently only support JSON output, so we don't use a |
| 155 | + // formatter. |
| 156 | + enc := json.NewEncoder(inv.Stdout) |
| 157 | + enc.SetIndent("", " ") |
| 158 | + err = enc.Encode(resp.Results) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return err |
| 164 | + }, |
| 165 | + } |
| 166 | +} |
0 commit comments