Introducing GCP’s new interactive CLI
Friday, March 9, 2018
If you develop applications on Google Cloud Platform (GCP), you probably spend a lot of time in the GCP command line. But as we grow our GCP services, the number of commands and flags is growing by leaps and bounds. So today, we’re introducing a new command line interface (CLI) that lets you discover—and use—all these commands more efficiently: gcloud interactive.
The Google Cloud SDK offers a variety of command line tools to interact with GCP, namely:
- gcloud — GCP’s primary CLI
- gsutil — CLI to interact with Google Cloud Storage
- bq — CLI to interact with Google BigQuery
- kubectl — Kubernetes Engine’s CLI
Currently in public alpha, the new interactive CLI environment provides auto-prompts and in-line help for gcloud, gsutil, bq and kubectl commands. No more context-switching as you search for command names, required flags or argument types in help pages. Now all of this information is included as part of the interactive environment as you type!
The interactive environment also supports standard bash features like:
- intermixing gcloud and standard bash commands
- running commands like cd and pwd, and set/use shell variables across command executions
- running and controlling background processes
- TAB-completing shell variables, and much more!
For example, you can assign the result of the command to a variable and later call this variable as an input to a different command:
$ active_vms=$(gcloud compute instances list --format="value(NAME)" --filter="STATUS=RUNNING")
$ echo $active_vms
You can also create and run bash scripts while you're in the interactive environment.
For example, the following script iterates all compute instances and restarts the ones that have been TERMINATED.
#!/bin/bash
terminated_vms=$(gcloud compute instances list --format="value(NAME)" --filter="STATUS=terminated")
for name in $terminated_vms
do
echo "Instance $name will restart."
zone=$(gcloud compute instances list --format="value(ZONE)" --filter="NAME=$name")
gcloud compute instances start $name --zone $zone
done
Getting started with gcloud interactive
Once you’ve installed the Google Cloud SDK, go ahead and try out gcloud interactive: (if you haven’t installed it yet, you can see instructions in this link)
1.Make sure your SDK components are up to date.
$ gcloud components update
2. Install the gcloud alpha component.
$ gcloud components install alpha
3. Start gcloud interactive
$ gcloud alpha interactive
[Optional] Enable interactive mode for gsutil, bq and kubectl (it's enabled for gcloud by default.) Note that this may take a couple of minutes to complete, but you only need to run this command once.
$ gcloud alpha interactive --update-cli-trees
Tips:
- When you want to learn more about the current command you typed, hit F8 to open the reference page in the browser.
- You can set the prompt context to any command group. This is useful if you mostly work with certain command groups, and saves you having to type the full command every time. You can do this by typing the command group and hitting F7.
- Toggle the interactive help area on and off with the F2 key.
- Use the F3 key to toggle the command line edit mode between emacs and vi.
Here at Google Cloud, we love developers and want you to be as productive as possible. Click here to learn more about gcloud interactive, and let us know what you think using the gcloud feedback command. Happy typing :)