7
7
"fmt"
8
8
"os"
9
9
"path"
10
+ "runtime"
10
11
"sync"
11
12
"time"
12
13
@@ -151,6 +152,14 @@ func (i invocation) GetArgs() []any {
151
152
return v .([]any )
152
153
}
153
154
155
+ func (i invocation ) GetOS () string {
156
+ v , ok := i .metadata ["os" ]
157
+ if ! ok {
158
+ return ""
159
+ }
160
+ return v .(string )
161
+ }
162
+
154
163
var store = sync .OnceValue (func () analyticsStore {
155
164
db , err := newDiskStore ()
156
165
if err != nil {
@@ -191,14 +200,15 @@ type analyticsStore struct {
191
200
db Execer
192
201
}
193
202
194
- func (s analyticsStore ) NewInvocation (ctx context.Context , uuid uuid.UUID , version string , meta map [string ]any ) error {
203
+ func (s analyticsStore ) NewInvocation (_ context.Context , uuid uuid.UUID , version string , meta map [string ]any ) error {
195
204
if s .db == nil {
196
205
return ErrDBNotInitialized
197
206
}
198
207
199
208
meta ["user_id" ] = getEmail ()
200
209
meta ["version" ] = version
201
210
meta ["start_time" ] = time .Now ()
211
+ meta ["os" ] = getOS ()
202
212
203
213
b , err := json .Marshal (meta )
204
214
if err != nil {
@@ -213,7 +223,7 @@ func (s analyticsStore) NewInvocation(ctx context.Context, uuid uuid.UUID, versi
213
223
return nil
214
224
}
215
225
216
- func (s analyticsStore ) AddMetadata (ctx context.Context , uuid uuid.UUID , meta map [string ]any ) error {
226
+ func (s analyticsStore ) AddMetadata (_ context.Context , uuid uuid.UUID , meta map [string ]any ) error {
217
227
if s .db == nil {
218
228
return ErrDBNotInitialized
219
229
}
@@ -231,7 +241,7 @@ func (s analyticsStore) AddMetadata(ctx context.Context, uuid uuid.UUID, meta ma
231
241
return nil
232
242
}
233
243
234
- func (s analyticsStore ) DeleteInvocation (ctx context.Context , uuid string ) error {
244
+ func (s analyticsStore ) DeleteInvocation (_ context.Context , uuid string ) error {
235
245
if s .db == nil {
236
246
return ErrDBNotInitialized
237
247
}
@@ -244,7 +254,7 @@ func (s analyticsStore) DeleteInvocation(ctx context.Context, uuid string) error
244
254
return nil
245
255
}
246
256
247
- func (s analyticsStore ) ListCompleted (ctx context.Context ) ([]invocation , error ) {
257
+ func (s analyticsStore ) ListCompleted (_ context.Context ) ([]invocation , error ) {
248
258
if s .db == nil {
249
259
return nil , nil
250
260
}
@@ -304,6 +314,24 @@ func emailfunc() string {
304
314
305
315
var getEmail = sync.OnceValue [string ](emailfunc )
306
316
317
+ func osFunc () string {
318
+ userOS := runtime .GOOS
319
+ switch userOS {
320
+ case "windows" :
321
+ return "Windows"
322
+ case "darwin" :
323
+ return "macOS"
324
+ case "linux" :
325
+ return "Linux"
326
+ default :
327
+ // weird case, but catching it instead of throwing an error
328
+ return userOS
329
+ }
330
+ }
331
+
332
+ // Dont invoke this function directly. Use the `getOS` function instead.
333
+ var getOS = sync.OnceValue [string ](osFunc )
334
+
307
335
func NewInvocation (ctx context.Context , version string , meta map [string ]any ) (context.Context , error ) {
308
336
// v7 for sortable property (not vital as we also store timestamps, but no harm to have)
309
337
u , _ := uuid .NewV7 ()
0 commit comments