-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathtrial.go
54 lines (51 loc) · 1.38 KB
/
trial.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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
)
func (a *App) CheckTrialCount() (int, error) {
countPath := filepath.Join(logdir, "debug.log")
count := 0
if _, err := os.Stat(countPath); err == nil {
// file exists
countFile, err := os.OpenFile(countPath, os.O_RDONLY, 0666)
if err != nil {
err = errors.Wrap(err, "无法验证试用状态!")
return count, err
}
defer countFile.Close()
countBytes, err := os.ReadFile(countPath)
if err != nil {
err = errors.Wrap(err, "无法验证试用状态!")
return count, err
}
count, err = strconv.Atoi(string(countBytes))
if err != nil {
err = errors.Wrap(err, "无法验证试用状态!")
return count, err
}
countFile.Close()
countFile, err = os.OpenFile(countPath, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
err = errors.Wrap(err, "无法验证试用状态!")
return count, err
}
defer countFile.Close()
countFile.WriteString(strconv.Itoa(count + 1))
return count + 1, nil
} else if os.IsNotExist(err) {
// file not exists
countFile, err := os.OpenFile(countPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
err = errors.Wrap(err, "无法验证试用状态!")
return count, err
}
defer countFile.Close()
countFile.WriteString("1")
return 1, nil
}
return count, fmt.Errorf("无法验证试用状态!")
}