@@ -96,7 +96,7 @@ It will return a `License` object as well as any validation errors that occur. T
96
96
object can be used to perform additional actions, such as ` license.Activate(fingerprint) ` .
97
97
98
98
``` go
99
- license , err := keygen.Validate (fingerprint)
99
+ license , err := keygen.Validate (context. Background (), fingerprint)
100
100
switch {
101
101
case err == keygen.ErrLicenseNotActivated :
102
102
panic (" license is not activated!" )
@@ -126,8 +126,10 @@ You can read more about generating a personal keypair and about code signing [he
126
126
``` go
127
127
opts := keygen.UpgradeOptions {CurrentVersion: " 1.0.0" , Channel : " stable" , PublicKey : " 5ec69b78d4b5d4b624699cef5faf3347dc4b06bb807ed4a2c6740129f1db7159" }
128
128
129
+ ctx := context.Background ()
130
+
129
131
// Check for an upgrade
130
- release , err := keygen.Upgrade (opts)
132
+ release , err := keygen.Upgrade (ctx, opts)
131
133
switch {
132
134
case err == keygen.ErrUpgradeNotAvailable :
133
135
fmt.Println (" No upgrade available, already at the latest version!" )
@@ -140,7 +142,7 @@ case err != nil:
140
142
}
141
143
142
144
// Install the upgrade
143
- if err := release.Install (); err != nil {
145
+ if err := release.Install (ctx ); err != nil {
144
146
panic (" upgrade install failed!" )
145
147
}
146
148
@@ -167,6 +169,8 @@ is cross-platform, using the operating system's native GUID.
167
169
package main
168
170
169
171
import (
172
+ " context"
173
+
170
174
" github.com/denisbrodbeck/machineid"
171
175
" github.com/keygen-sh/keygen-go/v3"
172
176
)
@@ -181,12 +185,14 @@ func main() {
181
185
panic (err)
182
186
}
183
187
188
+ ctx := context.Background ()
189
+
184
190
// Validate the license for the current fingerprint
185
- license , err := keygen.Validate (fingerprint)
191
+ license , err := keygen.Validate (ctx, fingerprint)
186
192
switch {
187
193
case err == keygen.ErrLicenseNotActivated :
188
194
// Activate the current fingerprint
189
- machine , err := license.Activate (fingerprint)
195
+ machine , err := license.Activate (ctx, fingerprint)
190
196
switch {
191
197
case err == keygen.ErrMachineLimitExceeded :
192
198
panic (" machine limit has been exceeded!" )
@@ -210,7 +216,11 @@ Check for an upgrade and automatically replace the current binary with the newes
210
216
``` go
211
217
package main
212
218
213
- import " github.com/keygen-sh/keygen-go/v3"
219
+ import (
220
+ " context"
221
+
222
+ " github.com/keygen-sh/keygen-go/v3"
223
+ )
214
224
215
225
// The current version of the program
216
226
const CurrentVersion = " 1.0.0"
@@ -224,10 +234,12 @@ func main() {
224
234
fmt.Printf (" Current version: %s \n " , CurrentVersion)
225
235
fmt.Println (" Checking for upgrades..." )
226
236
237
+ ctx := context.Background ()
238
+
227
239
opts := keygen.UpgradeOptions {CurrentVersion: CurrentVersion, Channel: " stable" , PublicKey: " YOUR_COMPANY_PUBLIC_KEY" }
228
240
229
241
// Check for upgrade
230
- release , err := keygen.Upgrade (opts)
242
+ release , err := keygen.Upgrade (ctx, opts)
231
243
switch {
232
244
case err == keygen.ErrUpgradeNotAvailable :
233
245
fmt.Println (" No upgrade available, already at the latest version!" )
@@ -243,7 +255,7 @@ func main() {
243
255
fmt.Println (" Installing upgrade..." )
244
256
245
257
// Download the upgrade and install it
246
- err = release.Install ()
258
+ err = release.Install (ctx )
247
259
if err != nil {
248
260
panic (" upgrade install failed!" )
249
261
}
@@ -263,6 +275,8 @@ nodes in cloud-based scenarios, since nodes may share underlying hardware.
263
275
package main
264
276
265
277
import (
278
+ " context"
279
+
266
280
" github.com/google/uuid"
267
281
" github.com/keygen-sh/keygen-go/v3"
268
282
)
@@ -275,15 +289,17 @@ func main() {
275
289
// The current device's fingerprint (could be e.g. MAC, mobo ID, GUID, etc.)
276
290
fingerprint := uuid.New ().String ()
277
291
292
+ ctx := context.Background ()
293
+
278
294
// Keep our example process alive
279
295
done := make (chan bool , 1 )
280
296
281
297
// Validate the license for the current fingerprint
282
- license , err := keygen.Validate (fingerprint)
298
+ license , err := keygen.Validate (ctx, fingerprint)
283
299
switch {
284
300
case err == keygen.ErrLicenseNotActivated :
285
301
// Activate the current fingerprint
286
- machine , err := license.Activate (fingerprint)
302
+ machine , err := license.Activate (ctx, fingerprint)
287
303
if err != nil {
288
304
fmt.Println (" machine activation failed!" )
289
305
@@ -299,7 +315,7 @@ func main() {
299
315
for sig := range sigs {
300
316
fmt.Printf (" Caught %v , deactivating machine and gracefully exiting...\n " , sig)
301
317
302
- if err := machine.Deactivate (); err != nil {
318
+ if err := machine.Deactivate (ctx ); err != nil {
303
319
panic (err)
304
320
}
305
321
@@ -311,7 +327,7 @@ func main() {
311
327
}()
312
328
313
329
// Start a heartbeat monitor for the current machine
314
- if err := machine.Monitor (); err != nil {
330
+ if err := machine.Monitor (ctx ); err != nil {
315
331
fmt.Println (" Machine heartbeat monitor failed to start!" )
316
332
317
333
panic (err)
@@ -463,17 +479,21 @@ key does not exist. You can handle this accordingly.
463
479
``` go
464
480
package main
465
481
466
- import " github.com/keygen-sh/keygen-go/v3"
482
+ import (
483
+ " context"
484
+
485
+ " github.com/keygen-sh/keygen-go/v3"
486
+ )
467
487
468
- func getLicense () (*keygen .License , error ) {
488
+ func getLicense (ctx context . Context ) (*keygen .License , error ) {
469
489
keygen.LicenseKey = promptForLicenseKey ()
470
490
471
- license , err := keygen.Validate ()
491
+ license , err := keygen.Validate (ctx )
472
492
if err != nil {
473
493
if _ , ok := err.(*keygen.LicenseKeyError ); ok {
474
494
fmt.Println (" License key does not exist!" )
475
495
476
- return getLicense ()
496
+ return getLicense (ctx )
477
497
}
478
498
479
499
return nil , err
@@ -486,7 +506,9 @@ func main() {
486
506
keygen.Account = " ..."
487
507
keygen.Product = " ..."
488
508
489
- license , err := getLicense ()
509
+ ctx := context.Background ()
510
+
511
+ license , err := getLicense (ctx)
490
512
if err != nil {
491
513
panic (err)
492
514
}
@@ -505,15 +527,15 @@ package main
505
527
506
528
import " github.com/keygen-sh/keygen-go/v3"
507
529
508
- func getLicense () (*keygen .License , error ) {
530
+ func getLicense (ctx context . Context ) (*keygen .License , error ) {
509
531
keygen.Token = promptForLicenseToken ()
510
532
511
- license , err := keygen.Validate ()
533
+ license , err := keygen.Validate (ctx )
512
534
if err != nil {
513
535
if _ , ok := err.(*keygen.LicenseTokenError ); ok {
514
536
fmt.Println (" License token does not exist!" )
515
537
516
- return getLicense ()
538
+ return getLicense (ctx )
517
539
}
518
540
519
541
return nil , err
@@ -526,7 +548,9 @@ func main() {
526
548
keygen.Account = " ..."
527
549
keygen.Product = " ..."
528
550
529
- license , err := getLicense ()
551
+ ctx := context.Background ()
552
+
553
+ license , err := getLicense (ctx)
530
554
if err != nil {
531
555
panic (err)
532
556
}
@@ -546,15 +570,15 @@ package main
546
570
547
571
import " github.com/keygen-sh/keygen-go/v3"
548
572
549
- func validate () (*keygen .License , error ) {
550
- license , err := keygen.Validate ()
573
+ func validate (ctx context . Context ) (*keygen .License , error ) {
574
+ license , err := keygen.Validate (ctx )
551
575
if err != nil {
552
576
if e , ok := err.(*keygen.RateLimitError ); ok {
553
577
// Sleep until our rate limit window is passed
554
578
time.Sleep (time.Duration (e.RetryAfter ) * time.Second )
555
579
556
580
// Retry validate
557
- return validate ()
581
+ return validate (ctx )
558
582
}
559
583
560
584
return nil , err
@@ -568,7 +592,9 @@ func main() {
568
592
keygen.Product = " YOUR_KEYGEN_PRODUCT_ID"
569
593
keygen.LicenseKey = " A_KEYGEN_LICENSE_KEY"
570
594
571
- license , err := validate ()
595
+ ctx := context.Background ()
596
+
597
+ license , err := validate (ctx)
572
598
if err != nil {
573
599
panic (err)
574
600
}
@@ -589,6 +615,8 @@ to implement automatic retries.
589
615
package main
590
616
591
617
import (
618
+ " context"
619
+
592
620
" github.com/hashicorp/go-retryablehttp"
593
621
" github.com/keygen-sh/keygen-go/v3"
594
622
)
@@ -605,8 +633,10 @@ func main() {
605
633
keygen.Product = " YOUR_KEYGEN_PRODUCT_ID"
606
634
keygen.LicenseKey = " A_KEYGEN_LICENSE_KEY"
607
635
636
+ ctx := context.Background ()
637
+
608
638
// Use SDK as you would normally
609
- keygen.Validate ()
639
+ keygen.Validate (ctx )
610
640
}
611
641
```
612
642
@@ -623,6 +653,7 @@ To do so in Go, you can utilize [`gock`](https://github.com/h2non/gock) or [`htt
623
653
package main
624
654
625
655
import (
656
+ " context"
626
657
" testing"
627
658
628
659
" github.com/keygen-sh/keygen-go/v3"
@@ -637,6 +668,7 @@ func init() {
637
668
}
638
669
639
670
func TestExample (t *testing .T ) {
671
+ ctx := context.Background ()
640
672
defer gock.Off ()
641
673
642
674
// Intercept Keygen's HTTP client
@@ -664,7 +696,7 @@ func TestExample(t *testing.T) {
664
696
keygen.MaxClockDrift = -1
665
697
666
698
// Use SDK as you would normally
667
- _ , err := keygen.Validate ()
699
+ _ , err := keygen.Validate (ctx )
668
700
if err != nil {
669
701
t.Fatalf (" Should not fail mock validation: err=%v " , err)
670
702
}
0 commit comments