Skip to content

Commit

Permalink
Skelletor the install command
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs-msft committed Mar 19, 2019
1 parent d86cad9 commit 4a97bfc
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"github.com/deislabs/porter/pkg/porter"
"github.com/spf13/cobra"
)

func buildBundleCommands(p *porter.Porter) *cobra.Command {
cmd := &cobra.Command{
Use: "bundle",
Short: "bundle commands",
}

cmd.AddCommand(buildBundleInstallCommand(p))

return cmd
}

func buildBundleInstallCommand(p *porter.Porter) *cobra.Command {
opts := porter.InstallOptions{}
cmd := &cobra.Command{
Use: "install",
Short: "Install a bundle",
Example: ` porter install
porter install --insecure
porter install --file myapp/bundle.json
porter install --name MyAppInDev
porter install --param-file=base-values.txt --param-file=dev-values.txt --param test-mode=true --param header-color=blue
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Prepare()
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.InstallBundle(opts)
},
}

f := cmd.Flags()
f.BoolVar(&opts.Insecure, "insecure", false,
"Allow installing untrusted bundles")
f.StringVarP(&opts.File, "file", "f", "bundle.json",
"Path to the CNAB definition to install")
f.StringVar(&opts.Name, "name", "",
"Name of the claim, defaults to the name of the bundle")
f.StringSliceVar(&opts.ParamFiles, "param-file", nil,
"Path to a parameters definition file for the bundle, each line in the form of NAME=VALUE. May be specified multiple times.")
f.StringSliceVar(&opts.RawParams, "param", nil,
"Define an individual parameter in the form NAME=VALUE. Overrides parameters set with the same name using --param-file. May be specified multiple times.")

return cmd
}

func buildInstallCommand(p *porter.Porter) *cobra.Command {
return buildBundleInstallCommand(p)
}
38 changes: 38 additions & 0 deletions cmd/porter/bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateInstallCommand(t *testing.T) {
testcases := []struct {
name string
args string
wantError string
}{
{"no args", "install", ""},
{"invalid param", "install --param A:B", "invalid parameter (A:B), must be in name=value format"},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
p := buildRootCommand()
osargs := strings.Split(tc.args, " ")
cmd, args, err := p.Find(osargs)
require.NoError(t, err)

err = cmd.ParseFlags(args)
require.NoError(t, err)

err = cmd.PreRunE(cmd, args)
if tc.wantError == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.wantError)
}
})
}
}
2 changes: 2 additions & 0 deletions cmd/porter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func buildRootCommand() *cobra.Command {
cmd.AddCommand(buildCreateCommand(p))
cmd.AddCommand(buildRunCommand(p))
cmd.AddCommand(buildBuildCommand(p))
cmd.AddCommand(buildBundleCommands(p))
cmd.AddCommand(buildInstallCommand(p))
cmd.AddCommand(buildListCommands(p))

return cmd
Expand Down
31 changes: 31 additions & 0 deletions cmd/porter/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommandWiring(t *testing.T) {
testcases := []string{
"build",
"create",
"install",
"run",
"schema",
"bundle install",
"list mixins",
"version",
}

for _, tc := range testcases {
t.Run(tc, func(t *testing.T) {
osargs := strings.Split(tc, " ")

rootCmd := buildRootCommand()
_, _, err := rootCmd.Find(osargs)
assert.NoError(t, err)
})
}
}
48 changes: 48 additions & 0 deletions pkg/porter/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package porter

import (
"fmt"

"github.com/deislabs/porter/pkg/parameters"
)

type InstallOptions struct {
// Name of the claim.
Name string

// File path to the CNAB bundle.
File string

// Insecure bundle installation allowed.
Insecure bool

// RawParams is the unparsed list of NAME=VALUE parameters set on the command line.
RawParams []string

// Params is the parsed set of parameters from RawParams.
Params map[string]string

// ParamFiles is a list of file paths containing lines of NAME=VALUE parameter definitions.
ParamFiles []string
}

func (o *InstallOptions) Prepare() error {
return o.parseParams()
}

func (o *InstallOptions) parseParams() error {
p, err := parameters.ParseVariableAssignments(o.RawParams)
if err == nil {
o.Params = p
}
return err
}

func (p *Porter) InstallBundle(opts InstallOptions) error {
err := p.Config.LoadManifest()
if err != nil {
return err
}
fmt.Fprintf(p.Out, "installing %s...\n", p.Manifest.Name)
return nil
}
19 changes: 19 additions & 0 deletions pkg/porter/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package porter

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInstallOptions_Prepare(t *testing.T) {
opts := InstallOptions{
RawParams: []string{"A=1", "B=2"},
}

err := opts.Prepare()
require.NoError(t, err)

assert.Len(t, opts.Params, 2)
}

0 comments on commit 4a97bfc

Please sign in to comment.