Creating a Go-Based CLI for Google Compute Engine

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating the CLI
  5. Adding Functionality
  6. Testing and Deployment
  7. Conclusion

Introduction

In this tutorial, we will learn how to create a Go-based Command Line Interface (CLI) for managing instances on Google Compute Engine. We will use the Google Cloud SDK and the Compute Engine API to interact with the Google Cloud Platform. By the end of this tutorial, you will have a fully functional CLI that can create, list, delete, and manage instances on Google Compute Engine.

Prerequisites

To follow along with this tutorial, you will need the following prerequisites:

  • Basic knowledge of Go programming language
  • A Google Cloud Platform account with Compute Engine API enabled
  • Google Cloud SDK installed

Setup

To get started, make sure you have the Google Cloud SDK installed and authenticated with your Google Cloud Platform account. You can install the SDK by following the instructions provided in the Google Cloud SDK documentation.

Once you have the SDK installed, authenticate by running the following command in your terminal:

gcloud auth login

This will open a browser window to authenticate your account. Follow the on-screen instructions to complete the authentication process.

Creating the CLI

Let’s start by setting up the project structure for our CLI. Create a new directory called gce-cli and navigate into it:

mkdir gce-cli
cd gce-cli

Next, we will create the main Go file for our CLI. Create a new file called main.go and open it in your favorite text editor.

touch main.go

In main.go, add the following code:

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	cmd := exec.Command("gcloud", "compute", "instances", "list")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(1)
	}
}

This code sets up a basic CLI that uses the gcloud command to list all instances on Google Compute Engine. We use the os/exec package to execute the command and pipe its output to the terminal.

To build and run the CLI, use the following commands:

go build
./gce-cli

You should see a list of instances printed in the terminal.

Adding Functionality

Now that we have a basic CLI, let’s add more functionality to it. We will add commands to create, delete, and manage instances on Google Compute Engine.

Before we proceed, let’s organize our code by creating a separate package for our CLI commands. Inside the gce-cli directory, create a new directory called commands:

mkdir commands

Inside the commands directory, create a new Go file called list.go and open it in your text editor.

touch commands/list.go

In list.go, add the following code:

package commands

import (
	"fmt"
	"os"
	"os/exec"
)

func ListInstances() {
	cmd := exec.Command("gcloud", "compute", "instances", "list")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(1)
	}
}

This code defines a ListInstances function that executes the gcloud compute instances list command.

Next, open the main.go file again and update the code as follows:

package main

import (
	"fmt"
	"os"

	"github.com/your-username/gce-cli/commands"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Usage: gce-cli <command>")
		os.Exit(1)
	}

	switch os.Args[1] {
	case "list":
		commands.ListInstances()
	default:
		fmt.Println("Invalid command")
		os.Exit(1)
	}
}

In this updated code, we import the commands package and check the command-line arguments to determine the command to execute. If the command is list, we call the ListInstances function.

To build and run the updated CLI, use the following commands:

go build
./gce-cli list

You should see the list of instances printed in the terminal, just like before.

You can add more commands by creating additional files in the commands directory and defining functions for each command.

Testing and Deployment

Creating tests for your CLI commands is crucial to ensure their correctness. You can create a separate commands_test.go file inside the commands directory to write tests for each command.

To run the tests, use the following command:

go test ./commands/...

Once you are satisfied with your CLI and its tests, you can deploy it to a production environment. You can build a binary for different platforms using the GOOS and GOARCH environment variables. For example, to build a binary for Linux, you can use the following command:

GOOS=linux GOARCH=amd64 go build -o gce-cli-linux

Conclusion

In this tutorial, we learned how to create a Go-based CLI for managing instances on Google Compute Engine. We started by setting up the project structure and executing a basic command. Then, we added more functionality to the CLI by creating separate commands. We also covered testing and deployment for the CLI. With this knowledge, you can now build your own powerful Go-based CLI applications for various purposes.

Remember to explore additional features of the Google Cloud SDK and Compute Engine API to enhance your CLI further. Happy coding!


Note: It’s important to note that this is a simplified example and may not cover all possible use cases for working with Google Compute Engine. Make sure to refer to the official documentation for a comprehensive understanding of available options and best practices.