-
Notifications
You must be signed in to change notification settings - Fork 31
/
count.go
116 lines (94 loc) · 2.16 KB
/
count.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
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
107
108
109
110
111
112
113
114
115
116
package metric
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
"github.com/aziontech/azion-cli/pkg/cmd/version"
"github.com/aziontech/azion-cli/pkg/github"
"github.com/riywo/loginshell"
"github.com/aziontech/azion-cli/pkg/config"
"github.com/spf13/cobra"
)
type command struct {
TotalSuccess int
TotalFailed int
ExecutionTime float64
CLIVersion string
VulcanVersion string
Shell string
}
func TotalCommandsCount(cmd *cobra.Command, commandName string, executionTime float64, errExec error) error {
if commandName == "" {
return nil
}
success := true
if errExec != nil {
success = false
}
dir, err := config.Dir()
if err != nil {
return err
}
ignoredWords := map[string]bool{
"__complete": true,
"completion": true,
}
if ignoredWords[commandName] {
return nil
}
metricsLocation := filepath.Join(dir.Dir, dir.Metrics)
file, err := os.OpenFile(metricsLocation, os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
return err
}
defer file.Close()
var data map[string]*command
decoder := json.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil && err != io.EOF {
return err
}
tagName, err := github.GetVersionGitHub("vulcan")
if err != nil {
return err
}
shell, err := loginshell.Shell()
if err != nil {
return err
}
// If EOF is encountered or the file is empty, initialize data as an empty map
if data == nil {
data = make(map[string]*command)
}
if data[commandName] == nil {
data[commandName] = &command{}
}
data[commandName].ExecutionTime = executionTime
data[commandName].CLIVersion = version.BinVersion
if len(tagName) > 0 {
data[commandName].VulcanVersion = tagName[1:]
}
data[commandName].Shell = shell
if success {
data[commandName].TotalSuccess++
} else {
data[commandName].TotalFailed++
}
// Reset file offset to the beginning
_, err = file.Seek(0, 0)
if err != nil {
return err
}
// Truncate the file in case the new content is smaller than the previous one
err = file.Truncate(0)
if err != nil {
return err
}
// Encode and write the updated map back to the file
if err := json.NewEncoder(file).Encode(data); err != nil {
log.Fatal(err)
}
return nil
}