forked from Foxboron/sbctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guid.go
44 lines (39 loc) · 770 Bytes
/
guid.go
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
package sbctl
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/google/uuid"
)
func CreateUUID() []byte {
id, _ := uuid.NewRandom()
return []byte(id.String())
}
func CreateGUID(output string) ([]byte, error) {
var uuid []byte
guidPath := filepath.Join(output, "GUID")
if _, err := os.Stat(guidPath); os.IsNotExist(err) {
uuid = CreateUUID()
err := ioutil.WriteFile(guidPath, uuid, 0600)
if err != nil {
return nil, err
}
} else {
uuid, err = ioutil.ReadFile(guidPath)
if err != nil {
return nil, err
}
}
return uuid, nil
}
func GetGUID() (uuid.UUID, error) {
b, err := os.ReadFile(GUIDPath)
if err != nil {
return [16]byte{}, err
}
u, err := uuid.ParseBytes(b)
if err != nil {
return [16]byte{}, err
}
return u, err
}