|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"encoding/json" |
| 5 | +"math" |
| 6 | +"time" |
| 7 | + |
| 8 | +"golang.org/x/xerrors" |
| 9 | + |
| 10 | +"github.com/coder/coder/v2/codersdk" |
| 11 | +"github.com/coder/serpent" |
| 12 | +) |
| 13 | + |
| 14 | +func (r*RootCmd)aibridge()*serpent.Command { |
| 15 | +cmd:=&serpent.Command{ |
| 16 | +Use:"aibridge", |
| 17 | +Short:"Manage AIBridge.", |
| 18 | +Handler:func(inv*serpent.Invocation)error { |
| 19 | +returninv.Command.HelpHandler(inv) |
| 20 | +}, |
| 21 | +Children: []*serpent.Command{ |
| 22 | +r.listInterceptions(), |
| 23 | +}, |
| 24 | +} |
| 25 | +returncmd |
| 26 | +} |
| 27 | + |
| 28 | +func (r*RootCmd)listInterceptions()*serpent.Command { |
| 29 | +var ( |
| 30 | +initiatorstring |
| 31 | +startedBeforeRawstring |
| 32 | +startedAfterRawstring |
| 33 | +providerstring |
| 34 | +modelstring |
| 35 | +limitint64 |
| 36 | +offsetint64 |
| 37 | +) |
| 38 | + |
| 39 | +return&serpent.Command{ |
| 40 | +Use:"list-interceptions", |
| 41 | +Short:"List AIBridge interceptions as JSON.", |
| 42 | +Options: serpent.OptionSet{ |
| 43 | +{ |
| 44 | +Flag:"initiator", |
| 45 | +Description:`Only return interceptions initiated by this user. Accepts a user ID, username, or "me".`, |
| 46 | +Default:"", |
| 47 | +Value:serpent.StringOf(&initiator), |
| 48 | +}, |
| 49 | +{ |
| 50 | +Flag:"started-before", |
| 51 | +Description:`Only return interceptions started before this time. Accepts a time in the format "2021-01-01T00:00:00Z".`, |
| 52 | +Default:"", |
| 53 | +Value:serpent.StringOf(&startedBeforeRaw), |
| 54 | +}, |
| 55 | +{ |
| 56 | +Flag:"started-after", |
| 57 | +Description:`Only return interceptions started after this time. Accepts a time in the format "2021-01-01T00:00:00Z".`, |
| 58 | +Default:"", |
| 59 | +Value:serpent.StringOf(&startedAfterRaw), |
| 60 | +}, |
| 61 | +{ |
| 62 | +Flag:"provider", |
| 63 | +Description:`Only return interceptions from this provider.`, |
| 64 | +Default:"", |
| 65 | +Value:serpent.StringOf(&provider), |
| 66 | +}, |
| 67 | +{ |
| 68 | +Flag:"model", |
| 69 | +Description:`Only return interceptions from this model.`, |
| 70 | +Default:"", |
| 71 | +Value:serpent.StringOf(&model), |
| 72 | +}, |
| 73 | +{ |
| 74 | +Flag:"limit", |
| 75 | +Description:`The limit of results to return.`, |
| 76 | +Default:"100", |
| 77 | +Value:serpent.Int64Of(&limit), |
| 78 | +}, |
| 79 | +{ |
| 80 | +Flag:"offset", |
| 81 | +Description:`The offset of results to return.`, |
| 82 | +Default:"0", |
| 83 | +Value:serpent.Int64Of(&offset), |
| 84 | +}, |
| 85 | +}, |
| 86 | +Handler:func(inv*serpent.Invocation)error { |
| 87 | +client,err:=r.InitClient(inv) |
| 88 | +iferr!=nil { |
| 89 | +returnerr |
| 90 | +} |
| 91 | + |
| 92 | +startedBefore:= time.Time{} |
| 93 | +ifstartedBeforeRaw!="" { |
| 94 | +startedBefore,err=time.Parse(time.RFC3339,startedBeforeRaw) |
| 95 | +iferr!=nil { |
| 96 | +returnxerrors.Errorf("parse started before filter value %q: %w",startedBeforeRaw,err) |
| 97 | +} |
| 98 | +} |
| 99 | + |
| 100 | +startedAfter:= time.Time{} |
| 101 | +ifstartedAfterRaw!="" { |
| 102 | +startedAfter,err=time.Parse(time.RFC3339,startedAfterRaw) |
| 103 | +iferr!=nil { |
| 104 | +returnxerrors.Errorf("parse started after filter value %q: %w",startedAfterRaw,err) |
| 105 | +} |
| 106 | +} |
| 107 | + |
| 108 | +iflimit>math.MaxInt32 { |
| 109 | +returnxerrors.Errorf("limit value to high") |
| 110 | +} |
| 111 | +iflimit<0 { |
| 112 | +returnxerrors.Errorf("limit value negative") |
| 113 | +} |
| 114 | + |
| 115 | +exCli:=codersdk.NewExperimentalClient(client) |
| 116 | +resp,err:=exCli.AIBridgeListInterceptions(inv.Context(), codersdk.AIBridgeListInterceptionsFilter{ |
| 117 | +Pagination: codersdk.Pagination{ |
| 118 | +// #nosec G115 - Safe conversion for pagination limit which is expected to be within int32 range |
| 119 | +Limit:int(limit), |
| 120 | +// #nosec G115 - Safe conversion for pagination offset which is expected to be within int32 range |
| 121 | +Offset:int(offset), |
| 122 | +}, |
| 123 | +Initiator:initiator, |
| 124 | +StartedBefore:startedBefore, |
| 125 | +StartedAfter:startedAfter, |
| 126 | +Provider:provider, |
| 127 | +Model:model, |
| 128 | +}) |
| 129 | +iferr!=nil { |
| 130 | +returnxerrors.Errorf("list interceptions: %w",err) |
| 131 | +} |
| 132 | + |
| 133 | +// We currently only support JSON output, so we don't use a |
| 134 | +// formatter. |
| 135 | +enc:=json.NewEncoder(inv.Stdout) |
| 136 | +enc.SetIndent(""," ") |
| 137 | +err=enc.Encode(resp.Results) |
| 138 | +iferr!=nil { |
| 139 | +returnerr |
| 140 | +} |
| 141 | + |
| 142 | +returnerr |
| 143 | +}, |
| 144 | +} |
| 145 | +} |