-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
177 lines (152 loc) · 5.12 KB
/
profile.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//go:build profile
// Program entry point
package main
import (
"fmt"
"log/slog"
"os"
"runtime"
"runtime/pprof"
"github.com/Joao-Felisberto/devprivops/cmd"
"github.com/Joao-Felisberto/devprivops/fs"
"github.com/Joao-Felisberto/devprivops/schema"
"github.com/Joao-Felisberto/devprivops/util"
"github.com/spf13/cobra"
)
var verbose = false // Whether the log should log more information or not
var profile = ""
// Builds the command and delegates execution to the appropriate function from the cmd package
func main() {
/*
cpuProfileFile, err := os.Create("cpu")
if err != nil {
fmt.Println(err)
}
memoryProfileFile, err := os.Create("mem")
if err != nil {
fmt.Println(err)
}
pprof.StartCPUProfile(cpuProfileFile)
defer pprof.StopCPUProfile()
*/
var rootCmd = &cobra.Command{
Use: util.AppName,
Short: fmt.Sprintf("A CLI application to analyze %s", util.AppName),
RunE: func(cmd_ *cobra.Command, args []string) error {
return fmt.Errorf("please specify a subcommand. Use '%s --help' for usage details", util.AppName)
},
}
var analyseCmd = &cobra.Command{
Use: "analyse <username> <password> <database ip> <database port> <dataset>",
Short: fmt.Sprintf("Analyse the specified database endpoint for %s", util.AppName),
Args: cobra.ExactArgs(5),
RunE: func(cmd_ *cobra.Command, args []string) error {
memoryProfileFile, err := os.Create("mem.prof")
if profile == "cpu" {
cpuProfileFile, err := os.Create("cpu.prof")
if err != nil {
return err
}
pprof.StartCPUProfile(cpuProfileFile)
} else if profile == "mem" {
// memoryProfileFile, err := os.Create("mem.prof")
if err != nil {
return err
}
}
logLevel := slog.LevelInfo
if verbose {
logLevel = slog.LevelDebug
}
util.SetupLogger(logLevel)
res := cmd.Analyse(cmd_, args)
if profile == "cpu" {
pprof.StopCPUProfile()
} else if profile == "mem" {
pprof.WriteHeapProfile(memoryProfileFile)
}
return res
},
}
var testCmd = &cobra.Command{
Use: "test <username> <password> <database ip> <database port> <dataset>",
Short: "Tests the queries against user-defined scenarios",
Args: cobra.ExactArgs(5),
RunE: func(cmd_ *cobra.Command, args []string) error {
// memoryProfileFile, err := os.Create("mem.prof")
if profile == "cpu" {
runtime.SetCPUProfileRate(400)
cpuProfileFile, err := os.Create("cpu.prof")
if err != nil {
return err
}
pprof.StartCPUProfile(cpuProfileFile)
} /*else if profile == "mem" {
memoryProfileFile, err := os.Create("mem.prof")
if err != nil {
return err
}
}*/
logLevel := slog.LevelInfo
if verbose {
logLevel = slog.LevelDebug
}
util.SetupLogger(logLevel)
res := cmd.Test(cmd_, args)
if profile == "cpu" {
pprof.StopCPUProfile()
} else if profile == "mem" {
memoryProfileFile, err := os.Create("mem.prof")
if err != nil {
return err
}
pprof.WriteHeapProfile(memoryProfileFile)
}
return res
},
}
var schemaCmd = &cobra.Command{
Use: "schema <attack-tree|query|report|requirement>",
Short: "Prints the internal json schema",
Args: cobra.ExactArgs(1),
RunE: func(cmd_ *cobra.Command, args []string) error {
logLevel := slog.LevelInfo
if verbose {
logLevel = slog.LevelDebug
}
util.SetupLogger(logLevel)
schemaName := args[0]
switch schemaName {
case "attack-tree":
fmt.Print(schema.ATK_TREE_SCHEMA)
case "query":
fmt.Print(schema.QUERY_SCHEMA)
case "report":
fmt.Print(schema.REPORT_DATA_SCHEMA)
case "requirement":
fmt.Print(schema.REQUIREMENT_SCHEMA)
default:
return fmt.Errorf("schema '%s' not found, valid possibilities: [attack-tree, query, report, requirement]", schemaName)
}
return nil
},
}
analyseCmd.Flags().StringVar(&util.ReportEndpoint, "report-endpoint", "", "Endpoint where to send the final report")
analyseCmd.Flags().BoolVar(&util.Pipeline, "pipeline", false, "whether to format the output for pipeline usage")
testCmd.Flags().BoolVar(&util.Pipeline, "pipeline", false, "whether to format the output for pipeline usage")
analyseCmd.Flags().StringVar(&fs.GlobalDir, "global-dir", fmt.Sprintf("/etc/%s", util.AppName), "The path to the global configurations")
testCmd.Flags().StringVar(&fs.GlobalDir, "global-dir", fmt.Sprintf("/etc/%s", util.AppName), "The path to the global configurations")
analyseCmd.Flags().StringVar(&fs.LocalDir, "local-dir", fmt.Sprintf("./.%s", util.AppName), "The path to the local configurations")
testCmd.Flags().StringVar(&fs.LocalDir, "local-dir", fmt.Sprintf("./.%s", util.AppName), "The path to the local configurations")
analyseCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "whether to display debug messages")
testCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "whether to display debug messages")
analyseCmd.Flags().StringVar(&profile, "profile", "", "What to profile")
testCmd.Flags().StringVar(&profile, "profile", "", "What to profile")
rootCmd.AddCommand(analyseCmd)
rootCmd.AddCommand(testCmd)
rootCmd.AddCommand(schemaCmd)
if err := rootCmd.Execute(); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}