-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
74 lines (62 loc) · 1.55 KB
/
Copy pathexample_test.go
File metadata and controls
74 lines (62 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package macwifi_test
import (
"context"
"fmt"
"log"
"time"
"github.com/jaisonerick/macwifi"
)
// One-shot scan of nearby WiFi networks.
func ExampleScan() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
networks, err := macwifi.Scan(ctx)
if err != nil {
log.Fatal(err)
}
for _, n := range networks {
fmt.Printf("%s %s %d dBm %s\n",
n.SSID, n.BSSID, n.RSSI, n.Security)
}
}
// Read a saved password from the macOS Keychain. The user sees a
// system Allow/Deny dialog; OnKeychainAccess runs just before it
// appears so a CLI or TUI can nudge the user to expect the prompt.
func ExamplePassword() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
password, err := macwifi.Password(ctx, "MyHomeWiFi",
macwifi.OnKeychainAccess(func(ssid string) {
fmt.Printf("Approve the macOS Keychain prompt for %q\n", ssid)
}),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(password)
}
// Reuse one helper session for a scan followed by a password lookup.
// Calling Scan and Password on the same Client avoids relaunching
// the signed helper app between operations.
func ExampleNew() {
ctx := context.Background()
c, err := macwifi.New(ctx)
if err != nil {
log.Fatal(err)
}
defer c.Close()
networks, err := c.Scan(ctx)
if err != nil {
log.Fatal(err)
}
for _, n := range networks {
if !n.Current {
continue
}
password, err := c.Password(ctx, n.SSID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("connected: %s (%s)\n", n.SSID, password)
}
}