Skip to content

Commit

Permalink
Merge pull request #33 from kopachevsky/master
Browse files Browse the repository at this point in the history
Initial implementation of CFT cli tool on golang
  • Loading branch information
morgante committed May 21, 2019
2 parents 27ec954 + b1fc0fc commit 6b8b714
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SHELL := /bin/bash

# need to change this to VERSION=`git describe --tags`
VERSION=v0.1.0
BINARY=bin/cft
GITHUB_REPO=github.com/GoogleCloudPlatform/cloud-foundation-toolkit

# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS=-ldflags "-X $(GITHUB_REPO)/cli/cmd.Version=$(VERSION)"

build:
go build ${LDFLAGS} -o bin/cft

.PHONY: test
test:
go test ./cmd/...
39 changes: 39 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Cloud Foundation Toolkit CLI Project

## Overview

### Requirements

* go 1.12

### Build and Run

make build

After build find binary at bin/cft location

### Usage

Follow cft --help instructions


Google Cloud Formation Toolkit CLI

```bash
Usage:
cft [flags]
cft [command] [flags]

Available Commands:
help Help about any command
version Print version information

Flags:
-h, --help help for cft

Use "cft [command] --help" for more information about a command.
```

## License

Apache 2.0 - See [LICENSE](LICENSE) for more information.
2 changes: 2 additions & 0 deletions cli/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
55 changes: 55 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Short: "Google Cloud Formation Toolkit CLI",
Long: "Google Cloud Formation Toolkit CLI",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// no params means same as -h flag
if args == nil || len(args) == 0 {
cmd.HelpFunc()(cmd, args)
}
},
}

func init() {
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}{{.UseLine}}{{end}}
{{if .HasAvailableSubCommands}}{{.CommandPath}} [command] [flags]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`)
if os.Args == nil {
rootCmd.SetArgs([]string{"-h"})
}
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
69 changes: 69 additions & 0 deletions cli/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"bytes"
"github.com/spf13/cobra"
"strings"
"testing"
)

func ExecuteCommand(root *cobra.Command, args ...string) (output string, err error) {
_, output, err = ExecuteCommandC(root, args...)
return output, err
}

func ExecuteCommandC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) {
buf := new(bytes.Buffer)
setOutput(root, buf)

// reset command state after prev test execution
root.SetArgs([]string{})
root.ResetFlags()

// set child command and/or command line args
if args != nil {
root.SetArgs(args)
}

c, err = root.ExecuteC()

return c, buf.String(), err
}

func setOutput(rootCommand *cobra.Command, buf *bytes.Buffer) {
rootCommand.SetOutput(buf)
for _, command := range rootCommand.Commands() {
setOutput(command, buf)
}
}

func TestRootCommand(t *testing.T) {
rootCmd.SetArgs([]string{})
output, err := ExecuteCommand(rootCmd)
if output == "" {
t.Errorf("Unexpected output: %v", output)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}

func TestRootCommandHelpArgs(t *testing.T) {
output, err := ExecuteCommand(rootCmd, "-h")
if output == "" {
t.Errorf("Unexpected output: %v", output)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}

func TestRootCommandWithUnknownCommand(t *testing.T) {
output, err := ExecuteCommand(rootCmd, "unknown")
if !strings.HasPrefix(output, "Error: unknown command \"unknown\" for \"cft\"") {
t.Errorf("Unexpected output: %v", output)
}
if err == nil {
t.Errorf("Expected unkwnown command error")
}
}
22 changes: 22 additions & 0 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/spf13/cobra"
)

var Version string

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version information",
Long: `Print version information
example: CFT CLI version v1.0.0`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("CFT CLI version", Version)
},
}
33 changes: 33 additions & 0 deletions cli/cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"strings"
"testing"
)

func TestVersionCommand(t *testing.T) {
c, output, err := ExecuteCommandC(rootCmd, "version")

if c.Name() != "version" {
t.Errorf(`invalid command returned from ExecuteC: expected "version"', got %q`, c.Name())
}

if output == "" {
t.Errorf("Unexpected output: %v", output)
}

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}

func TestVersionCommandHelp(t *testing.T) {

output, err := ExecuteCommand(rootCmd, "version", "-h")
if !strings.HasPrefix(output, versionCmd.Long) {
t.Errorf("Unexpected output: %v", output)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
8 changes: 8 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/GoogleCloudPlatform/cloud-foundation-toolkit/cli

go 1.12

require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
)
4 changes: 4 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
10 changes: 10 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/cli/cmd"
)

func main() {

cmd.Execute()
}

0 comments on commit 6b8b714

Please sign in to comment.