-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
142 lines (120 loc) · 3.2 KB
/
example_test.go
File metadata and controls
142 lines (120 loc) · 3.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package machineid_test
import (
"context"
"fmt"
"github.com/slashdevops/machineid"
)
// ExampleNew demonstrates the simplest way to generate a machine ID.
func ExampleNew() {
provider := machineid.New().
WithCPU().
WithSystemUUID()
id, err := provider.ID(context.Background())
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
fmt.Printf("ID length: %d\n", len(id))
// Output:
// ID length: 64
}
// ExampleProvider_WithSalt shows how a salt produces application-specific IDs.
func ExampleProvider_WithSalt() {
ctx := context.Background()
id1, _ := machineid.New().
WithCPU().
WithSystemUUID().
WithSalt("app-one").
ID(ctx)
id2, _ := machineid.New().
WithCPU().
WithSystemUUID().
WithSalt("app-two").
ID(ctx)
fmt.Printf("Same length: %v\n", len(id1) == len(id2))
fmt.Printf("Different IDs: %v\n", id1 != id2)
// Output:
// Same length: true
// Different IDs: true
}
// ExampleProvider_WithFormat demonstrates the four power-of-two output formats.
func ExampleProvider_WithFormat() {
ctx := context.Background()
base := func(mode machineid.FormatMode) int {
id, err := machineid.New().
WithCPU().
WithSystemUUID().
WithFormat(mode).
ID(ctx)
if err != nil {
return -1
}
return len(id)
}
fmt.Printf("Format32: %d chars\n", base(machineid.Format32))
fmt.Printf("Format64: %d chars\n", base(machineid.Format64))
fmt.Printf("Format128: %d chars\n", base(machineid.Format128))
fmt.Printf("Format256: %d chars\n", base(machineid.Format256))
// Output:
// Format32: 32 chars
// Format64: 64 chars
// Format128: 128 chars
// Format256: 256 chars
}
// ExampleProvider_Validate shows how to check a stored ID against the current machine.
func ExampleProvider_Validate() {
provider := machineid.New().
WithCPU().
WithSystemUUID()
id, _ := provider.ID(context.Background())
// Validate the correct ID
valid, _ := provider.Validate(context.Background(), id)
fmt.Printf("Correct ID valid: %v\n", valid)
// Validate an incorrect ID
valid, _ = provider.Validate(context.Background(), "0000000000000000000000000000000000000000000000000000000000000000")
fmt.Printf("Wrong ID valid: %v\n", valid)
// Output:
// Correct ID valid: true
// Wrong ID valid: false
}
// ExampleProvider_VMFriendly_preset demonstrates the VM-friendly preset.
func ExampleProvider_VMFriendly_preset() {
id, err := machineid.New().
VMFriendly().
ID(context.Background())
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
fmt.Printf("VM-friendly ID length: %d\n", len(id))
// Output:
// VM-friendly ID length: 64
}
// ExampleProvider_ID_allComponents shows using every available hardware source.
func ExampleProvider_ID_allComponents() {
provider := machineid.New().
WithCPU().
WithMotherboard().
WithSystemUUID().
WithMAC().
WithDisk().
WithSalt("full-example")
id, err := provider.ID(context.Background())
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
fmt.Printf("Full ID length: %d\n", len(id))
fmt.Printf("Is hex: %v\n", isAllHex(id))
// Output:
// Full ID length: 64
// Is hex: true
}
func isAllHex(s string) bool {
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
return len(s) > 0
}