Skip to content

Commit d437314

Browse files
mathnogueiraezekg
authored andcommitted
chore(v3): update examples in README to add the context.Context in all calls
1 parent 3b20406 commit d437314

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

README.md

+59-27
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ It will return a `License` object as well as any validation errors that occur. T
9696
object can be used to perform additional actions, such as `license.Activate(fingerprint)`.
9797

9898
```go
99-
license, err := keygen.Validate(fingerprint)
99+
license, err := keygen.Validate(context.Background(), fingerprint)
100100
switch {
101101
case err == keygen.ErrLicenseNotActivated:
102102
panic("license is not activated!")
@@ -126,8 +126,10 @@ You can read more about generating a personal keypair and about code signing [he
126126
```go
127127
opts := keygen.UpgradeOptions{CurrentVersion: "1.0.0", Channel: "stable", PublicKey: "5ec69b78d4b5d4b624699cef5faf3347dc4b06bb807ed4a2c6740129f1db7159"}
128128

129+
ctx := context.Background()
130+
129131
// Check for an upgrade
130-
release, err := keygen.Upgrade(opts)
132+
release, err := keygen.Upgrade(ctx, opts)
131133
switch {
132134
case err == keygen.ErrUpgradeNotAvailable:
133135
fmt.Println("No upgrade available, already at the latest version!")
@@ -140,7 +142,7 @@ case err != nil:
140142
}
141143

142144
// Install the upgrade
143-
if err := release.Install(); err != nil {
145+
if err := release.Install(ctx); err != nil {
144146
panic("upgrade install failed!")
145147
}
146148

@@ -167,6 +169,8 @@ is cross-platform, using the operating system's native GUID.
167169
package main
168170

169171
import (
172+
"context"
173+
170174
"github.com/denisbrodbeck/machineid"
171175
"github.com/keygen-sh/keygen-go/v3"
172176
)
@@ -181,12 +185,14 @@ func main() {
181185
panic(err)
182186
}
183187

188+
ctx := context.Background()
189+
184190
// Validate the license for the current fingerprint
185-
license, err := keygen.Validate(fingerprint)
191+
license, err := keygen.Validate(ctx, fingerprint)
186192
switch {
187193
case err == keygen.ErrLicenseNotActivated:
188194
// Activate the current fingerprint
189-
machine, err := license.Activate(fingerprint)
195+
machine, err := license.Activate(ctx, fingerprint)
190196
switch {
191197
case err == keygen.ErrMachineLimitExceeded:
192198
panic("machine limit has been exceeded!")
@@ -210,7 +216,11 @@ Check for an upgrade and automatically replace the current binary with the newes
210216
```go
211217
package main
212218

213-
import "github.com/keygen-sh/keygen-go/v3"
219+
import (
220+
"context"
221+
222+
"github.com/keygen-sh/keygen-go/v3"
223+
)
214224

215225
// The current version of the program
216226
const CurrentVersion = "1.0.0"
@@ -224,10 +234,12 @@ func main() {
224234
fmt.Printf("Current version: %s\n", CurrentVersion)
225235
fmt.Println("Checking for upgrades...")
226236

237+
ctx := context.Background()
238+
227239
opts := keygen.UpgradeOptions{CurrentVersion: CurrentVersion, Channel: "stable", PublicKey: "YOUR_COMPANY_PUBLIC_KEY"}
228240

229241
// Check for upgrade
230-
release, err := keygen.Upgrade(opts)
242+
release, err := keygen.Upgrade(ctx, opts)
231243
switch {
232244
case err == keygen.ErrUpgradeNotAvailable:
233245
fmt.Println("No upgrade available, already at the latest version!")
@@ -243,7 +255,7 @@ func main() {
243255
fmt.Println("Installing upgrade...")
244256

245257
// Download the upgrade and install it
246-
err = release.Install()
258+
err = release.Install(ctx)
247259
if err != nil {
248260
panic("upgrade install failed!")
249261
}
@@ -263,6 +275,8 @@ nodes in cloud-based scenarios, since nodes may share underlying hardware.
263275
package main
264276

265277
import (
278+
"context"
279+
266280
"github.com/google/uuid"
267281
"github.com/keygen-sh/keygen-go/v3"
268282
)
@@ -275,15 +289,17 @@ func main() {
275289
// The current device's fingerprint (could be e.g. MAC, mobo ID, GUID, etc.)
276290
fingerprint := uuid.New().String()
277291

292+
ctx := context.Background()
293+
278294
// Keep our example process alive
279295
done := make(chan bool, 1)
280296

281297
// Validate the license for the current fingerprint
282-
license, err := keygen.Validate(fingerprint)
298+
license, err := keygen.Validate(ctx, fingerprint)
283299
switch {
284300
case err == keygen.ErrLicenseNotActivated:
285301
// Activate the current fingerprint
286-
machine, err := license.Activate(fingerprint)
302+
machine, err := license.Activate(ctx, fingerprint)
287303
if err != nil {
288304
fmt.Println("machine activation failed!")
289305

@@ -299,7 +315,7 @@ func main() {
299315
for sig := range sigs {
300316
fmt.Printf("Caught %v, deactivating machine and gracefully exiting...\n", sig)
301317

302-
if err := machine.Deactivate(); err != nil {
318+
if err := machine.Deactivate(ctx); err != nil {
303319
panic(err)
304320
}
305321

@@ -311,7 +327,7 @@ func main() {
311327
}()
312328

313329
// Start a heartbeat monitor for the current machine
314-
if err := machine.Monitor(); err != nil {
330+
if err := machine.Monitor(ctx); err != nil {
315331
fmt.Println("Machine heartbeat monitor failed to start!")
316332

317333
panic(err)
@@ -463,17 +479,21 @@ key does not exist. You can handle this accordingly.
463479
```go
464480
package main
465481

466-
import "github.com/keygen-sh/keygen-go/v3"
482+
import (
483+
"context"
484+
485+
"github.com/keygen-sh/keygen-go/v3"
486+
)
467487

468-
func getLicense() (*keygen.License, error) {
488+
func getLicense(ctx context.Context) (*keygen.License, error) {
469489
keygen.LicenseKey = promptForLicenseKey()
470490

471-
license, err := keygen.Validate()
491+
license, err := keygen.Validate(ctx)
472492
if err != nil {
473493
if _, ok := err.(*keygen.LicenseKeyError); ok {
474494
fmt.Println("License key does not exist!")
475495

476-
return getLicense()
496+
return getLicense(ctx)
477497
}
478498

479499
return nil, err
@@ -486,7 +506,9 @@ func main() {
486506
keygen.Account = "..."
487507
keygen.Product = "..."
488508

489-
license, err := getLicense()
509+
ctx := context.Background()
510+
511+
license, err := getLicense(ctx)
490512
if err != nil {
491513
panic(err)
492514
}
@@ -505,15 +527,15 @@ package main
505527

506528
import "github.com/keygen-sh/keygen-go/v3"
507529

508-
func getLicense() (*keygen.License, error) {
530+
func getLicense(ctx context.Context) (*keygen.License, error) {
509531
keygen.Token = promptForLicenseToken()
510532

511-
license, err := keygen.Validate()
533+
license, err := keygen.Validate(ctx)
512534
if err != nil {
513535
if _, ok := err.(*keygen.LicenseTokenError); ok {
514536
fmt.Println("License token does not exist!")
515537

516-
return getLicense()
538+
return getLicense(ctx)
517539
}
518540

519541
return nil, err
@@ -526,7 +548,9 @@ func main() {
526548
keygen.Account = "..."
527549
keygen.Product = "..."
528550

529-
license, err := getLicense()
551+
ctx := context.Background()
552+
553+
license, err := getLicense(ctx)
530554
if err != nil {
531555
panic(err)
532556
}
@@ -546,15 +570,15 @@ package main
546570

547571
import "github.com/keygen-sh/keygen-go/v3"
548572

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)
551575
if err != nil {
552576
if e, ok := err.(*keygen.RateLimitError); ok {
553577
// Sleep until our rate limit window is passed
554578
time.Sleep(time.Duration(e.RetryAfter) * time.Second)
555579

556580
// Retry validate
557-
return validate()
581+
return validate(ctx)
558582
}
559583

560584
return nil, err
@@ -568,7 +592,9 @@ func main() {
568592
keygen.Product = "YOUR_KEYGEN_PRODUCT_ID"
569593
keygen.LicenseKey = "A_KEYGEN_LICENSE_KEY"
570594

571-
license, err := validate()
595+
ctx := context.Background()
596+
597+
license, err := validate(ctx)
572598
if err != nil {
573599
panic(err)
574600
}
@@ -589,6 +615,8 @@ to implement automatic retries.
589615
package main
590616

591617
import (
618+
"context"
619+
592620
"github.com/hashicorp/go-retryablehttp"
593621
"github.com/keygen-sh/keygen-go/v3"
594622
)
@@ -605,8 +633,10 @@ func main() {
605633
keygen.Product = "YOUR_KEYGEN_PRODUCT_ID"
606634
keygen.LicenseKey = "A_KEYGEN_LICENSE_KEY"
607635

636+
ctx := context.Background()
637+
608638
// Use SDK as you would normally
609-
keygen.Validate()
639+
keygen.Validate(ctx)
610640
}
611641
```
612642

@@ -623,6 +653,7 @@ To do so in Go, you can utilize [`gock`](https://github.com/h2non/gock) or [`htt
623653
package main
624654

625655
import (
656+
"context"
626657
"testing"
627658

628659
"github.com/keygen-sh/keygen-go/v3"
@@ -637,6 +668,7 @@ func init() {
637668
}
638669

639670
func TestExample(t *testing.T) {
671+
ctx := context.Background()
640672
defer gock.Off()
641673

642674
// Intercept Keygen's HTTP client
@@ -664,7 +696,7 @@ func TestExample(t *testing.T) {
664696
keygen.MaxClockDrift = -1
665697

666698
// Use SDK as you would normally
667-
_, err := keygen.Validate()
699+
_, err := keygen.Validate(ctx)
668700
if err != nil {
669701
t.Fatalf("Should not fail mock validation: err=%v", err)
670702
}

0 commit comments

Comments
 (0)