Skip to content

Commit c9c6b75

Browse files
authored
Merge pull request GoogleCloudPlatform#173 from lychung83/master
fix GoogleCloudPlatform#157
2 parents 74e2f41 + f75ef85 commit c9c6b75

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

cmd/cloud_sql_proxy/cloud_sql_proxy.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ can be removed automatically by this program.`)
8181
token = flag.String("token", "", "When set, the proxy uses this Bearer token for authorization.")
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.`)
84+
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
8496
)
8597

8698
const (
@@ -334,9 +346,12 @@ func gcloudProject() []string {
334346

335347
if err := cmd.Run(); err != nil {
336348
if strings.Contains(err.Error(), "executable file not found") {
337-
// gcloud not installed; ignore the error
349+
// gcloud not found (probably not installed). Ignore the error but record
350+
// the status for later use.
351+
gcloudStatus = gcloudNotFound
338352
return nil
339353
}
354+
gcloudStatus = gcloudExecErr
340355
logging.Errorf("Error detecting gcloud project: %v", err)
341356
return nil
342357
}
@@ -348,6 +363,7 @@ func gcloudProject() []string {
348363
}
349364

350365
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
366+
gcloudStatus = gcloudExecErr
351367
logging.Errorf("Failed to unmarshal bytes from gcloud: %v", err)
352368
logging.Errorf(" gcloud returned:\n%s", buf)
353369
return nil

cmd/cloud_sql_proxy/proxy.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,24 @@ 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
344+
var flags string
341345
if fuse.Supported() {
342-
return nil, errors.New("must specify -projects, -fuse, or -instances")
346+
flags = "-projects, -fuse, -instances or -instances_metadata"
347+
} else {
348+
flags = "-projects, -instances or -instances_metadata"
349+
}
350+
351+
errStr := fmt.Sprintf("no instance selected because none of %s is specified", flags)
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)
343357
}
344-
return nil, errors.New("must specify -projects or -instances")
358+
return nil, errors.New(errStr)
345359
}
346360
return cfgs, nil
347361
}

0 commit comments

Comments
 (0)