-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlicense_service.go
More file actions
106 lines (92 loc) · 2.65 KB
/
Copy pathlicense_service.go
File metadata and controls
106 lines (92 loc) · 2.65 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
package main
import (
"context"
"time"
"yaria/pkg/license"
)
// LicenseService provides license management methods to the frontend.
// Always included in both free and pro builds.
type LicenseService struct {
ctx context.Context
}
func (l *LicenseService) startup(ctx context.Context) {
l.ctx = ctx
}
// CheckLicense returns the current license status.
func (l *LicenseService) CheckLicense() map[string]interface{} {
info := license.CheckLicense()
return map[string]interface{}{
"valid": info.Valid,
"plan": info.Plan,
"email": info.Email,
"device_id": info.DeviceID,
"device_name": info.DeviceName,
"key": info.Key,
"expires_at": formatExpiry(info.ExpiresAt),
"pro_available": ProAvailable(),
}
}
func formatExpiry(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format("2006-01-02")
}
// ActivateKey activates a license key for this device.
func (l *LicenseService) ActivateKey(key string) map[string]interface{} {
if key == "" {
return map[string]interface{}{"error": "license key is required"}
}
info, err := license.ActivateKey(key)
if err != nil {
proInvalidate()
return map[string]interface{}{"error": err.Error()}
}
proInvalidate()
return map[string]interface{}{
"valid": info.Valid,
"plan": info.Plan,
"email": info.Email,
"device_id": info.DeviceID,
"device_name": info.DeviceName,
"expires_at": formatExpiry(info.ExpiresAt),
}
}
// StartTrial starts a one-time 30-day Pro trial for this device.
func (l *LicenseService) StartTrial() map[string]interface{} {
info, err := license.StartTrial()
if err != nil {
proInvalidate()
return map[string]interface{}{"error": err.Error()}
}
proInvalidate()
return map[string]interface{}{
"valid": info.Valid,
"plan": info.Plan,
"email": info.Email,
"device_id": info.DeviceID,
"device_name": info.DeviceName,
"expires_at": formatExpiry(info.ExpiresAt),
}
}
// Deactivate removes the stored license.
func (l *LicenseService) Deactivate() map[string]interface{} {
if err := license.Deactivate(); err != nil {
return map[string]interface{}{"error": err.Error()}
}
proInvalidate()
return map[string]interface{}{"status": "deactivated"}
}
// GetDeviceInfo returns the current device ID and summary.
func (l *LicenseService) GetDeviceInfo() map[string]interface{} {
id, summary := license.GetDeviceInfo()
return map[string]interface{}{
"device_id": id,
"device_name": summary,
}
}
// IsPro returns whether this device has an active pro license AND
// the pro module is compiled in.
func (l *LicenseService) IsPro() bool {
return ProAvailable() && license.IsPro()
}