-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
key.go
77 lines (63 loc) · 1.79 KB
/
key.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
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
package lock
import (
"encoding/base64"
"fmt"
"io"
"os"
"path/filepath"
"github.com/google/uuid"
)
func GenerateLicenseKey() (string, error) {
uuid := uuid.New()
uuidBytes := uuid[:]
licenseKeyBytes := append(uuidBytes)
licenseKey := base64.StdEncoding.EncodeToString(licenseKeyBytes)
return licenseKey, nil
}
func WriteLicenseKeyToFile(licenseKey string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("Error getting user home directory: %v", err)
}
licenseFilePath := filepath.Join(homeDir, ".tempxcli")
licenseFile, err := os.Create(licenseFilePath)
if err != nil {
return fmt.Errorf("Error creating license file: %v", err)
}
defer licenseFile.Close()
_, err = licenseFile.WriteString(licenseKey)
if err != nil {
return fmt.Errorf("Error writing license key to file: %v", err)
}
return nil
}
func ReadLicenseKeyFromFile() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("Error getting user home directory: %v", err)
}
licenseFilePath := filepath.Join(homeDir, ".tempxcli")
licenseFile, err := os.Open(licenseFilePath)
if err != nil {
return "", fmt.Errorf("Error opening license file: %v", err)
}
defer licenseFile.Close()
licenseFileBytes, err := io.ReadAll(licenseFile)
if err != nil {
return "", fmt.Errorf("Error reading license file: %v", err)
}
licenseKey := string(licenseFileBytes)
return licenseKey, nil
}
func ClearLicenseFile() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("Error getting user home directory: %v", err)
}
licenseFilePath := filepath.Join(homeDir, ".tempxcli")
err = os.Remove(licenseFilePath)
if err != nil {
return fmt.Errorf("Error deleting license file: %v", err)
}
return nil
}