Skip to content

Commit 27e0b9d

Browse files
committed
Try to fix the CI
1 parent bb0476a commit 27e0b9d

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

.golangci.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ linters:
3232
- goconst # Find repeated strings that could be constants
3333
- godox # Find TODO, FIXME, etc. comments
3434
- goprintffuncname # Check printf-style function names
35-
- gosec # Security-focused linter
3635
- misspell # Check for misspellings
3736
- nakedret # Check naked returns
3837
- nilerr # Check nil error returns
3938
- nolintlint # Check nolint directives
4039
- predeclared # Check for shadowed predeclared identifiers
41-
- revive # Fast, configurable, extensible linter
4240
- rowserrcheck # Check SQL rows.Err
4341
- sqlclosecheck # Check SQL Close calls
4442
- unconvert # Check unnecessary type conversions
4543
- wastedassign # Check wasted assignments
4644
- whitespace # Check for extra whitespace
47-
45+
- gocritic
46+
47+
4848
disable:
4949
# Disabled as requested
5050
- gochecknoglobals # Ignore global variables (as requested)
@@ -70,4 +70,5 @@ linters:
7070
- gocyclo # Check cyclomatic complexity
7171
- cyclop # Check cyclomatic complexity
7272
- funlen # Check function length
73-
- gocritic
73+
- gosec # Security-focused linter
74+
- revive # Fast, configurable, extensible linter

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ USAGE:
288288
Get the status of async-profiler on a running Java application
289289

290290
OPTIONS:
291+
-verbose -v, enable verbose output for the plugin
292+
-app-instance-index -i [index], select to which instance of the app to connect
291293
-args -a, Miscellaneous arguments to pass to the command (if supported) in the
292294
container, be aware to end it with a space if it is a simple option. For
293295
commands that create arbitrary files (jcmd, asprof), the environment
@@ -303,8 +305,6 @@ OPTIONS:
303305
defaults to the current directory
304306
-no-download -nd, don't download the heap dump/JFR/... file to local, only keep it in the
305307
container, implies '--keep'
306-
-verbose -v, enable verbose output for the plugin
307-
-app-instance-index -i [index], select to which instance of the app to connect
308308

309309
</pre>
310310

cf_cli_java_plugin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,9 @@ func (c *JavaPlugin) execute(cliConnection plugin.CliConnection, args []string)
803803
return "cf " + strings.Join(cfSSHArguments, " "), nil
804804
}
805805

806-
fullCommand := append(cfSSHArguments, remoteCommand)
807-
c.logVerbose("Executing command: %v", cfSSHArguments)
806+
fullCommand := append([]string{}, cfSSHArguments...)
807+
fullCommand = append(fullCommand, remoteCommand)
808+
c.logVerbose("Executing command: %v", fullCommand)
808809

809810
output, err := cliConnection.CliCommand(fullCommand...)
810811
if err != nil {

scripts/lint-go.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ case "$MODE" in
4848

4949
echo "🔍 Running gofumpt..."
5050
if command -v gofumpt >/dev/null 2>&1; then
51-
if ! gofumpt -l -w *.go cmd/ utils/; then
52-
print_error "Go formatting issues found with gofumpt"
53-
exit 1
51+
# Get only Git-tracked Go files
52+
GO_FILES=$(git ls-files '*.go')
53+
if [ -n "$GO_FILES" ]; then
54+
if ! echo "$GO_FILES" | xargs gofumpt -l -w; then
55+
print_error "Go formatting issues found with gofumpt"
56+
exit 1
57+
fi
58+
print_status "gofumpt formatting check passed on Git-tracked files"
59+
else
60+
print_warning "No Git-tracked Go files found"
5461
fi
55-
print_status "gofumpt formatting check passed"
5662
else
5763
echo "🔍 Running go fmt..."
5864
if ! go fmt ./...; then
@@ -70,6 +76,17 @@ case "$MODE" in
7076
fi
7177
print_status "Go vet check passed"
7278

79+
echo "🔍 Running golangci-lint..."
80+
if command -v golangci-lint >/dev/null 2>&1; then
81+
if (! golangci-lint run --timeout=3m *.go || ! golangci-lint run utils/*.go); then
82+
print_error "golangci-lint issues found"
83+
exit 1
84+
fi
85+
else
86+
print_warning "golangci-lint not found, skipping comprehensive linting"
87+
print_info "Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
88+
fi
89+
7390
print_status "All Go linting checks passed!"
7491
;;
7592

utils/cfutils.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package utils provides utility functions for Cloud Foundry CLI Java plugin operations.
12
package utils
23

34
import (
@@ -22,6 +23,8 @@ type Version struct {
2223
Build int
2324
}
2425

26+
// CFAppEnv represents the environment configuration for a Cloud Foundry application,
27+
// including environment variables, staging configuration, and system services.
2528
type CFAppEnv struct {
2629
EnvironmentVariables struct {
2730
JbpConfigSpringAutoReconfiguration string `json:"JBP_CONFIG_SPRING_AUTO_RECONFIGURATION"`
@@ -78,7 +81,9 @@ type CFAppEnv struct {
7881
func GenerateUUID() string {
7982
// Generate 16 random bytes
8083
bytes := make([]byte, 16)
81-
rand.Read(bytes)
84+
if _, err := rand.Read(bytes); err != nil {
85+
panic(err) // This should never happen with crypto/rand
86+
}
8287

8388
// Set version (4) and variant bits according to RFC 4122
8489
bytes[6] = (bytes[6] & 0x0f) | 0x40 // Version 4
@@ -99,7 +104,7 @@ func readAppEnv(app string) ([]byte, error) {
99104
return nil, err
100105
}
101106

102-
env, err := exec.Command("cf", "curl", fmt.Sprintf("/v3/apps/%s/env", strings.Trim(string(guid[:]), "\n"))).Output()
107+
env, err := exec.Command("cf", "curl", fmt.Sprintf("/v3/apps/%s/env", strings.Trim(string(guid), "\n"))).Output()
103108
if err != nil {
104109
return nil, err
105110
}
@@ -112,20 +117,22 @@ func checkUserPathAvailability(app string, path string) (bool, error) {
112117
return false, err
113118
}
114119

115-
if strings.Contains(string(output[:]), "exists and read-writeable") {
120+
if strings.Contains(string(output), "exists and read-writeable") {
116121
return true, nil
117122
}
118123

119124
return false, nil
120125
}
121126

127+
// FindReasonForAccessError determines the reason why accessing a Cloud Foundry app failed,
128+
// providing helpful diagnostic information and suggestions.
122129
func FindReasonForAccessError(app string) string {
123130
out, err := exec.Command("cf", "apps").Output()
124131
if err != nil {
125132
return "cf is not logged in, please login and try again"
126133
}
127134
// Find all app names
128-
lines := strings.Split(string(out[:]), "\n")
135+
lines := strings.Split(string(out), "\n")
129136
appNames := []string{}
130137
foundHeader := false
131138
for _, line := range lines {
@@ -145,6 +152,8 @@ func FindReasonForAccessError(app string) string {
145152
return "Could not find " + app + ". Did you mean " + matches[0] + "?"
146153
}
147154

155+
// CheckRequiredTools verifies that the necessary tools and permissions are available
156+
// for the specified Cloud Foundry application, including SSH access.
148157
func CheckRequiredTools(app string) (bool, error) {
149158
guid, err := exec.Command("cf", "app", app, "--guid").Output()
150159
if err != nil {
@@ -155,7 +164,7 @@ func CheckRequiredTools(app string) (bool, error) {
155164
return false, err
156165
}
157166
var result map[string]any
158-
if err := json.Unmarshal([]byte(output), &result); err != nil {
167+
if err := json.Unmarshal(output, &result); err != nil {
159168
return false, err
160169
}
161170

@@ -166,6 +175,8 @@ func CheckRequiredTools(app string) (bool, error) {
166175
return true, nil
167176
}
168177

178+
// GetAvailablePath determines the best available path for operations on the Cloud Foundry app,
179+
// preferring user-specified paths and falling back to volume mounts or /tmp.
169180
func GetAvailablePath(data string, userpath string) (string, error) {
170181
if len(userpath) > 0 {
171182
valid, _ := checkUserPathAvailability(data, userpath)
@@ -178,7 +189,7 @@ func GetAvailablePath(data string, userpath string) (string, error) {
178189

179190
env, err := readAppEnv(data)
180191
if err != nil {
181-
return "/tmp", nil
192+
return "/tmp", err
182193
}
183194

184195
var cfAppEnv CFAppEnv
@@ -197,12 +208,18 @@ func GetAvailablePath(data string, userpath string) (string, error) {
197208
return "/tmp", nil
198209
}
199210

211+
// CopyOverCat copies a remote file to a local destination using the cf ssh command and cat.
200212
func CopyOverCat(args []string, src string, dest string) error {
201-
f, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
213+
f, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
202214
if err != nil {
203215
return errors.New("Error creating local file at " + dest + ". Please check that you are allowed to create files at the given local path.")
204216
}
205-
defer f.Close()
217+
defer func() {
218+
if closeErr := f.Close(); closeErr != nil {
219+
// Log the error, but don't override the main function's error
220+
fmt.Fprintf(os.Stderr, "Warning: failed to close file %s: %v\n", dest, closeErr)
221+
}
222+
}()
206223

207224
args = append(args, "cat "+src)
208225
cat := exec.Command("cf", args...)
@@ -222,6 +239,7 @@ func CopyOverCat(args []string, src string, dest string) error {
222239
return nil
223240
}
224241

242+
// DeleteRemoteFile removes a file from the remote Cloud Foundry application container.
225243
func DeleteRemoteFile(args []string, path string) error {
226244
args = append(args, "rm -fr "+path)
227245
_, err := exec.Command("cf", args...).Output()
@@ -232,14 +250,18 @@ func DeleteRemoteFile(args []string, path string) error {
232250
return nil
233251
}
234252

253+
// FindHeapDumpFile locates heap dump files (*.hprof) in the specified path on the remote container.
235254
func FindHeapDumpFile(args []string, fullpath string, fspath string) (string, error) {
236255
return FindFile(args, fullpath, fspath, "*.hprof")
237256
}
238257

258+
// FindJFRFile locates Java Flight Recorder files (*.jfr) in the specified path on the remote container.
239259
func FindJFRFile(args []string, fullpath string, fspath string) (string, error) {
240260
return FindFile(args, fullpath, fspath, "*.jfr")
241261
}
242262

263+
// FindFile searches for files matching the given pattern in the remote container,
264+
// returning the most recently modified file that matches.
243265
func FindFile(args []string, fullpath string, fspath string, pattern string) (string, error) {
244266
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 "
245267

@@ -256,17 +278,18 @@ func FindFile(args []string, fullpath string, fspath string, pattern string) (st
256278
return "", errors.New("error while checking the generated file: " + errorStr)
257279
}
258280

259-
return strings.Trim(string(output[:]), "\n"), nil
281+
return strings.Trim(string(output), "\n"), nil
260282
}
261283

284+
// ListFiles retrieves a list of files in the specified directory on the remote container.
262285
func ListFiles(args []string, path string) ([]string, error) {
263286
cmd := "ls " + path
264287
args = append(args, cmd)
265288
output, err := exec.Command("cf", args...).Output()
266289
if err != nil {
267-
return nil, errors.New("error occurred while listing files: " + string(output[:]))
290+
return nil, errors.New("error occurred while listing files: " + string(output))
268291
}
269-
files := strings.Split(strings.Trim(string(output[:]), "\n"), "\n")
292+
files := strings.Split(strings.Trim(string(output), "\n"), "\n")
270293
// Filter all empty strings
271294
j := 0
272295
for _, s := range files {
@@ -278,9 +301,9 @@ func ListFiles(args []string, path string) ([]string, error) {
278301
return files[:j], nil
279302
}
280303

281-
// FuzzySearch returns up to `max` words from `words` that are closest in
304+
// FuzzySearch returns up to `maxResults` words from `words` that are closest in
282305
// Levenshtein distance to `needle`.
283-
func FuzzySearch(needle string, words []string, max int) []string {
306+
func FuzzySearch(needle string, words []string, maxResults int) []string {
284307
type match struct {
285308
distance int
286309
word string
@@ -298,12 +321,12 @@ func FuzzySearch(needle string, words []string, max int) []string {
298321
return matches[i].distance < matches[j].distance
299322
})
300323

301-
if max > len(matches) {
302-
max = len(matches)
324+
if maxResults > len(matches) {
325+
maxResults = len(matches)
303326
}
304327

305-
results := make([]string, 0, max)
306-
for i := range max {
328+
results := make([]string, 0, maxResults)
329+
for i := range maxResults {
307330
results = append(results, matches[i].word)
308331
}
309332

0 commit comments

Comments
 (0)