Skip to content

Commit 3c92e40

Browse files
committed
Improve examples
1 parent 2bcde89 commit 3c92e40

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

device/examples_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
package device
1+
package device_test
22

33
import (
44
"context"
55
"fmt"
66
"net/http"
77
"os"
8+
9+
"github.com/cli/oauth/device"
810
)
911

1012
// This demonstrates how to perform OAuth Device Authorization Flow for GitHub.com.
1113
// After RequestCode successfully completes, the client app should prompt the user to copy
1214
// the UserCode and to open VerificationURI in their web browser to enter the code.
13-
func Example() {
15+
func ExampleRequestCode() {
1416
clientID := os.Getenv("OAUTH_CLIENT_ID")
1517
scopes := []string{"repo", "read:org"}
1618
httpClient := http.DefaultClient
1719

18-
code, err := RequestCode(httpClient, "https://github.com/login/device/code", clientID, scopes)
20+
code, err := device.RequestCode(httpClient, "https://github.com/login/device/code", clientID, scopes)
1921
if err != nil {
2022
panic(err)
2123
}
2224

2325
fmt.Printf("Copy code: %s\n", code.UserCode)
2426
fmt.Printf("then open: %s\n", code.VerificationURI)
2527

26-
accessToken, err := Wait(context.TODO(), httpClient, "https://github.com/login/oauth/access_token", WaitOptions{
28+
accessToken, err := device.Wait(context.TODO(), httpClient, "https://github.com/login/oauth/access_token", device.WaitOptions{
2729
ClientID: clientID,
2830
DeviceCode: code,
2931
})

examples_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
package oauth
1+
package oauth_test
22

33
import (
44
"fmt"
55
"os"
6+
7+
"github.com/cli/oauth"
68
)
79

8-
// Try initiating OAuth Device flow on the server and fall back to OAuth Web application flow if
9-
// Device flow seems unsupported. This approach isn't strictly needed for github.com, as its Device
10-
// flow support is globally available, but enables logging in to hosted GitHub instances as well.
11-
func Example() {
12-
flow := &Flow{
13-
Host: GitHubHost("https://github.com"),
10+
// DetectFlow attempts to initiate OAuth Device flow with the server and falls back to OAuth Web
11+
// application flow if Device flow seems unsupported. This approach isn't strictly needed for
12+
// github.com, as its Device flow support is globally available, but it enables logging in to
13+
// self-hosted GitHub instances as well.
14+
func ExampleFlow_DetectFlow() {
15+
flow := &oauth.Flow{
16+
Host: oauth.GitHubHost("https://github.com"),
1417
ClientID: os.Getenv("OAUTH_CLIENT_ID"),
1518
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), // only applicable to web app flow
1619
CallbackURI: "http://127.0.0.1/callback", // only applicable to web app flow

webapp/examples_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package webapp
1+
package webapp_test
22

33
import (
44
"context"
@@ -7,22 +7,23 @@ import (
77
"os"
88

99
"github.com/cli/browser"
10+
"github.com/cli/oauth/webapp"
1011
)
1112

12-
// This demonstrates how to perform OAuth App Authorization Flow for GitHub.com.
13-
// Ensure that the OAuth app on GitHub lists the callback URL: "http://127.0.0.1/callback"
14-
func Example() {
13+
// Initiate the OAuth App Authorization Flow for GitHub.com.
14+
func ExampleInitFlow() {
1515
clientID := os.Getenv("OAUTH_CLIENT_ID")
1616
clientSecret := os.Getenv("OAUTH_CLIENT_SECRET")
17+
callbackURL := "http://127.0.0.1/callback"
1718

18-
flow, err := InitFlow()
19+
flow, err := webapp.InitFlow()
1920
if err != nil {
2021
panic(err)
2122
}
2223

23-
params := BrowserParams{
24+
params := webapp.BrowserParams{
2425
ClientID: clientID,
25-
RedirectURI: "http://127.0.0.1/callback",
26+
RedirectURI: callbackURL,
2627
Scopes: []string{"repo", "read:org"},
2728
AllowSignup: true,
2829
}
@@ -43,7 +44,7 @@ func Example() {
4344
}
4445

4546
httpClient := http.DefaultClient
46-
accessToken, err := flow.Wait(context.TODO(), httpClient, "https://github.com/login/oauth/access_token", WaitOptions{
47+
accessToken, err := flow.Wait(context.TODO(), httpClient, "https://github.com/login/oauth/access_token", webapp.WaitOptions{
4748
ClientSecret: clientSecret,
4849
})
4950
if err != nil {

0 commit comments

Comments
 (0)