Skip to content

Commit ecdf0e8

Browse files
committed
remove useless struct & interface
1 parent 4807ac3 commit ecdf0e8

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

cf_cli_java_plugin.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (c *JavaPlugin) Run(cliConnection plugin.CliConnection, args []string) {
102102
fmt.Printf("[VERBOSE] Run called with args: %v\n", args)
103103
}
104104

105-
_, err := c.DoRun(&commandExecutorImpl{cliConnection: cliConnection}, &uuidGeneratorImpl{}, utils.CfJavaPluginUtilImpl{}, args)
105+
_, err := c.DoRun(&commandExecutorImpl{cliConnection: cliConnection}, &uuidGeneratorImpl{}, args)
106106
if err != nil {
107107
if c.verbose {
108108
fmt.Printf("[VERBOSE] Error occurred: %v\n", err)
@@ -115,15 +115,15 @@ func (c *JavaPlugin) Run(cliConnection plugin.CliConnection, args []string) {
115115
}
116116

117117
// DoRun is an internal method that we use to wrap the cmd package with CommandExecutor for test purposes
118-
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) {
119119
traceLogger := trace.NewLogger(os.Stdout, true, os.Getenv("CF_TRACE"), "")
120120
ui := terminal.NewUI(os.Stdin, os.Stdout, terminal.NewTeePrinter(os.Stdout), traceLogger)
121121

122122
if c.verbose {
123123
fmt.Printf("[VERBOSE] DoRun called with args: %v\n", args)
124124
}
125125

126-
output, err := c.execute(commandExecutor, uuidGenerator, util, args)
126+
output, err := c.execute(commandExecutor, uuidGenerator, args)
127127
if err != nil {
128128
if err.Error() == "unexpected EOF" {
129129
return output, err
@@ -486,7 +486,7 @@ func toSentenceCase(input string) string {
486486
return strings.ToUpper(string(input[0])) + strings.ToLower(input[1:])
487487
}
488488

489-
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) {
490490
if len(args) == 0 {
491491
return "", &InvalidUsageError{message: "No command provided"}
492492
}
@@ -632,7 +632,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
632632

633633
logVerbose("CF SSH arguments: %v", cfSSHArguments)
634634

635-
supported, err := util.CheckRequiredTools(applicationName)
635+
supported, err := utils.CheckRequiredTools(applicationName)
636636

637637
if err != nil || !supported {
638638
return "required tools checking failed", err
@@ -665,7 +665,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
665665
// Initialize fspath and fileName for commands that need them
666666
if command.GenerateFiles || command.NeedsFileName || command.GenerateArbitraryFiles {
667667
logVerbose("Command requires file generation")
668-
fspath, err = util.GetAvailablePath(applicationName, remoteDir)
668+
fspath, err = utils.GetAvailablePath(applicationName, remoteDir)
669669
if err != nil {
670670
return "", fmt.Errorf("failed to get available path: %w", err)
671671
}
@@ -740,10 +740,10 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
740740
switch command.FileExtension {
741741
case ".hprof":
742742
logVerbose("Finding heap dump file")
743-
finalFile, err = util.FindHeapDumpFile(cfSSHArguments, fileName, fspath)
743+
finalFile, err = utils.FindHeapDumpFile(cfSSHArguments, fileName, fspath)
744744
case ".jfr":
745745
logVerbose("Finding JFR file")
746-
finalFile, err = util.FindJFRFile(cfSSHArguments, fileName, fspath)
746+
finalFile, err = utils.FindJFRFile(cfSSHArguments, fileName, fspath)
747747
default:
748748
return "", &InvalidUsageError{message: fmt.Sprintf("Unsupported file extension %q", command.FileExtension)}
749749
}
@@ -759,7 +759,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
759759

760760
localFileFullPath := localDir + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
761761
logVerbose("Downloading file to: %s", localFileFullPath)
762-
err = util.CopyOverCat(cfSSHArguments, fileName, localFileFullPath)
762+
err = utils.CopyOverCat(cfSSHArguments, fileName, localFileFullPath)
763763
if err == nil {
764764
logVerbose("File download completed successfully")
765765
fmt.Println(toSentenceCase(command.FileLabel) + " file saved to: " + localFileFullPath)
@@ -770,7 +770,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
770770

771771
if !keepAfterDownload {
772772
logVerbose("Deleting remote file")
773-
err = util.DeleteRemoteFile(cfSSHArguments, fileName)
773+
err = utils.DeleteRemoteFile(cfSSHArguments, fileName)
774774
if err != nil {
775775
logVerbose("Failed to delete remote file: %v", err)
776776
return "", err
@@ -785,7 +785,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
785785
logVerbose("Processing arbitrary files download: %s", fspath)
786786
logVerbose("cfSSHArguments: %v", cfSSHArguments)
787787
// download all files in the generic folder
788-
files, err := util.ListFiles(cfSSHArguments, fspath)
788+
files, err := utils.ListFiles(cfSSHArguments, fspath)
789789
for i, file := range files {
790790
logVerbose("File %d: %s", i+1, file)
791791
}
@@ -798,7 +798,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
798798
for _, file := range files {
799799
logVerbose("Downloading file: %s", file)
800800
localFileFullPath := localDir + "/" + file
801-
err = util.CopyOverCat(cfSSHArguments, fspath+"/"+file, localFileFullPath)
801+
err = utils.CopyOverCat(cfSSHArguments, fspath+"/"+file, localFileFullPath)
802802
if err == nil {
803803
logVerbose("File %s downloaded successfully", file)
804804
fmt.Printf("File %s saved to: %s\n", file, localFileFullPath)
@@ -810,7 +810,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
810810

811811
if !keepAfterDownload {
812812
logVerbose("Deleting remote file folder")
813-
err = util.DeleteRemoteFile(cfSSHArguments, fspath)
813+
err = utils.DeleteRemoteFile(cfSSHArguments, fspath)
814814
if err != nil {
815815
logVerbose("Failed to delete remote folder: %v", err)
816816
return "", err
@@ -897,7 +897,7 @@ func (c *JavaPlugin) GetMetadata() plugin.PluginMetadata {
897897
}
898898
}
899899

900-
// 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
901901
// commands provided in your plugin. Main will be used to initialize the plugin
902902
// process, as well as any dependencies you might require for your
903903
// plugin.

utils/cf_java_plugin_util.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import (
66
"strings"
77
)
88

9-
type CfJavaPluginUtil interface {
10-
FindReasonForAccessError(app string) string
11-
CheckRequiredTools(app string) (bool, error)
12-
GetAvailablePath(data string, userpath string) (string, error)
13-
CopyOverCat(args []string, src string, dest string) error
14-
DeleteRemoteFile(args []string, path string) error
15-
FindHeapDumpFile(args []string, fullpath string, fspath string) (string, error)
16-
FindJFRFile(args []string, fullpath string, fspath string) (string, error)
17-
FindFile(args []string, fullpath string, fspath string, pattern string) (string, error)
18-
ListFiles(args []string, path string) ([]string, error)
19-
}
20-
219
// FuzzySearch returns up to `max` words from `words` that are closest in
2210
// Levenshtein distance to `needle`.
2311
func FuzzySearch(needle string, words []string, max int) []string {

utils/cfutils.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111
)
1212

13-
type CfJavaPluginUtilImpl struct{}
14-
1513
type CFAppEnv struct {
1614
EnvironmentVariables struct {
1715
JbpConfigSpringAutoReconfiguration string `json:"JBP_CONFIG_SPRING_AUTO_RECONFIGURATION"`
@@ -90,14 +88,14 @@ func checkUserPathAvailability(app string, path string) (bool, error) {
9088
return false, nil
9189
}
9290

93-
func (checker CfJavaPluginUtilImpl) FindReasonForAccessError(app string) string {
91+
func FindReasonForAccessError(app string) string {
9492
out, err := exec.Command("cf", "apps").Output()
9593
if err != nil {
9694
return "cf is not logged in, please login and try again"
9795
}
9896
// find all app names
9997
lines := strings.Split(string(out[:]), "\n")
100-
appNames := make([]string, 100)
98+
appNames := []string{}
10199
foundHeader := false
102100
for _, line := range lines {
103101
if foundHeader && len(line) > 0 {
@@ -116,17 +114,19 @@ func (checker CfJavaPluginUtilImpl) FindReasonForAccessError(app string) string
116114
return "Could not find " + app + ". Did you mean " + matches[0] + "?"
117115
}
118116

119-
func (checker CfJavaPluginUtilImpl) CheckRequiredTools(app string) (bool, error) {
117+
func CheckRequiredTools(app string) (bool, error) {
120118
guid, err := exec.Command("cf", "app", app, "--guid").Output()
121119
if err != nil {
122-
return false, errors.New(checker.FindReasonForAccessError(app))
120+
return false, errors.New(FindReasonForAccessError(app))
123121
}
124122
output, err := exec.Command("cf", "curl", "/v3/apps/"+strings.TrimSuffix(string(guid), "\n")+"/ssh_enabled").Output()
125123
if err != nil {
126124
return false, err
127125
}
128126
var result map[string]any
129-
json.Unmarshal([]byte(output), &result)
127+
if err := json.Unmarshal([]byte(output), &result); err != nil {
128+
return false, err
129+
}
130130

131131
if enabled, ok := result["enabled"].(bool); !ok || !enabled {
132132
return false, errors.New("ssh is not enabled for app: '" + app + "', please run below 2 shell commands to enable ssh and try again(please note application should be restarted before take effect):\ncf enable-ssh " + app + "\ncf restart " + app)
@@ -135,7 +135,7 @@ func (checker CfJavaPluginUtilImpl) CheckRequiredTools(app string) (bool, error)
135135
return true, nil
136136
}
137137

138-
func (checker CfJavaPluginUtilImpl) GetAvailablePath(data string, userpath string) (string, error) {
138+
func GetAvailablePath(data string, userpath string) (string, error) {
139139
if len(userpath) > 0 {
140140
valid, _ := checkUserPathAvailability(data, userpath)
141141
if valid {
@@ -151,7 +151,9 @@ func (checker CfJavaPluginUtilImpl) GetAvailablePath(data string, userpath strin
151151
}
152152

153153
var cfAppEnv CFAppEnv
154-
json.Unmarshal(env, &cfAppEnv)
154+
if err := json.Unmarshal(env, &cfAppEnv); err != nil {
155+
return "", err
156+
}
155157

156158
for _, v := range cfAppEnv.SystemEnvJSON.VcapServices.FsStorage {
157159
for _, v2 := range v.VolumeMounts {
@@ -164,7 +166,7 @@ func (checker CfJavaPluginUtilImpl) GetAvailablePath(data string, userpath strin
164166
return "/tmp", nil
165167
}
166168

167-
func (checker CfJavaPluginUtilImpl) CopyOverCat(args []string, src string, dest string) error {
169+
func CopyOverCat(args []string, src string, dest string) error {
168170
f, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
169171
if err != nil {
170172
return errors.New("Error creating local file at " + dest + ". Please check that you are allowed to create files at the given local path.")
@@ -189,7 +191,7 @@ func (checker CfJavaPluginUtilImpl) CopyOverCat(args []string, src string, dest
189191
return nil
190192
}
191193

192-
func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(args []string, path string) error {
194+
func DeleteRemoteFile(args []string, path string) error {
193195
args = append(args, "rm -fr "+path)
194196
_, err := exec.Command("cf", args...).Output()
195197
if err != nil {
@@ -199,15 +201,15 @@ func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(args []string, path string)
199201
return nil
200202
}
201203

202-
func (checker CfJavaPluginUtilImpl) FindHeapDumpFile(args []string, fullpath string, fspath string) (string, error) {
203-
return checker.FindFile(args, fullpath, fspath, "java_pid*.hprof")
204+
func FindHeapDumpFile(args []string, fullpath string, fspath string) (string, error) {
205+
return FindFile(args, fullpath, fspath, "java_pid*.hprof")
204206
}
205207

206-
func (checker CfJavaPluginUtilImpl) FindJFRFile(args []string, fullpath string, fspath string) (string, error) {
207-
return checker.FindFile(args, fullpath, fspath, "*.jfr")
208+
func FindJFRFile(args []string, fullpath string, fspath string) (string, error) {
209+
return FindFile(args, fullpath, fspath, "*.jfr")
208210
}
209211

210-
func (checker CfJavaPluginUtilImpl) FindFile(args []string, fullpath string, fspath string, pattern string) (string, error) {
212+
func FindFile(args []string, fullpath string, fspath string, pattern string) (string, error) {
211213
cmd := " [ -f '" + fullpath + "' ] && echo '" + fullpath + "' || find " + fspath + " -name '" + pattern + "' -printf '%T@ %p\\0' | sort -zk 1nr | sed -z 's/^[^ ]* //' | tr '\\0' '\\n' | head -n 1 "
212214

213215
args = append(args, cmd)
@@ -219,7 +221,7 @@ func (checker CfJavaPluginUtilImpl) FindFile(args []string, fullpath string, fsp
219221
return strings.Trim(string(output[:]), "\n"), nil
220222
}
221223

222-
func (checker CfJavaPluginUtilImpl) ListFiles(args []string, path string) ([]string, error) {
224+
func ListFiles(args []string, path string) ([]string, error) {
223225
cmd := "ls " + path
224226
args = append(args, cmd)
225227
output, err := exec.Command("cf", args...).Output()

0 commit comments

Comments
 (0)