Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-15) Add prm set backend command
Browse files Browse the repository at this point in the history
This commit adds the `prm set backend` command and some minor
refactoring of existing set command functionality and the `config`
package.
  • Loading branch information
sanfrancrisko committed Nov 30, 2021
1 parent 387f13d commit 37daa52
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
48 changes: 48 additions & 0 deletions cmd/set/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package set

import (
"fmt"
"strings"

"github.com/puppetlabs/prm/pkg/prm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var SelectedBackend prm.BackendType

func (sc *SetCommand) createSetBackendCommand() *cobra.Command {
tmp := &cobra.Command{
Use: "backend <BACKEND>",
Short: "Sets the backend exec environment to the specified type",
Long: `Sets the backend exec environment to the specified type`,
PreRunE: sc.setBackendPreRunE,
RunE: sc.setBackendType,
ValidArgs: []string{string(prm.DOCKER)},
}

return tmp
}

func (sc *SetCommand) setBackendPreRunE(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
return fmt.Errorf("too many args, please specify ONE of the following backend types after 'set backend':\n- %s", prm.DOCKER)
}

if len(args) < 1 {
return fmt.Errorf("please specify specify one of the following backend types after 'set backend':\n- %s", prm.DOCKER)
}

switch strings.ToLower(args[0]) {
case string(prm.DOCKER):
SelectedBackend = prm.DOCKER
default:
return fmt.Errorf("'%s' is not a valid backend type, please specify one of the following backend types:\n- %s", args[0], prm.DOCKER)
}

return nil
}

func (sc *SetCommand) setBackendType(cmd *cobra.Command, args []string) error {
return sc.Utils.SetAndWriteConfig(prm.BackendCfgKey, string(SelectedBackend))
}
3 changes: 3 additions & 0 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import (

func CreateSetCommand() *cobra.Command {
tmp := &cobra.Command{
// TODO: Add Backend as valid arg
Use: fmt.Sprintf("set %s", prm.PuppetCmdFlag),
Short: "Sets the specified configuration to the specified value",
Long: "Sets the specified configuration to the specified value",
DisableFlagsInUseLine: true,
// TODO: Add Backend as valid arg
ValidArgs: []string{prm.PuppetCmdFlag},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}

tmp.AddCommand(createSetPuppetCommand())
tmp.AddCommand(createSetBackendCommand())

return tmp
}
64 changes: 55 additions & 9 deletions cmd/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package set_test

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/puppetlabs/prm/cmd/set"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/stretchr/testify/assert"
)

type test struct {
name string
args []string
expectedOutput string
expectedPuppetVer string
(??) name string
(??) args []string
(??) expectedErrMsg string
(??) expectedPuppetVer string
expectError bool
}

Expand Down Expand Up @@ -69,6 +71,37 @@ func Test_SetPuppetCommand(t *testing.T) {
execTests(t, tests)
}

func Test_SetBackendCommand(t *testing.T) {
tests := []test{
{
name: "Should handle valid backend selection (docker)",
args: []string{"backend", "docker"},
expectedBackedType: prm.DOCKER,
},
{
name: "Should handle valid backend selection (dOcKeR)",
args: []string{"backend", "dOcKeR"},
expectedBackedType: prm.DOCKER,
},
{
name: "Should error when too many args supplied to 'backend' sub cmd",
args: []string{"backend", "foo", "bar"},
expectedErrMsg: fmt.Sprintf("Error: too many args, please specify ONE of the following backend types after 'set backend':\n- %s", prm.DOCKER),
},
{
name: "Should error when no arg supplied to 'badckend' sub cmd",
args: []string{"backend"},
expectedErrMsg: fmt.Sprintf("please specify specify one of the following backend types after 'set backend':\n- %s", prm.DOCKER),
},
{
name: "Should error when invalid backend type supplied to 'badckend' sub cmd",
args: []string{"backend", "foo"},
expectedErrMsg: fmt.Sprintf("Error: 'foo' is not a valid backend type, please specify one of the following backend types:\n- %s", prm.DOCKER),
},
}
execTests(t, tests)
}

func execTests(t *testing.T, tests []test) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -91,11 +124,24 @@ func execTests(t *testing.T, tests []test) {
return
}

if tt.expectedPuppetVer != "" {
if set.PuppetSemVer.String() != tt.expectedPuppetVer {
t.Errorf("Normalised Puppet version (%s) did not match expected version (%s)", set.PuppetSemVer.String(), tt.expectedPuppetVer)
}
}
validatePuppetVer(t, tt)
validateBackendType(t, tt)
})
}
}

func validatePuppetVer(t *testing.T, tt test) {
if tt.expectedPuppetVer != "" {
if set.PuppetSemVer.String() != tt.expectedPuppetVer {
t.Errorf("Normalised Puppet version (%s) did not match expected version (%s)", set.PuppetSemVer.String(), tt.expectedPuppetVer)
}
}
}

func validateBackendType(t *testing.T, tt test) {
if tt.expectedBackedType != "" {
if set.SelectedBackend != tt.expectedBackedType {
t.Errorf("Normalised Backend type (%s) did not match expected backend type (%s)", set.SelectedBackend, tt.expectedBackedType)
}
}
}
6 changes: 6 additions & 0 deletions pkg/prm/backend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//nolint:structcheck,unused
package prm

type BackendType string

const (
DOCKER BackendType = "docker"
)

type BackendI interface {
GetTool(toolName string, prmConfig Config) (Tool, error)
Validate(tool *Tool) (ToolExitCode, error)
Expand Down
1 change: 1 addition & 0 deletions pkg/prm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
PuppetCmdFlag string = "puppet"
PuppetVerCfgKey string = "puppet.version"
DefaultPuppetVer string = "7"
BackendCfgKey string = "backend.type"
)

type Config struct {
Expand Down

0 comments on commit 37daa52

Please sign in to comment.