Skip to content

Commit f792cf1

Browse files
authored
fix dependencies, polish code (#46)
* fix dependencies * assert interface * make code more idiomatic * add verbose to JavaPlugin * remove useless struct & interface
1 parent d1fed2b commit f792cf1

File tree

5 files changed

+252
-307
lines changed

5 files changed

+252
-307
lines changed

cf_cli_java_plugin.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3237
type UUIDGenerator interface {
@@ -86,49 +91,39 @@ const (
8691
// 1 should the plugin exit nonzero.
8792
func (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.

go.mod

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,45 @@
11
module cf.plugin.ref/requires
22

3-
go 1.23.0
3+
go 1.24.3
44

5-
toolchain go1.23.5
5+
toolchain go1.24.4
66

77
require (
8-
code.cloudfoundry.org/cli v7.1.0+incompatible
8+
code.cloudfoundry.org/cli v0.0.0-20250623142502-fb19e7a825ee
9+
github.com/lithammer/fuzzysearch v1.1.8
910
github.com/satori/go.uuid v1.2.0
1011
github.com/simonleung8/flags v0.0.0-20170704170018-8020ed7bcf1a
1112
)
1213

1314
require (
14-
code.cloudfoundry.org/bytefmt v0.0.0-20210608160410-67692ebc98de // indirect
15-
code.cloudfoundry.org/cli-plugin-repo v0.0.0-20250414213145-88c4afe9cd65 // indirect
16-
code.cloudfoundry.org/go-log-cache v1.0.0 // indirect
17-
code.cloudfoundry.org/go-loggregator v7.4.0+incompatible // indirect
18-
code.cloudfoundry.org/gofileutils v0.0.0-20170111115228-4d0c80011a0f // indirect
15+
code.cloudfoundry.org/bytefmt v0.42.0 // indirect
1916
code.cloudfoundry.org/jsonry v1.1.4 // indirect
20-
code.cloudfoundry.org/rfc5424 v0.0.0-20201103192249-000122071b78 // indirect
21-
code.cloudfoundry.org/tlsconfig v0.0.0-20210615191307-5d92ef3894a7 // indirect
22-
code.cloudfoundry.org/ykk v0.0.0-20170424192843-e4df4ce2fd4d // indirect
23-
github.com/SermoDigital/jose v0.9.1 // indirect
24-
github.com/blang/semver v3.5.1+incompatible // indirect
17+
code.cloudfoundry.org/tlsconfig v0.29.0 // indirect
18+
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 // indirect
19+
github.com/blang/semver/v4 v4.0.0 // indirect
2520
github.com/bmatcuk/doublestar v1.3.4 // indirect
26-
github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f // indirect
27-
github.com/charlievieth/fs v0.0.1 // indirect
21+
github.com/charlievieth/fs v0.0.3 // indirect
2822
github.com/cloudfoundry/bosh-cli v6.4.1+incompatible // indirect
29-
github.com/cloudfoundry/bosh-utils v0.0.264 // indirect
30-
github.com/cppforlife/go-patch v0.2.0 // indirect
31-
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
32-
github.com/fatih/color v1.12.0 // indirect
33-
github.com/golang/protobuf v1.5.4 // indirect
34-
github.com/google/go-querystring v1.1.0 // indirect
35-
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
36-
github.com/jessevdk/go-flags v1.5.0 // indirect
23+
github.com/cloudfoundry/bosh-utils v0.0.397 // indirect
24+
github.com/cppforlife/go-patch v0.1.0 // indirect
25+
github.com/fatih/color v1.18.0 // indirect
26+
github.com/google/uuid v1.6.0 // indirect
27+
github.com/jessevdk/go-flags v1.6.1 // indirect
3728
github.com/kr/pretty v0.3.1 // indirect
38-
github.com/lithammer/fuzzysearch v1.1.8 // indirect
3929
github.com/lunixbochs/vtclean v1.0.0 // indirect
40-
github.com/mattn/go-colorable v0.1.8 // indirect
41-
github.com/mattn/go-isatty v0.0.12 // indirect
42-
github.com/mattn/go-runewidth v0.0.13 // indirect
43-
github.com/onsi/gomega v1.36.2 // indirect
30+
github.com/mattn/go-colorable v0.1.14 // indirect
31+
github.com/mattn/go-isatty v0.0.20 // indirect
32+
github.com/mattn/go-runewidth v0.0.16 // indirect
4433
github.com/rivo/uniseg v0.2.0 // indirect
45-
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
46-
github.com/sirupsen/logrus v1.8.1 // indirect
47-
github.com/stretchr/testify v1.8.4 // indirect
48-
github.com/tedsuo/rata v1.0.0 // indirect
49-
github.com/vito/go-interact v1.0.0 // indirect
50-
golang.org/x/crypto v0.36.0 // indirect
51-
golang.org/x/net v0.37.0 // indirect
52-
golang.org/x/sys v0.32.0 // indirect
53-
golang.org/x/term v0.30.0 // indirect
54-
golang.org/x/text v0.23.0 // indirect
55-
google.golang.org/genproto v0.0.0-20250414145226-207652e42e2e // indirect
56-
google.golang.org/genproto/googleapis/api v0.0.0-20250409194420-de1ac958c67a // indirect
57-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250409194420-de1ac958c67a // indirect
58-
google.golang.org/grpc v1.71.0 // indirect
59-
google.golang.org/protobuf v1.36.6 // indirect
34+
github.com/rogpeppe/go-internal v1.13.1 // indirect
35+
github.com/sirupsen/logrus v1.9.3 // indirect
36+
github.com/stretchr/testify v1.10.0 // indirect
37+
github.com/vito/go-interact v0.0.0-20171111012221-fa338ed9e9ec // indirect
38+
golang.org/x/crypto v0.39.0 // indirect
39+
golang.org/x/sys v0.33.0 // indirect
40+
golang.org/x/term v0.32.0 // indirect
41+
golang.org/x/text v0.26.0 // indirect
42+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
6043
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
6144
gopkg.in/yaml.v2 v2.4.0 // indirect
6245
)

0 commit comments

Comments
 (0)