Skip to content

Commit 5be0746

Browse files
committed
more cleanup
1 parent db15206 commit 5be0746

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cmd/cloud_sql_proxy/cloud_sql_proxy.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,17 @@ can be removed automatically by this program.`)
8282
tokenFile = flag.String("credential_file", "", `If provided, this json file will be used to retrieve Service Account credentials.
8383
You may set the GOOGLE_APPLICATION_CREDENTIALS environment variable for the same effect.`)
8484

85-
// Set when gcloud execution failed.
86-
gcloudErrStr string
85+
// Set to non-default value when gcloud execution failed.
86+
gcloudStatus gcloudStatusCode
87+
)
88+
89+
type gcloudStatusCode int
90+
91+
const (
92+
gcloudOk gcloudStatusCode = iota
93+
gcloudNotFound
94+
// generic execution failure error not specified above.
95+
gcloudExecErr
8796
)
8897

8998
const (
@@ -338,10 +347,12 @@ func gcloudProject() []string {
338347

339348
if err := cmd.Run(); err != nil {
340349
if strings.Contains(err.Error(), "executable file not found") {
341-
gcloudErrStr = "gcloud could not be found in the system path"
350+
// gcloud not found (probably not installed). Ignore the error but record
351+
// the status for later use.
352+
gcloudStatus = gcloudNotFound
342353
return nil
343354
}
344-
gcloudErrStr = gcloudResultErrStr
355+
gcloudStatus = gcloudExecErr
345356
logging.Errorf("Error detecting gcloud project: %v", err)
346357
return nil
347358
}
@@ -353,7 +364,7 @@ func gcloudProject() []string {
353364
}
354365

355366
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
356-
gcloudErrStr = gcloudResultErrStr
367+
gcloudStatus = gcloudExecErr
357368
logging.Errorf("Failed to unmarshal bytes from gcloud: %v", err)
358369
logging.Errorf(" gcloud returned:\n%s", buf)
359370
return nil

cmd/cloud_sql_proxy/proxy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ func CreateInstanceConfigs(dir string, useFuse bool, instances []string, instanc
338338
}
339339
// FUSE disabled.
340340
if len(instances) == 0 && instancesSrc == "" {
341+
// Failure to specifying instance can be caused by following reasons.
342+
// 1. not enough information is provided by flags
343+
// 2. failed to invoke gcloud
341344
var flags string
342345
if fuse.Supported() {
343346
flags = "-projects, -fuse, or -instances"
@@ -346,8 +349,11 @@ func CreateInstanceConfigs(dir string, useFuse bool, instances []string, instanc
346349
}
347350

348351
errStr := fmt.Sprintf("no instance selected because none of %s is specified", flags)
349-
if gcloudErrStr != "" {
350-
errStr = fmt.Sprintf("%s and %s", errStr, gcloudErrStr)
352+
switch gcloudStatus {
353+
case gcloudNotFound:
354+
errStr = fmt.Sprintf("%s and gcloud could not be found in the system path", errStr)
355+
case gcloudExecErr:
356+
errStr = fmt.Sprintf("%s and gcloud failed to get the project list", errStr)
351357
}
352358
return nil, errors.New(errStr)
353359
}

0 commit comments

Comments
 (0)