@@ -81,6 +81,10 @@ 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.
8383You may set the GOOGLE_APPLICATION_CREDENTIALS environment variable for the same effect.` )
84+
85+ // Set when gcloud execution failed.
86+ gcloudErrStr string
87+
8488)
8589
8690const (
@@ -328,33 +332,36 @@ func listInstances(ctx context.Context, cl *http.Client, projects []string) ([]s
328332}
329333
330334func gcloudProject () []string {
331- buf := new (bytes.Buffer )
332- cmd := exec .Command ("gcloud" , "--format" , "json" , "config" , "list" , "core/project" )
333- cmd .Stdout = buf
334-
335- if err := cmd .Run (); err != nil {
336- if strings .Contains (err .Error (), "executable file not found" ) {
337- // gcloud not installed; ignore the error
338- return nil
339- }
340- logging .Errorf ("Error detecting gcloud project: %v" , err )
341- return nil
342- }
343-
344- var data struct {
345- Core struct {
346- Project string
347- }
348- }
349-
350- if err := json .Unmarshal (buf .Bytes (), & data ); err != nil {
351- logging .Errorf ("Failed to unmarshal bytes from gcloud: %v" , err )
352- logging .Errorf (" gcloud returned:\n %s" , buf )
353- return nil
354- }
355-
356- logging .Infof ("Using gcloud's active project: %v" , data .Core .Project )
357- return []string {data .Core .Project }
335+ const gcloudResultErrStr = "gcloud failed to get project list. See log for detail."
336+ buf := new (bytes.Buffer )
337+ cmd := exec .Command ("gcloud" , "--format" , "json" , "config" , "list" , "core/project" )
338+ cmd .Stdout = buf
339+
340+ if err := cmd .Run (); err != nil {
341+ if strings .Contains (err .Error (), "executable file not found" ) {
342+ gcloudErrStr = "gcloud could not be found in the system path"
343+ return nil
344+ }
345+ gcloudErrStr = gcloudResultErrStr
346+ logging .Errorf ("Error detecting gcloud project: %v" , err )
347+ return nil
348+ }
349+
350+ var data struct {
351+ Core struct {
352+ Project string
353+ }
354+ }
355+
356+ if err := json .Unmarshal (buf .Bytes (), & data ); err != nil {
357+ gcloudErrStr = gcloudResultErrStr
358+ logging .Errorf ("Failed to unmarshal bytes from gcloud: %v" , err )
359+ logging .Errorf (" gcloud returned:\n %s" , buf )
360+ return nil
361+ }
362+
363+ logging .Infof ("Using gcloud's active project: %v" , data .Core .Project )
364+ return []string {data .Core .Project }
358365}
359366
360367// Main executes the main function of the proxy, allowing it to be called from tests.
0 commit comments