This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(GH-16) Add `prm (set|get) puppet` commands
- Loading branch information
Showing
12 changed files
with
416 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package get | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/puppetlabs/prm/pkg/prm" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CreateGetCommand() *cobra.Command { | ||
tmp := &cobra.Command{ | ||
Use: fmt.Sprintf("get %s", prm.PuppetCmdFlag), | ||
Short: "Displays the requested configuration value", | ||
Long: "Displays the requested configuration value", | ||
DisableFlagsInUseLine: true, | ||
ValidArgs: []string{prm.PuppetCmdFlag}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.HelpFunc()(cmd, args) | ||
}, | ||
} | ||
tmp.AddCommand(createGetPuppetCommand()) | ||
|
||
return tmp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package get_test | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/puppetlabs/prm/cmd/get" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type test struct { | ||
name string | ||
args []string | ||
expectedOutput string | ||
expectError bool | ||
} | ||
|
||
func Test_GetCommand(t *testing.T) { | ||
tests := []test{ | ||
{ | ||
name: "Should display help when no subcommand passed to 'get'", | ||
args: []string{""}, | ||
expectedOutput: "Displays the requested configuration value", | ||
expectError: true, | ||
}, | ||
} | ||
execTests(t, tests) | ||
} | ||
|
||
func Test_GetPuppetCommand(t *testing.T) { | ||
tests := []test{ | ||
{ | ||
name: "Should display help when invalid subcommand passed to 'get'", | ||
args: []string{"foo"}, | ||
expectedOutput: "Error: unknown command \"foo\" for \"get\"", | ||
expectError: true, | ||
}, | ||
} | ||
execTests(t, tests) | ||
} | ||
|
||
func execTests(t *testing.T, tests []test) { | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
getCmd := get.CreateGetCommand() | ||
b := bytes.NewBufferString("") | ||
getCmd.SetOutput(b) | ||
getCmd.SetArgs(tt.args) | ||
|
||
err := getCmd.Execute() | ||
|
||
if (err != nil) && (!tt.expectError) { | ||
t.Errorf("Unexpected error message: %s", err) | ||
return | ||
} | ||
|
||
out, _ := ioutil.ReadAll(b) | ||
|
||
if tt.expectedOutput != "" { | ||
assert.Contains(t, string(out), tt.expectedOutput) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package get | ||
|
||
import ( | ||
"github.com/puppetlabs/prm/pkg/prm" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func createGetPuppetCommand() *cobra.Command { | ||
tmp := &cobra.Command{ | ||
Use: "puppet", | ||
Short: "Gets the Puppet runtime version currently configured", | ||
Long: "Gets the Puppet runtime version currently configured", | ||
Run: getPuppetVersion, | ||
} | ||
|
||
return tmp | ||
} | ||
|
||
func getPuppetVersion(cmd *cobra.Command, args []string) { | ||
log.Info().Msgf("Puppet version is configured to: %s", prm.RunningConfig.PuppetVersion.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package set | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Masterminds/semver" | ||
"github.com/puppetlabs/prm/pkg/prm" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var PuppetSemVer *semver.Version | ||
|
||
func createSetPuppetCommand() *cobra.Command { | ||
tmp := &cobra.Command{ | ||
Use: "puppet <VERSION>", | ||
Short: "Sets the Puppet runtime to the specified version", | ||
Long: `Sets the Puppet runtime to the specified version`, | ||
RunE: setPuppetVersion, | ||
} | ||
|
||
return tmp | ||
} | ||
|
||
func setPuppetVersion(cmd *cobra.Command, args []string) (err error) { | ||
if len(args) > 1 { | ||
return fmt.Errorf("only a single Puppet version can be set") | ||
} | ||
|
||
if len(args) < 1 { | ||
return fmt.Errorf("please specify a Puppet version after 'set puppet'") | ||
} | ||
|
||
PuppetSemVer, err = semver.NewVersion(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("'%s' is not a semantic (x.y.z) Puppet version: %s", args[0], err) | ||
} | ||
|
||
viper.Set(prm.PuppetVerCfgKey, PuppetSemVer.String) | ||
|
||
return err | ||
} | ||
|
||
// TODO: (GH-26) Consume a list of available Puppet versions to faciliate tab completion | ||
// on command line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package set | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/puppetlabs/prm/pkg/prm" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CreateSetCommand() *cobra.Command { | ||
tmp := &cobra.Command{ | ||
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, | ||
ValidArgs: []string{prm.PuppetCmdFlag}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.HelpFunc()(cmd, args) | ||
}, | ||
} | ||
|
||
tmp.AddCommand(createSetPuppetCommand()) | ||
|
||
return tmp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package set_test | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/puppetlabs/prm/cmd/set" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type test struct { | ||
name string | ||
args []string | ||
expectedOutput string | ||
expectedPuppetVer string | ||
expectError bool | ||
} | ||
|
||
func Test_SetCommand(t *testing.T) { | ||
tests := []test{ | ||
{ | ||
name: "Should display help when no subcommand passed to 'set'", | ||
args: []string{""}, | ||
expectedOutput: "Sets the specified configuration to the specified value", | ||
expectError: true, | ||
}, | ||
} | ||
execTests(t, tests) | ||
} | ||
|
||
func Test_SetPuppetCommand(t *testing.T) { | ||
tests := []test{ | ||
{ | ||
name: "Should display help when invalid subcommand passed to 'set'", | ||
args: []string{"foo"}, | ||
expectedOutput: "Error: unknown command \"foo\" for \"set\"", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Should keep 'X.Y.Z' ver as-is", | ||
args: []string{"puppet", "7.10.1"}, | ||
expectedPuppetVer: "7.10.1", | ||
}, | ||
{ | ||
name: "Should normalise 'X' ver to 'X.Y.Z'", | ||
args: []string{"puppet", "7"}, | ||
expectedPuppetVer: "7.0.0", | ||
}, | ||
{ | ||
name: "Should error when too many args supplied to 'puppet' sub cmd", | ||
args: []string{"puppet", "7", "a", "b"}, | ||
expectedOutput: "Error: only a single Puppet version can be set", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Should error when no arg supplied to 'puppet' sub cmd", | ||
args: []string{"puppet"}, | ||
expectedOutput: "Error: please specify a Puppet version after 'set puppet'", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Should error when invalid version supplied to 'puppet' sub cmd", | ||
args: []string{"puppet", "foo"}, | ||
expectedOutput: "Error: 'foo' is not a semantic (x.y.z) Puppet version", | ||
expectError: true, | ||
}, | ||
} | ||
execTests(t, tests) | ||
} | ||
|
||
func execTests(t *testing.T, tests []test) { | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
setCmd := set.CreateSetCommand() | ||
b := bytes.NewBufferString("") | ||
setCmd.SetOutput(b) | ||
setCmd.SetArgs(tt.args) | ||
|
||
err := setCmd.Execute() | ||
|
||
if (err != nil) && (!tt.expectError) { | ||
t.Errorf("Unexpected error message: %s", err) | ||
return | ||
} | ||
|
||
if tt.expectedOutput != "" { | ||
out, _ := ioutil.ReadAll(b) | ||
assert.Contains(t, string(out), tt.expectedOutput) | ||
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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.