Skip to content

Commit cea71f3

Browse files
committed
Improve verbose printing
1 parent 248e83d commit cea71f3

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

cf_cli_java_plugin.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ fi`,
312312
Description: "Check the running Java Flight Recorder recording on a running Java application",
313313
RequiredTools: []string{"jcmd"},
314314
GenerateFiles: false,
315-
SshCommand: FilterJCMDRemoteMessage + `$JCMD_COMMAND $(pidof java) JFR.check | filter_jcmd_remote_message`,
315+
SshCommand: FilterJCMDRemoteMessage + `$JCMD_COMMAND $(pidof java) JFR.check | filter_jcmd_remote_message`,
316316
},
317317
{
318318
Name: "vm-version",
@@ -457,32 +457,32 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
457457

458458
verbose := commandFlags.IsSet("verbose")
459459

460-
// Helper function for verbose logging
461-
logVerbose := func(message string) {
460+
// Helper function for verbose logging with format strings
461+
logVerbose := func(format string, args ...interface{}) {
462462
if verbose {
463-
fmt.Printf("[VERBOSE] %s\n", message)
463+
fmt.Printf("[VERBOSE] "+format+"\n", args...)
464464
}
465465
}
466466

467467
logVerbose("Starting command execution")
468-
logVerbose(fmt.Sprintf("Command arguments: %v", args))
468+
logVerbose("Command arguments: %v", args)
469469

470470
applicationInstance := commandFlags.Int("app-instance-index")
471471
noDownload := commandFlags.IsSet("no-download")
472472
keepAfterDownload := commandFlags.IsSet("keep") || noDownload
473473

474-
logVerbose(fmt.Sprintf("Application instance: %d", applicationInstance))
475-
logVerbose(fmt.Sprintf("No download: %t", noDownload))
476-
logVerbose(fmt.Sprintf("Keep after download: %t", keepAfterDownload))
474+
logVerbose("Application instance: %d", applicationInstance)
475+
logVerbose("No download: %t", noDownload)
476+
logVerbose("Keep after download: %t", keepAfterDownload)
477477

478478
remoteDir := commandFlags.String("container-dir")
479479
localDir := commandFlags.String("local-dir")
480480
if localDir == "" {
481481
localDir = "."
482482
}
483483

484-
logVerbose(fmt.Sprintf("Remote directory: %s", remoteDir))
485-
logVerbose(fmt.Sprintf("Local directory: %s", localDir))
484+
logVerbose("Remote directory: %s", remoteDir)
485+
logVerbose("Local directory: %s", localDir)
486486

487487
arguments := commandFlags.Args()
488488
argumentLen := len(arguments)
@@ -492,7 +492,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
492492
}
493493

494494
commandName := arguments[0]
495-
logVerbose(fmt.Sprintf("Command name: %s", commandName))
495+
logVerbose("Command name: %s", commandName)
496496

497497
index := -1
498498
for i, command := range commands {
@@ -511,12 +511,12 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
511511
}
512512

513513
command := commands[index]
514-
logVerbose(fmt.Sprintf("Found command: %s - %s", command.Name, command.Description))
514+
logVerbose("Found command: %s - %s", command.Name, command.Description)
515515
if !command.GenerateFiles && !command.GenerateArbitraryFiles {
516516
logVerbose("Command does not generate files, checking for invalid file flags")
517517
for _, flag := range fileFlags {
518518
if commandFlags.IsSet(flag) {
519-
logVerbose(fmt.Sprintf("Invalid flag %q detected for command %s", flag, command.Name))
519+
logVerbose("Invalid flag %q detected for command %s", flag, command.Name)
520520
return "", &InvalidUsageError{message: fmt.Sprintf("The flag %q is not supported for %s", flag, command.Name)}
521521
}
522522
}
@@ -534,7 +534,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
534534
}
535535
}
536536
if !command.HasMiscArgs() && commandFlags.IsSet("args") {
537-
logVerbose(fmt.Sprintf("Command %s does not support --args flag", command.Name))
537+
logVerbose("Command %s does not support --args flag", command.Name)
538538
return "", &InvalidUsageError{message: fmt.Sprintf("The flag %q is not supported for %s", "args", command.Name)}
539539
}
540540
if argumentLen == 1 {
@@ -544,14 +544,14 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
544544
}
545545

546546
applicationName := arguments[1]
547-
logVerbose(fmt.Sprintf("Application name: %s", applicationName))
547+
logVerbose("Application name: %s", applicationName)
548548

549549
cfSSHArguments := []string{"ssh", applicationName}
550550
if applicationInstance > 0 {
551551
cfSSHArguments = append(cfSSHArguments, "--app-instance-index", strconv.Itoa(applicationInstance))
552552
}
553553

554-
logVerbose(fmt.Sprintf("CF SSH arguments: %v", cfSSHArguments))
554+
logVerbose("CF SSH arguments: %v", cfSSHArguments)
555555

556556
supported, err := util.CheckRequiredTools(applicationName)
557557

@@ -564,10 +564,10 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
564564
var remoteCommandTokens = []string{JavaDetectionCommand}
565565

566566
logVerbose("Building remote command tokens")
567-
logVerbose(fmt.Sprintf("Java detection command: %s", JavaDetectionCommand))
567+
logVerbose("Java detection command: %s", JavaDetectionCommand)
568568

569569
for _, requiredTool := range command.RequiredTools {
570-
logVerbose(fmt.Sprintf("Setting up required tool: %s", requiredTool))
570+
logVerbose("Setting up required tool: %s", requiredTool)
571571
uppercase := strings.ToUpper(requiredTool)
572572
var toolCommand = fmt.Sprintf("%s_COMMAND=$(realpath $(find -executable -name %s | head -1 | tr -d [:space:])); if [ -z \"${%s_COMMAND}\" ]; then echo \"%s not found\"; exit 1; fi", uppercase, requiredTool, uppercase, requiredTool)
573573
if requiredTool == "jcmd" {
@@ -576,7 +576,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
576576
logVerbose("Added jcmd with asprof fallback")
577577
} else {
578578
remoteCommandTokens = append(remoteCommandTokens, toolCommand)
579-
logVerbose(fmt.Sprintf("Added tool command for %s", requiredTool))
579+
logVerbose("Added tool command for %s", requiredTool)
580580
}
581581
}
582582
fileName := ""
@@ -593,21 +593,21 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
593593
if err != nil {
594594
return "", err
595595
}
596-
logVerbose(fmt.Sprintf("Available path: %s", fspath))
596+
logVerbose("Available path: %s", fspath)
597597
if command.GenerateArbitraryFiles {
598598
fspath = fspath + "/" + command.GenerateArbitraryFilesFolderName
599-
logVerbose(fmt.Sprintf("Updated path for arbitrary files: %s", fspath))
599+
logVerbose("Updated path for arbitrary files: %s", fspath)
600600
}
601601

602602
fileName = fspath + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
603-
logVerbose(fmt.Sprintf("Generated filename: %s", fileName))
603+
logVerbose("Generated filename: %s", fileName)
604604
replacements["$$FILE_NAME"] = fileName
605605
replacements["$$FSPATH"] = fspath
606606
if command.GenerateArbitraryFiles {
607607
// prepend 'mkdir -p $$FSPATH' to the command to create the directory if it does not exist
608608
remoteCommandTokens = append([]string{"mkdir -p " + fspath}, remoteCommandTokens...)
609609
remoteCommandTokens = append(remoteCommandTokens, "cd "+fspath)
610-
logVerbose(fmt.Sprintf("Added directory creation and navigation commands for: %s", fspath))
610+
logVerbose("Added directory creation and navigation commands for: %s", fspath)
611611
}
612612
}
613613

@@ -617,13 +617,13 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
617617
}
618618
remoteCommandTokens = append(remoteCommandTokens, commandText)
619619

620-
logVerbose(fmt.Sprintf("Command text after replacements: %s", commandText))
621-
logVerbose(fmt.Sprintf("Full remote command tokens: %v", remoteCommandTokens))
620+
logVerbose("Command text after replacements: %s", commandText)
621+
logVerbose("Full remote command tokens: %v", remoteCommandTokens)
622622

623623
cfSSHArguments = append(cfSSHArguments, "--command")
624624
remoteCommand := strings.Join(remoteCommandTokens, "; ")
625625

626-
logVerbose(fmt.Sprintf("Final remote command: %s", remoteCommand))
626+
logVerbose("Final remote command: %s", remoteCommand)
627627

628628
if commandFlags.IsSet("dry-run") {
629629
logVerbose("Dry-run mode enabled, returning command without execution")
@@ -634,7 +634,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
634634
}
635635

636636
fullCommand := append(cfSSHArguments, remoteCommand)
637-
logVerbose(fmt.Sprintf("Executing command: %v", fullCommand))
637+
logVerbose("Executing command: %v", fullCommand)
638638

639639
output, err := commandExecutor.Execute(fullCommand)
640640
logVerbose("Command execution completed")
@@ -656,30 +656,30 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
656656
}
657657
if err == nil && finalFile != "" {
658658
fileName = finalFile
659-
logVerbose(fmt.Sprintf("Found file: %s", finalFile))
659+
logVerbose("Found file: %s", finalFile)
660660
fmt.Println("Successfully created " + command.FileLabel + " in application container at: " + fileName)
661661
} else {
662-
logVerbose(fmt.Sprintf("Failed to find file, error: %v", err))
662+
logVerbose("Failed to find file, error: %v", err)
663663
fmt.Println("Failed to find " + command.FileLabel + " in application container")
664664
return "", err
665665
}
666666

667667
localFileFullPath := localDir + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
668-
logVerbose(fmt.Sprintf("Downloading file to: %s", localFileFullPath))
668+
logVerbose("Downloading file to: %s", localFileFullPath)
669669
err = util.CopyOverCat(cfSSHArguments, fileName, localFileFullPath)
670670
if err == nil {
671671
logVerbose("File download completed successfully")
672672
fmt.Println(toSentenceCase(command.FileLabel) + " file saved to: " + localFileFullPath)
673673
} else {
674-
logVerbose(fmt.Sprintf("File download failed: %v", err))
674+
logVerbose("File download failed: %v", err)
675675
return "", err
676676
}
677677

678678
if !keepAfterDownload {
679679
logVerbose("Deleting remote file")
680680
err = util.DeleteRemoteFile(cfSSHArguments, fileName)
681681
if err != nil {
682-
logVerbose(fmt.Sprintf("Failed to delete remote file: %v", err))
682+
logVerbose("Failed to delete remote file: %v", err)
683683
return "", err
684684
}
685685
logVerbose("Remote file deleted successfully")
@@ -689,28 +689,28 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
689689
}
690690
}
691691
if command.GenerateArbitraryFiles && !noDownload {
692-
logVerbose(fmt.Sprintf("Processing arbitrary files download: %s", fspath))
693-
logVerbose(fmt.Sprintf("cfSSHArguments: %v", cfSSHArguments))
692+
logVerbose("Processing arbitrary files download: %s", fspath)
693+
logVerbose("cfSSHArguments: %v", cfSSHArguments)
694694
// download all files in the generic folder
695695
files, err := util.ListFiles(cfSSHArguments, fspath)
696696
for i, file := range files {
697-
logVerbose(fmt.Sprintf("File %d: %s", i+1, file))
697+
logVerbose("File %d: %s", i+1, file)
698698
}
699699
if err != nil {
700-
logVerbose(fmt.Sprintf("Failed to list files: %v", err))
700+
logVerbose("Failed to list files: %v", err)
701701
return "", err
702702
}
703-
logVerbose(fmt.Sprintf("Found %d files to download", len(files)))
703+
logVerbose("Found %d files to download", len(files))
704704
if len(files) != 0 {
705705
for _, file := range files {
706-
logVerbose(fmt.Sprintf("Downloading file: %s", file))
706+
logVerbose("Downloading file: %s", file)
707707
localFileFullPath := localDir + "/" + file
708708
err = util.CopyOverCat(cfSSHArguments, fspath+"/"+file, localFileFullPath)
709709
if err == nil {
710-
logVerbose(fmt.Sprintf("File %s downloaded successfully", file))
710+
logVerbose("File %s downloaded successfully", file)
711711
fmt.Printf("File %s saved to: %s\n", file, localFileFullPath)
712712
} else {
713-
logVerbose(fmt.Sprintf("Failed to download file %s: %v", file, err))
713+
logVerbose("Failed to download file %s: %v", file, err)
714714
return "", err
715715
}
716716
}
@@ -719,7 +719,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
719719
logVerbose("Deleting remote file folder")
720720
err = util.DeleteRemoteFile(cfSSHArguments, fspath)
721721
if err != nil {
722-
logVerbose(fmt.Sprintf("Failed to delete remote folder: %v", err))
722+
logVerbose("Failed to delete remote folder: %v", err)
723723
return "", err
724724
}
725725
logVerbose("Remote folder deleted successfully")

0 commit comments

Comments
 (0)