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

Commit

Permalink
Merge pull request #22 from sanfrancrisko/GH-16/main/prm_set_get_puppet
Browse files Browse the repository at this point in the history
(GH-16) Add `prm (set|get) puppet` commands
  • Loading branch information
da-ar authored Nov 9, 2021
2 parents 8b7c69f + 03092c7 commit 4f43e85
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 24 deletions.
48 changes: 48 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,54 @@
"args": [
"--version"
],
},
{
"name": "Debug (With Telemetry): prm set puppet 7",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"args": [
"set",
"puppet",
"7"
],
"buildFlags": "-tags='telemetry' -ldflags='-X main.honeycomb_api_key=${input:honeycomb_api_key} -X main.honeycomb_dataset=pct_dev'",
},
{
"name": "Debug (No Telemetry): prm set puppet 7",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"args": [
"set",
"puppet",
"7"
],
},
{
"name": "Debug (With Telemetry): prm get puppet",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"args": [
"get",
"puppet",
],
"buildFlags": "-tags='telemetry' -ldflags='-X main.honeycomb_api_key=${input:honeycomb_api_key} -X main.honeycomb_dataset=pct_dev'",
},
{
"name": "Debug (No Telemetry): prm get puppet",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"args": [
"get",
"puppet",
],
}
],
"inputs": [
Expand Down
24 changes: 24 additions & 0 deletions cmd/get/get.go
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
}
66 changes: 66 additions & 0 deletions cmd/get/get_test.go
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)
}
})
}
}
22 changes: 22 additions & 0 deletions cmd/get/puppet.go
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())
}
5 changes: 5 additions & 0 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -84,6 +85,10 @@ func InitConfig() {
if err := viper.ReadInConfig(); err == nil {
log.Trace().Msgf("Using config file: %s", viper.ConfigFileUsed())
}

if err := prm.LoadConfig(); err != nil {
log.Warn().Msgf("Error setting running config: %s", err)
}
}

// Returns the cobra command called, e.g. new or install
Expand Down
45 changes: 45 additions & 0 deletions cmd/set/puppet.go
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
25 changes: 25 additions & 0 deletions cmd/set/set.go
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
}
101 changes: 101 additions & 0 deletions cmd/set/set_test.go
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)
}
}
})
}
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/puppetlabs/pdkgo v0.0.0-20211028170403-930883ee37a8
github.com/puppetlabs/pdkgo v0.0.0-20211102094418-ae84e9820e30
github.com/rs/zerolog v1.26.0
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/proto/otlp v0.10.0 // indirect
golang.org/x/net v0.0.0-20211104170005-ce137452f963 // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 // indirect
google.golang.org/grpc v1.42.0 // indirect
gotest.tools/v3 v3.0.3 // indirect
)
Loading

0 comments on commit 4f43e85

Please sign in to comment.