Bluemix Command Line (Bluemix CLI) is a useful tool for developers and operators to access and manage IBM Bluemix, IBM’s Platform as a Service cloud solution. It is also a must-have for automation tasks. As Bluemix evolves, the need to open the CLI capabilities to the whole community increases. Today, I’m excited to announce the open source project of IBM Bluemix CLI SDK on GitHub.

Features

The IBM Bluemix CLI SDK provides the following capabilities:

  • Libraries to interact with Bluemix artifacts
  • Common utilities to facilitate plugin development
  • Plugin interface definition
  • Plugin examples
  • Developer guide

A “Hello World” plugin

Below is a quick guide for you to develop a “Hello World” plugin. For details, please refer to the plugin examples in GitHub.

Prerequisites

  • You should have basic knowledge of Go programming language. Start with Go official documents if you’re not familiar with this language.
  • Install Go on your machine. At the time of this writing, we recommend Go 1.7.4. You can download Go language binaries here.
  • Install Bluemix CLI. Refer to the Bluemix documentation regarding the installation details.
  • Install govendor, which is a Go package management tool.

Download Bluemix CLI SDK

First, download Bluemix CLI SDK under your workspace by typing in:

> go get github.com/IBM-Bluemix/bluemix-cli-sdk

Then, go to the “bluemix-cli-sdk” folder, and restore its dependency:

> govender sync

You can verify the SDK by typing:

> go test ./...

Compose your plugin

Project setup

Create a Go project, “hello-world-plugin,” under your local Go workspace, and then create a main.go file as the application entry.

Plugin definition

In the main.go, import “github.com/IBM-Bluemix/Bluemix-cli-sdk/plugin”.

Let’s define a struct “HelloWorldPlugin”, and define two methods: Run() and GetMetadata(), like below:

package main
import (
       "github.com/IBM-Bluemix/bluemix-cli-sdk/plugin"
)

type HelloWorldPlugin struct{}

func (pluginDemo *HelloWorldPlugin) Run(context plugin.PluginContext, args []strong) {
        // TODO
}

func (pluginDemo * HelloWorldPlugin) GetMetadata() plugin.PluginMetadata
{
         // TODO
}

Run() is the entry of the plugin. And GetMetadata() provides the metadata of the plugin and its commands.

Plugin and command definition

In GetMetadata(), let’s give the plugin a name called “hello-world” and a version 0.1.0. Also. let’s define a command “hello”.

See the code snippet below for how to do this:

func (pluginDemo *HelloWorldPlugin) GetMetadata() plugin.PluginMetadata {
       return plugin.PluginMetadata{
              Name: "hello-world",
              Version: plugin.VersionType{
                      Major: 0,
                      Minor: 1,
                      Build: 0,
              },
              Commands: []plugin.Command{
                      {
                             Name:         "hello",   
                             Alias:        "hi",
                             Description:  "Say hello to Bluemix.",
                             Usage:        "bluemix hello",
                       },

               },
         }
}

Command implementation

Run() is the entry of plugin implementation. Normally, we handle user input here. You can use whatever command line parsing library you want (for example, https://github.com/urfave/cli). Here we only implement a simple logic to echo the user input.

Add an import “fmt”, and fill in the following codes into Run().

func (pluginDemo *HelloWorldPlugin) Run (context plugin.PluginContext,
args []string {
       if len (args) == 2 && (args[1] == "hello" || args[1] == "hi") {
                fmt.Print1n("Hi, this is my first plugin for Bluemix")
       } else {
                fmt.Print1n("Input error.")
       }
}

Last but not least, don’t forget to define the application entry main().

func main() {
       plugin.Start (new(HelloWorldPLugin))
}

Compile and run

You’re almost at the end! Go back to the root folder of “hello-world-plugin” and execute the following command.
> go build -o hello-world

If you’ve done everything correctly, the compilation should be successful, and you should get a binary executable. That’s the plguin!

Use the following command to install the plugin.

> bluemix plugin install hello-world

And then have a try:

> bluemix hello
Hi, This is my first plugin for Bluemix

Congratulations, you have your first plugin for Bluemix CLI.

References

Join The Discussion

Your email address will not be published. Required fields are marked *