Skip to content

Commit 57ca2cf

Browse files
committed
make code more idiomatic
1 parent 561bd2c commit 57ca2cf

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

cf_cli_java_plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
539539
verbose := commandFlags.IsSet("verbose")
540540

541541
// Helper function for verbose logging with format strings
542-
logVerbose := func(format string, args ...interface{}) {
542+
logVerbose := func(format string, args ...any) {
543543
if verbose {
544544
fmt.Printf("[VERBOSE] "+format+"\n", args...)
545545
}

utils/cf_java_plugin_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func FuzzySearch(needle string, words []string, max int) []string {
4343
}
4444

4545
results := make([]string, 0, max)
46-
for i := 0; i < max; i++ {
46+
for i := range max {
4747
results = append(results, matches[i].word)
4848
}
4949

utils/cfutils.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,32 @@ import (
1010
"strings"
1111
)
1212

13-
type CfJavaPluginUtilImpl struct {
14-
}
13+
type CfJavaPluginUtilImpl struct{}
1514

1615
type CFAppEnv struct {
1716
EnvironmentVariables struct {
1817
JbpConfigSpringAutoReconfiguration string `json:"JBP_CONFIG_SPRING_AUTO_RECONFIGURATION"`
1918
JbpConfigOpenJdkJre string `json:"JBP_CONFIG_OPEN_JDK_JRE"`
2019
JbpConfigComponents string `json:"JBP_CONFIG_COMPONENTS"`
2120
} `json:"environment_variables"`
22-
StagingEnvJSON struct {
23-
} `json:"staging_env_json"`
21+
StagingEnvJSON struct{} `json:"staging_env_json"`
2422
RunningEnvJSON struct {
2523
CredhubAPI string `json:"CREDHUB_API"`
2624
} `json:"running_env_json"`
2725
SystemEnvJSON struct {
2826
VcapServices struct {
2927
FsStorage []struct {
30-
Label string `json:"label"`
31-
Provider interface{} `json:"provider"`
32-
Plan string `json:"plan"`
33-
Name string `json:"name"`
34-
Tags []string `json:"tags"`
35-
InstanceGUID string `json:"instance_guid"`
36-
InstanceName string `json:"instance_name"`
37-
BindingGUID string `json:"binding_guid"`
38-
BindingName interface{} `json:"binding_name"`
39-
Credentials struct {
40-
} `json:"credentials"`
41-
SyslogDrainURL interface{} `json:"syslog_drain_url"`
28+
Label string `json:"label"`
29+
Provider any `json:"provider"`
30+
Plan string `json:"plan"`
31+
Name string `json:"name"`
32+
Tags []string `json:"tags"`
33+
InstanceGUID string `json:"instance_guid"`
34+
InstanceName string `json:"instance_name"`
35+
BindingGUID string `json:"binding_guid"`
36+
BindingName any `json:"binding_name"`
37+
Credentials struct{} `json:"credentials"`
38+
SyslogDrainURL any `json:"syslog_drain_url"`
4239
VolumeMounts []struct {
4340
ContainerDir string `json:"container_dir"`
4441
Mode string `json:"mode"`
@@ -53,16 +50,16 @@ type CFAppEnv struct {
5350
Limits struct {
5451
Fds int `json:"fds"`
5552
} `json:"limits"`
56-
ApplicationName string `json:"application_name"`
57-
ApplicationUris []string `json:"application_uris"`
58-
Name string `json:"name"`
59-
SpaceName string `json:"space_name"`
60-
SpaceID string `json:"space_id"`
61-
OrganizationID string `json:"organization_id"`
62-
OrganizationName string `json:"organization_name"`
63-
Uris []string `json:"uris"`
64-
Users interface{} `json:"users"`
65-
ApplicationID string `json:"application_id"`
53+
ApplicationName string `json:"application_name"`
54+
ApplicationUris []string `json:"application_uris"`
55+
Name string `json:"name"`
56+
SpaceName string `json:"space_name"`
57+
SpaceID string `json:"space_id"`
58+
OrganizationID string `json:"organization_id"`
59+
OrganizationName string `json:"organization_name"`
60+
Uris []string `json:"uris"`
61+
Users any `json:"users"`
62+
ApplicationID string `json:"application_id"`
6663
} `json:"VCAP_APPLICATION"`
6764
} `json:"application_env_json"`
6865
}
@@ -78,7 +75,6 @@ func readAppEnv(app string) ([]byte, error) {
7875
return nil, err
7976
}
8077
return env, nil
81-
8278
}
8379

8480
func checkUserPathAvailability(app string, path string) (bool, error) {
@@ -119,6 +115,7 @@ func (checker CfJavaPluginUtilImpl) FindReasonForAccessError(app string) string
119115
matches := FuzzySearch(app, appNames, 1)
120116
return "Could not find " + app + ". Did you mean " + matches[0] + "?"
121117
}
118+
122119
func (checker CfJavaPluginUtilImpl) CheckRequiredTools(app string) (bool, error) {
123120
guid, err := exec.Command("cf", "app", app, "--guid").Output()
124121
if err != nil {
@@ -128,13 +125,13 @@ func (checker CfJavaPluginUtilImpl) CheckRequiredTools(app string) (bool, error)
128125
if err != nil {
129126
return false, err
130127
}
131-
var result map[string]interface{}
128+
var result map[string]any
132129
json.Unmarshal([]byte(output), &result)
133130

134131
if enabled, ok := result["enabled"].(bool); !ok || !enabled {
135132
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)
136133
}
137-
134+
138135
return true, nil
139136
}
140137

@@ -195,10 +192,8 @@ func (checker CfJavaPluginUtilImpl) CopyOverCat(args []string, src string, dest
195192
func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(args []string, path string) error {
196193
args = append(args, "rm -fr "+path)
197194
_, err := exec.Command("cf", args...).Output()
198-
199195
if err != nil {
200196
return errors.New("error occured while removing dump file generated")
201-
202197
}
203198

204199
return nil
@@ -217,30 +212,28 @@ func (checker CfJavaPluginUtilImpl) FindFile(args []string, fullpath string, fsp
217212

218213
args = append(args, cmd)
219214
output, err := exec.Command("cf", args...).Output()
220-
221215
if err != nil {
222216
return "", errors.New("error while checking the generated file")
223217
}
224218

225219
return strings.Trim(string(output[:]), "\n"), nil
226-
227220
}
228221

229222
func (checker CfJavaPluginUtilImpl) ListFiles(args []string, path string) ([]string, error) {
230223
cmd := "ls " + path
231224
args = append(args, cmd)
232225
output, err := exec.Command("cf", args...).Output()
233-
234226
if err != nil {
235227
return nil, errors.New("error occured while listing files: " + string(output[:]))
236228
}
237229
files := strings.Split(strings.Trim(string(output[:]), "\n"), "\n")
238230
// filter all empty strings
239-
for i := 0; i < len(files); i++ {
240-
if len(files[i]) == 0 {
241-
files = append(files[:i], files[i+1:]...)
242-
i--
231+
j := 0
232+
for _, s := range files {
233+
if s != "" {
234+
files[j] = s
235+
j++
243236
}
244237
}
245-
return files, nil
238+
return files[:j], nil
246239
}

0 commit comments

Comments
 (0)