forked from Foxboron/sbctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guid.go
44 lines (39 loc) · 784 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 (
"os"
"path/filepath"
"github.com/foxboron/sbctl/fs"
"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 := fs.Fs.Stat(guidPath); os.IsNotExist(err) {
uuid = CreateUUID()
err := fs.WriteFile(guidPath, uuid, 0644)
if err != nil {
return nil, err
}
} else {
uuid, err = fs.ReadFile(guidPath)
if err != nil {
return nil, err
}
}
return uuid, nil
}
func GetGUID() (uuid.UUID, error) {
b, err := fs.ReadFile(GUIDPath)
if err != nil {
return [16]byte{}, err
}
u, err := uuid.ParseBytes(b)
if err != nil {
return [16]byte{}, err
}
return u, err
}