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

Commit

Permalink
Refactor pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
petergmurphy committed Jun 29, 2022
1 parent 7928adf commit 00f0db7
Show file tree
Hide file tree
Showing 33 changed files with 553 additions and 429 deletions.
10 changes: 6 additions & 4 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package exec

import (
"fmt"
"github.com/puppetlabs/prm/pkg/backend/docker"
"github.com/puppetlabs/prm/pkg/config"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -92,10 +94,10 @@ func preExecute(cmd *cobra.Command, args []string) error {
}

switch prmApi.RunningConfig.Backend {
case prm.DOCKER:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
case config.DOCKER:
prmApi.Backend = &docker.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
default:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
prmApi.Backend = &docker.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
}

if prmApi.CodeDir == "" {
Expand Down Expand Up @@ -142,7 +144,7 @@ func flagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]str
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
localToolPath = viper.GetString(prm.ToolPathCfgKey)
localToolPath = viper.GetString(config.ToolPathCfgKey)

return completeName(localToolPath, toComplete), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exec_test

import (
"bytes"
"github.com/puppetlabs/prm/pkg/config"
"io/ioutil"
"path"
"regexp"
Expand Down Expand Up @@ -89,7 +90,7 @@ plugin:
prmObj := &prm.Prm{
AFS: &afero.Afero{Fs: fs},
IOFS: &afero.IOFS{Fs: fs},
RunningConfig: prm.Config{
RunningConfig: config.Config{
ToolPath: toolDir,
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package get

import (
"fmt"
"github.com/puppetlabs/prm/pkg/config"

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

func CreateGetCommand(parent *prm.Prm) *cobra.Command {
tmp := &cobra.Command{
Use: fmt.Sprintf("get <%s|%s>", prm.BackendCmdFlag, prm.PuppetCmdFlag),
Use: fmt.Sprintf("get <%s|%s>", config.BackendCmdFlag, config.PuppetCmdFlag),
Short: "Displays the requested configuration value",
Long: "Displays the requested configuration value",
DisableFlagsInUseLine: true,
ValidArgs: []string{prm.BackendCmdFlag, prm.PuppetCmdFlag},
ValidArgs: []string{config.BackendCmdFlag, config.PuppetCmdFlag},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package install

import (
"fmt"
"github.com/puppetlabs/prm/pkg/config"

"github.com/spf13/afero"

"github.com/puppetlabs/pct/pkg/install"
"github.com/puppetlabs/pct/pkg/telemetry"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (ic *InstallCommand) executeInstall(cmd *cobra.Command, args []string) erro

func (ic *InstallCommand) setInstallPath() error {
if ic.InstallPath == "" {
defaultToolPath := viper.GetString(prm.ToolPathCfgKey)
defaultToolPath := viper.GetString(config.ToolPathCfgKey)
if defaultToolPath == "" {
return fmt.Errorf("Could not determine location to install tool") //: %v", err)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/set/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package set

import (
"fmt"
"github.com/puppetlabs/prm/pkg/config"
"strings"

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

var SelectedBackend prm.BackendType
var SelectedBackend config.BackendType

func (sc *SetCommand) createSetBackendCommand() *cobra.Command {
tmp := &cobra.Command{
Expand All @@ -17,31 +17,31 @@ func (sc *SetCommand) createSetBackendCommand() *cobra.Command {
Long: `Sets the backend exec environment to the specified type`,
PreRunE: sc.setBackendPreRunE,
RunE: sc.setBackendType,
ValidArgs: []string{string(prm.DOCKER)},
ValidArgs: []string{string(config.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)
return fmt.Errorf("too many args, please specify ONE of the following backend types after 'set backend':\n- %s", config.DOCKER)
}

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

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

return nil
}

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

import (
"fmt"
"github.com/puppetlabs/prm/pkg/config"

"github.com/Masterminds/semver"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func (sc *SetCommand) setPuppetVersion(cmd *cobra.Command, args []string) error
return fmt.Errorf("'%s' is not a semantic (x.y.z) Puppet version: %s", args[0], err)
}

return sc.Utils.SetAndWriteConfig(prm.PuppetVerCfgKey, puppetSemVer.String())
return config.SetAndWriteConfig(config.PuppetVerCfgKey, puppetSemVer.String())
}

// TODO: (GH-26) Consume a list of available Puppet versions to faciliate tab completion
Expand Down
6 changes: 3 additions & 3 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package set

import (
"fmt"
"github.com/puppetlabs/prm/pkg/config"

"github.com/puppetlabs/prm/pkg/prm"
"github.com/puppetlabs/prm/pkg/utils"
"github.com/spf13/cobra"
)
Expand All @@ -14,11 +14,11 @@ type SetCommand struct {

func (sc *SetCommand) CreateSetCommand() *cobra.Command {
tmp := &cobra.Command{
Use: fmt.Sprintf("set <%s|%s> value", prm.BackendCmdFlag, prm.PuppetCmdFlag),
Use: fmt.Sprintf("set <%s|%s> value", config.BackendCmdFlag, config.PuppetCmdFlag),
Short: "Sets the specified configuration to the specified value",
Long: "Sets the specified configuration to the specified value",
DisableFlagsInUseLine: true,
ValidArgs: []string{prm.BackendCmdFlag, prm.PuppetCmdFlag},
ValidArgs: []string{config.BackendCmdFlag, config.PuppetCmdFlag},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
Expand Down
14 changes: 7 additions & 7 deletions cmd/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package set_test
import (
"bytes"
"fmt"
"github.com/puppetlabs/prm/pkg/config"
"io/ioutil"
"testing"

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

Expand All @@ -17,7 +17,7 @@ type test struct {
args []string
expectedOutput string
expectedPuppetVer string
expectedBackedType prm.BackendType
expectedBackedType config.BackendType
expectError bool
}

Expand Down Expand Up @@ -78,29 +78,29 @@ func Test_SetBackendCommand(t *testing.T) {
{
name: "Should handle valid backend selection (docker)",
args: []string{"backend", "docker"},
expectedBackedType: prm.DOCKER,
expectedBackedType: config.DOCKER,
},
{
name: "Should handle valid backend selection (dOcKeR)",
args: []string{"backend", "dOcKeR"},
expectedBackedType: prm.DOCKER,
expectedBackedType: config.DOCKER,
},
{
name: "Should error when too many args supplied to 'backend' sub cmd",
args: []string{"backend", "foo", "bar"},
expectedOutput: fmt.Sprintf("Error: too many args, please specify ONE of the following backend types after 'set backend':\n- %s", prm.DOCKER),
expectedOutput: fmt.Sprintf("Error: too many args, please specify ONE of the following backend types after 'set backend':\n- %s", config.DOCKER),
expectError: true,
},
{
name: "Should error when no arg supplied to 'badckend' sub cmd",
args: []string{"backend"},
expectedOutput: fmt.Sprintf("please specify specify one of the following backend types after 'set backend':\n- %s", prm.DOCKER),
expectedOutput: fmt.Sprintf("please specify specify one of the following backend types after 'set backend':\n- %s", config.DOCKER),
expectError: true,
},
{
name: "Should error when invalid backend type supplied to 'badckend' sub cmd",
args: []string{"backend", "foo"},
expectedOutput: fmt.Sprintf("Error: 'foo' is not a valid backend type, please specify one of the following backend types:\n- %s", prm.DOCKER),
expectedOutput: fmt.Sprintf("Error: 'foo' is not a valid backend type, please specify one of the following backend types:\n- %s", config.DOCKER),
expectError: true,
},
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package status

import (
"fmt"
"github.com/puppetlabs/prm/pkg/backend"
"github.com/puppetlabs/prm/pkg/backend/docker"

"github.com/puppetlabs/prm/pkg/prm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,13 +38,13 @@ func CreateStatusCommand(parent *prm.Prm) *cobra.Command {
func preExecute(cmd *cobra.Command, args []string) error {
switch prmApi.RunningConfig.Backend {
default:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, ContextTimeout: prmApi.RunningConfig.Timeout}
prmApi.Backend = &docker.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, ContextTimeout: prmApi.RunningConfig.Timeout}
}
return nil
}

func execute(cmd *cobra.Command, args []string) error {
status, err := prm.FormatStatus(prmApi.GetStatus(), format)
status, err := backend.FormatStatus(prmApi.GetStatus(), format)
if err != nil {
return err
}
Expand Down
39 changes: 29 additions & 10 deletions cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package validate

import (
"fmt"
"github.com/puppetlabs/prm/pkg/backend"
"github.com/puppetlabs/prm/pkg/backend/docker"
"github.com/puppetlabs/prm/pkg/config"
"github.com/puppetlabs/prm/pkg/validate"
"os"
"os/user"
"path"
Expand Down Expand Up @@ -127,10 +131,10 @@ func preExecute(cmd *cobra.Command, args []string) error {
}

switch prmApi.RunningConfig.Backend {
case prm.DOCKER:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
case config.DOCKER:
prmApi.Backend = &docker.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
default:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
prmApi.Backend = &docker.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild, ContextTimeout: prmApi.RunningConfig.Timeout}
}

if !listTools {
Expand Down Expand Up @@ -179,7 +183,7 @@ func flagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]str
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
localToolPath = viper.GetString(prm.ToolPathCfgKey)
localToolPath = viper.GetString(config.ToolPathCfgKey)

return completeName(localToolPath, toComplete), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
}
Expand Down Expand Up @@ -233,16 +237,23 @@ func execute(cmd *cobra.Command, args []string) error {
additionalToolArgs, _ = shlex.Split(toolArgs)
}

toolInfo := prm.ToolInfo{
toolInfo := backend.ToolInfo{
Tool: cachedTool,
Args: additionalToolArgs,
}
settings := prm.OutputSettings{
settings := backend.OutputSettings{
ResultsView: resultsView,
OutputDir: path.Join(prmApi.CodeDir, ".prm-validate"),
}

err := prmApi.Validate([]prm.ToolInfo{toolInfo}, 1, settings)
validator := validate.Validator{
Backend: prmApi.Backend,
AFS: prmApi.AFS,
DirectoryPaths: backend.DirectoryPaths{CodeDir: prmApi.CodeDir, CacheDir: prmApi.CacheDir},
RunningConfig: prmApi.RunningConfig,
}

err := validator.Validate([]backend.ToolInfo{toolInfo}, 1, settings)
if err != nil {
return err
}
Expand All @@ -264,14 +275,14 @@ func execute(cmd *cobra.Command, args []string) error {
}

// Gather a list of tools
var toolList []prm.ToolInfo
var toolList []backend.ToolInfo
for _, tool := range toolGroup.Tools {
cachedTool, ok := prmApi.IsToolAvailable(tool.Name)
if !ok {
return fmt.Errorf("Tool %s not found in cache", tool)
}

info := prm.ToolInfo{Tool: cachedTool, Args: tool.Args}
info := backend.ToolInfo{Tool: cachedTool, Args: tool.Args}
toolList = append(toolList, info)
}

Expand All @@ -281,7 +292,15 @@ func execute(cmd *cobra.Command, args []string) error {
if isSerial || workerCount < 1 {
workerCount = 1
}
err = prmApi.Validate(toolList, workerCount, prm.OutputSettings{ResultsView: resultsView, OutputDir: outputDir})

validator := validate.Validator{
Backend: prmApi.Backend,
AFS: prmApi.AFS,
DirectoryPaths: backend.DirectoryPaths{CodeDir: prmApi.CodeDir, CacheDir: prmApi.CacheDir},
RunningConfig: prmApi.RunningConfig,
}

err = validator.Validate(toolList, workerCount, backend.OutputSettings{ResultsView: resultsView, OutputDir: outputDir})
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validate_test

import (
"bytes"
"github.com/puppetlabs/prm/pkg/config"
"io/ioutil"
"path"
"testing"
Expand Down Expand Up @@ -141,7 +142,7 @@ common:
prmObj := &prm.Prm{
AFS: &afero.Afero{Fs: fs},
IOFS: &afero.IOFS{Fs: fs},
RunningConfig: prm.Config{
RunningConfig: config.Config{
ToolPath: toolDir,
},
}
Expand Down
Loading

0 comments on commit 00f0db7

Please sign in to comment.