Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

[feat-633-bundle-actions-cmd] Add bundle actions command #666

Merged
merged 3 commits into from Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/duffle/bundle.go
Expand Up @@ -23,6 +23,7 @@ func newBundleCmd(w io.Writer) *cobra.Command {
newBundleSignCmd(w),
newBundleVerifyCmd(w),
newBundleRemoveCmd(w),
newBundleActionsCmd(w),
)
return cmd
}
67 changes: 67 additions & 0 deletions cmd/duffle/bundle_actions.go
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"io"

"github.com/deislabs/duffle/pkg/duffle/home"

"github.com/gosuri/uitable"
"github.com/spf13/cobra"
)

const bundleActionsDesc = ` Lists all actions available in a bundle`

type bundleActionsCmd struct {
out io.Writer
home home.Home
bundle string
insecure bool
bundleIsFile bool
}

func newBundleActionsCmd(w io.Writer) *cobra.Command {
a := &bundleActionsCmd{out: w}

cmd := &cobra.Command{
Use: "actions BUNDLE",
Short: bundleActionsDesc,
Long: bundleActionsDesc,
RunE: func(cmd *cobra.Command, args []string) error {
a.bundle = args[0]
a.home = home.Home(homePath())

return a.run()
},
}

cmd.Flags().BoolVarP(&a.bundleIsFile, "bundle-is-file", "f", false, "Indicates that the bundle source is a file path")
cmd.Flags().BoolVarP(&a.insecure, "insecure", "k", false, "Do not verify the bundle (INSECURE)")

return cmd
}

func (a *bundleActionsCmd) run() error {
bundleFile, err := resolveBundleFilePath(a.bundle, a.home.String(), a.bundleIsFile, a.insecure)
if err != nil {
return err
}

bun, err := loadBundle(bundleFile, a.insecure)
if err != nil {
return err
}

table := uitable.New()
table.MaxColWidth = 100
table.Wrap = true

table.AddRow("ACTION", "STATELESS", "MODIFIES", "DESCRIPTION")
for name, act := range bun.Actions {
table.AddRow(name, act.Stateless, act.Modifies, act.Description)
}

fmt.Fprintln(a.out, table)

return nil
}
42 changes: 42 additions & 0 deletions cmd/duffle/bundle_actions_test.go
@@ -0,0 +1,42 @@
package main

import (
"bytes"
"strings"
"testing"
)

func TestBundleActions(t *testing.T) {
out := bytes.NewBuffer(nil)
cmd := &bundleActionsCmd{
out: out,
bundleIsFile: true,
bundle: "./testdata/testbundle/actions-test-bundle.json",
insecure: true,
}

if err := cmd.run(); err != nil {
t.Fatal(err)
}

result := out.String()

newLines := strings.Count(result, "\n")

// the bundle contains 3 actions, plus the table header
// so the output should have 4 lines
if newLines != 4 {
t.Errorf("Expected output to have 4 lines. It had %v", newLines)
}

if !strings.Contains(result, "dry-run") {
t.Errorf("Expected actions to contain %s but did not", "dry-run")
}
if !strings.Contains(result, "migrate") {
t.Errorf("Expected actions to contain %s but did not", "migrate")
}

if !strings.Contains(result, "status") {
t.Errorf("Expected actions to contain %s but did not", "status")
}
}
17 changes: 17 additions & 0 deletions cmd/duffle/testdata/testbundle/actions-test-bundle.json
@@ -0,0 +1,17 @@
{
"actions": {
"dry-run": {
"description": "prints what install would do with the given parameters values",
"modifies": false,
"stateless": true
},
"migrate": {
"description": "migration action",
"modifies": false
},
"status": {
"description": "retrieves the status of an installation",
"modifies": false
}
}
}