@@ -6,10 +6,10 @@ import (
66 "os"
77 "os/signal"
88 "runtime"
9- "runtime/pprof"
109 "syscall"
1110
1211 "github.com/spf13/cobra"
12+ "github.com/spf13/pflag"
1313 "github.com/taoky/ayano/pkg/analyze"
1414 "github.com/taoky/ayano/pkg/fileiter"
1515 "github.com/taoky/ayano/pkg/systemd"
@@ -123,49 +123,24 @@ func runCmd() *cobra.Command {
123123 return cmd
124124}
125125
126+ func normalizeAnalyzeFlags (f * pflag.FlagSet , name string ) pflag.NormalizedName {
127+ switch name {
128+ case "cpuprofile" :
129+ name = "cpuprof"
130+ case "memprofile" :
131+ name = "memprof"
132+ }
133+ return pflag .NormalizedName (name )
134+ }
135+
126136// First, create a helper function to handle common command configuration
127137func setupAnalyzeCommand (cmd * cobra.Command , cmdType string ) (analyze.AnalyzerConfig , error ) {
128138 config := analyze .DefaultConfig ()
129139 config .InstallFlags (cmd .Flags (), cmd .Name ())
130-
131- var cpuProf string
132- var memProf string
133- cmd .Flags ().StringVar (& cpuProf , "cpuprof" , "" , "write CPU pprof data to file" )
134- cmd .Flags ().StringVar (& memProf , "memprof" , "" , "write memory pprof data to file" )
135-
136- // Return a function to handle performance profiling logic
137- handleProf := func () error {
138- if cpuProf != "" {
139- f , err := os .Create (cpuProf )
140- if err != nil {
141- return fmt .Errorf ("failed to create CPU pprof file: %w" , err )
142- }
143- defer f .Close ()
144- if err := pprof .StartCPUProfile (f ); err != nil {
145- return fmt .Errorf ("failed to start CPU pprof: %w" , err )
146- }
147- defer pprof .StopCPUProfile ()
148- }
149-
150- if memProf != "" {
151- f , err := os .Create (memProf )
152- if err != nil {
153- return fmt .Errorf ("failed to create memory pprof file: %w" , err )
154- }
155- defer f .Close ()
156- if err := pprof .WriteHeapProfile (f ); err != nil {
157- return fmt .Errorf ("failed to write memory pprof: %w" , err )
158- }
159- }
160- return nil
161- }
140+ cmd .Flags ().SetNormalizeFunc (normalizeAnalyzeFlags )
162141
163142 // Set the command execution function
164143 cmd .RunE = func (cmd * cobra.Command , args []string ) error {
165- if err := handleProf (); err != nil {
166- return err
167- }
168-
169144 switch cmdType {
170145 case "analyze" :
171146 config .Analyze = true
@@ -192,8 +167,9 @@ func analyzeCmd() *cobra.Command {
192167
193168func dirAnalyzeCmd () * cobra.Command {
194169 cmd := & cobra.Command {
195- Use : "dir-analyze [filename...]" ,
196- Short : "Analyze log by directory (show statistics for each first-level directory)" ,
170+ Use : "dir-analyze [filename...]" ,
171+ Aliases : []string {"dir-analyse" },
172+ Short : "Analyze log by directory (show statistics for each first-level directory)" ,
197173 }
198174 setupAnalyzeCommand (cmd , "dir-analyze" )
199175 return cmd
0 commit comments