@@ -25,8 +25,13 @@ import (
2525 "github.com/simonleung8/flags"
2626)
2727
28+ // Assert that JavaPlugin implements plugin.Plugin.
29+ var _ plugin.Plugin = (* JavaPlugin )(nil )
30+
2831// The JavaPlugin is a cf cli plugin that supports taking heap and thread dumps on demand
29- type JavaPlugin struct {}
32+ type JavaPlugin struct {
33+ verbose bool
34+ }
3035
3136// UUIDGenerator is an interface that encapsulates the generation of UUIDs
3237type UUIDGenerator interface {
@@ -86,49 +91,39 @@ const (
8691// 1 should the plugin exit nonzero.
8792func (c * JavaPlugin ) Run (cliConnection plugin.CliConnection , args []string ) {
8893 // Check if verbose flag is in args for early logging
89- verbose := false
9094 for _ , arg := range args {
9195 if arg == "-v" || arg == "--verbose" {
92- verbose = true
96+ c . verbose = true
9397 break
9498 }
9599 }
96100
97- if verbose {
101+ if c . verbose {
98102 fmt .Printf ("[VERBOSE] Run called with args: %v\n " , args )
99103 }
100104
101- _ , err := c .DoRun (& commandExecutorImpl {cliConnection : cliConnection }, & uuidGeneratorImpl {}, utils. CfJavaPluginUtilImpl {}, args )
105+ _ , err := c .DoRun (& commandExecutorImpl {cliConnection : cliConnection }, & uuidGeneratorImpl {}, args )
102106 if err != nil {
103- if verbose {
107+ if c . verbose {
104108 fmt .Printf ("[VERBOSE] Error occurred: %v\n " , err )
105109 }
106110 os .Exit (1 )
107111 }
108- if verbose {
112+ if c . verbose {
109113 fmt .Printf ("[VERBOSE] Run completed successfully\n " )
110114 }
111115}
112116
113117// DoRun is an internal method that we use to wrap the cmd package with CommandExecutor for test purposes
114- func (c * JavaPlugin ) DoRun (commandExecutor cmd.CommandExecutor , uuidGenerator UUIDGenerator , util utils. CfJavaPluginUtil , args []string ) (string , error ) {
118+ func (c * JavaPlugin ) DoRun (commandExecutor cmd.CommandExecutor , uuidGenerator UUIDGenerator , args []string ) (string , error ) {
115119 traceLogger := trace .NewLogger (os .Stdout , true , os .Getenv ("CF_TRACE" ), "" )
116120 ui := terminal .NewUI (os .Stdin , os .Stdout , terminal .NewTeePrinter (os .Stdout ), traceLogger )
117121
118- // Check if verbose flag is in args for early logging
119- verbose := false
120- for _ , arg := range args {
121- if arg == "-v" || arg == "--verbose" {
122- verbose = true
123- break
124- }
125- }
126-
127- if verbose {
122+ if c .verbose {
128123 fmt .Printf ("[VERBOSE] DoRun called with args: %v\n " , args )
129124 }
130125
131- output , err := c .execute (commandExecutor , uuidGenerator , util , args )
126+ output , err := c .execute (commandExecutor , uuidGenerator , args )
132127 if err != nil {
133128 if err .Error () == "unexpected EOF" {
134129 return output , err
@@ -491,7 +486,7 @@ func toSentenceCase(input string) string {
491486 return strings .ToUpper (string (input [0 ])) + strings .ToLower (input [1 :])
492487}
493488
494- func (c * JavaPlugin ) execute (commandExecutor cmd.CommandExecutor , uuidGenerator UUIDGenerator , util utils. CfJavaPluginUtil , args []string ) (string , error ) {
489+ func (c * JavaPlugin ) execute (commandExecutor cmd.CommandExecutor , uuidGenerator UUIDGenerator , args []string ) (string , error ) {
495490 if len (args ) == 0 {
496491 return "" , & InvalidUsageError {message : "No command provided" }
497492 }
@@ -536,7 +531,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
536531 verbose := commandFlags .IsSet ("verbose" )
537532
538533 // Helper function for verbose logging with format strings
539- logVerbose := func (format string , args ... interface {} ) {
534+ logVerbose := func (format string , args ... any ) {
540535 if verbose {
541536 fmt .Printf ("[VERBOSE] " + format + "\n " , args ... )
542537 }
@@ -637,7 +632,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
637632
638633 logVerbose ("CF SSH arguments: %v" , cfSSHArguments )
639634
640- supported , err := util .CheckRequiredTools (applicationName )
635+ supported , err := utils .CheckRequiredTools (applicationName )
641636
642637 if err != nil || ! supported {
643638 return "required tools checking failed" , err
@@ -670,7 +665,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
670665 // Initialize fspath and fileName for commands that need them
671666 if command .GenerateFiles || command .NeedsFileName || command .GenerateArbitraryFiles {
672667 logVerbose ("Command requires file generation" )
673- fspath , err = util .GetAvailablePath (applicationName , remoteDir )
668+ fspath , err = utils .GetAvailablePath (applicationName , remoteDir )
674669 if err != nil {
675670 return "" , fmt .Errorf ("failed to get available path: %w" , err )
676671 }
@@ -745,10 +740,10 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
745740 switch command .FileExtension {
746741 case ".hprof" :
747742 logVerbose ("Finding heap dump file" )
748- finalFile , err = util .FindHeapDumpFile (cfSSHArguments , fileName , fspath )
743+ finalFile , err = utils .FindHeapDumpFile (cfSSHArguments , fileName , fspath )
749744 case ".jfr" :
750745 logVerbose ("Finding JFR file" )
751- finalFile , err = util .FindJFRFile (cfSSHArguments , fileName , fspath )
746+ finalFile , err = utils .FindJFRFile (cfSSHArguments , fileName , fspath )
752747 default :
753748 return "" , & InvalidUsageError {message : fmt .Sprintf ("Unsupported file extension %q" , command .FileExtension )}
754749 }
@@ -764,7 +759,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
764759
765760 localFileFullPath := localDir + "/" + applicationName + "-" + command .FileNamePart + "-" + uuidGenerator .Generate () + command .FileExtension
766761 logVerbose ("Downloading file to: %s" , localFileFullPath )
767- err = util .CopyOverCat (cfSSHArguments , fileName , localFileFullPath )
762+ err = utils .CopyOverCat (cfSSHArguments , fileName , localFileFullPath )
768763 if err == nil {
769764 logVerbose ("File download completed successfully" )
770765 fmt .Println (toSentenceCase (command .FileLabel ) + " file saved to: " + localFileFullPath )
@@ -775,7 +770,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
775770
776771 if ! keepAfterDownload {
777772 logVerbose ("Deleting remote file" )
778- err = util .DeleteRemoteFile (cfSSHArguments , fileName )
773+ err = utils .DeleteRemoteFile (cfSSHArguments , fileName )
779774 if err != nil {
780775 logVerbose ("Failed to delete remote file: %v" , err )
781776 return "" , err
@@ -790,7 +785,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
790785 logVerbose ("Processing arbitrary files download: %s" , fspath )
791786 logVerbose ("cfSSHArguments: %v" , cfSSHArguments )
792787 // download all files in the generic folder
793- files , err := util .ListFiles (cfSSHArguments , fspath )
788+ files , err := utils .ListFiles (cfSSHArguments , fspath )
794789 for i , file := range files {
795790 logVerbose ("File %d: %s" , i + 1 , file )
796791 }
@@ -803,7 +798,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
803798 for _ , file := range files {
804799 logVerbose ("Downloading file: %s" , file )
805800 localFileFullPath := localDir + "/" + file
806- err = util .CopyOverCat (cfSSHArguments , fspath + "/" + file , localFileFullPath )
801+ err = utils .CopyOverCat (cfSSHArguments , fspath + "/" + file , localFileFullPath )
807802 if err == nil {
808803 logVerbose ("File %s downloaded successfully" , file )
809804 fmt .Printf ("File %s saved to: %s\n " , file , localFileFullPath )
@@ -815,7 +810,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
815810
816811 if ! keepAfterDownload {
817812 logVerbose ("Deleting remote file folder" )
818- err = util .DeleteRemoteFile (cfSSHArguments , fspath )
813+ err = utils .DeleteRemoteFile (cfSSHArguments , fspath )
819814 if err != nil {
820815 logVerbose ("Failed to delete remote folder: %v" , err )
821816 return "" , err
@@ -902,7 +897,7 @@ func (c *JavaPlugin) GetMetadata() plugin.PluginMetadata {
902897 }
903898}
904899
905- // Unlike most Go programs, the `Main ()` function will not be used to run all of the
900+ // Unlike most Go programs, the `main ()` function will not be used to run all of the
906901// commands provided in your plugin. Main will be used to initialize the plugin
907902// process, as well as any dependencies you might require for your
908903// plugin.
0 commit comments