Table of Contents
- Introduction
- Prerequisites
- Setting up Google Cloud Firestore
-
Creating a Go-based CLI 1. Installing Required Packages 2. Initializing a New Go Module 3. Configuring Authentication 4. Implementing CLI Commands 5. Building and Testing the CLI
- Conclusion
Introduction
In this tutorial, we will learn how to develop a command-line interface (CLI) using Go to interact with Google Cloud Firestore, a flexible, scalable, and fully managed NoSQL document database. By the end of this tutorial, you will be able to create a Go-based CLI application that can perform basic CRUD operations on Google Cloud Firestore.
Prerequisites
Before starting this tutorial, you should have the following prerequisites:
- Basic understanding of the Go programming language.
-
A Google Cloud project with the Cloud Firestore API enabled.
- Google Cloud SDK installed and authenticated with your project.
Setting up Google Cloud Firestore
First, let’s set up Google Cloud Firestore for our project:
- Create a new Google Cloud project or use an existing one.
-
Enable the Cloud Firestore API.
-
Create a new Firestore database in the project.
Make sure to note down your project ID and the path to your Firestore database, as we will need them later.
Creating a Go-based CLI
Installing Required Packages
To begin, we need to install some required Go packages. Open a terminal and run the following command to install them:
go get github.com/spf13/cobra
go get cloud.google.com/go/firestore
The cobra
package will help us create a CLI application with commands and flags, while the cloud.google.com/go/firestore
package will provide the necessary functionality to interact with Google Cloud Firestore.
Initializing a New Go Module
Next, let’s create a new directory and initialize it as a Go module. Run the following commands in the terminal:
mkdir firestore-cli
cd firestore-cli
go mod init github.com/your-username/firestore-cli
Replace your-username
with your actual GitHub username or any other valid name you prefer.
Configuring Authentication
To access your Firestore database, we need to configure authentication. Follow these steps:
- Create a new service account key in your Google Cloud project.
-
Download the JSON key file and save it in a secure location.
-
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the location of the JSON key file. This can be done by running the following command in the terminal:```bash export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json ```
Implementing CLI Commands
Now, let’s start implementing our CLI commands using the cobra
package. Create a new file called cli.go
in the firestore-cli
directory with the following content:
package main
import (
"context"
"log"
"cloud.google.com/go/firestore"
"github.com/spf13/cobra"
"google.golang.org/api/iterator"
)
var rootCmd = &cobra.Command{
Use: "firestore-cli",
Short: "A CLI for interacting with Google Cloud Firestore",
}
var listCmd = &cobra.Command{
Use: "list",
Short: "List all documents in a collection",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
collection := args[0]
client, err := firestore.NewClient(context.Background(), "your-project-id")
if err != nil {
log.Fatalf("Failed to create Firestore client: %v", err)
}
defer client.Close()
iter := client.Collection(collection).Documents(context.Background())
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate documents: %v", err)
}
log.Printf("Document ID: %s, Data: %+v", doc.Ref.ID, doc.Data())
}
},
}
func init() {
rootCmd.AddCommand(listCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatalf("Failed to execute command: %v", err)
}
}
In the listCmd
command, replace "your-project-id"
with your actual Google Cloud project ID.
This code sets up a CLI application with a single command list
that accepts a collection name as an argument and lists all documents within that collection. It uses the firestore
package to interact with Google Cloud Firestore.
Building and Testing the CLI
To build the CLI, run the following command in the terminal:
go build -o firestore-cli
This will create an executable file named firestore-cli
.
Now, let’s test our CLI. Run the following command to list all documents in a collection named “users”:
./firestore-cli list users
If everything is set up correctly, you should see the document IDs and their corresponding data printed in the terminal.
You can add more commands to the CLI by following a similar pattern as the list
command.
Conclusion
Congratulations! You have developed a Go-based CLI for Google Cloud Firestore that can perform basic CRUD operations on your database. You learned how to set up Google Cloud Firestore, create a Go module, configure authentication, implement CLI commands using the cobra
package, and interact with Firestore using the cloud.google.com/go/firestore
package.
Feel free to explore and expand upon this CLI to add more functionality or customize it according to your needs. Happy coding!