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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petergmurphy committed Jun 15, 2022
1 parent 7928adf commit a33eef2
Show file tree
Hide file tree
Showing 21 changed files with 174 additions and 148 deletions.
8 changes: 5 additions & 3 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"
"github.com/puppetlabs/prm/pkg/backend/docker"
"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 backend.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
15 changes: 8 additions & 7 deletions cmd/set/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package set

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

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

var SelectedBackend prm.BackendType
var SelectedBackend backend.BackendType

func (sc *SetCommand) createSetBackendCommand() *cobra.Command {
tmp := &cobra.Command{
Expand All @@ -17,26 +18,26 @@ 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(backend.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", backend.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", backend.DOCKER)
}

switch strings.ToLower(args[0]) {
case string(prm.DOCKER):
SelectedBackend = prm.DOCKER
case string(backend.DOCKER):
SelectedBackend = backend.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], backend.DOCKER)
}

return nil
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/backend"
"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 backend.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: backend.DOCKER,
},
{
name: "Should handle valid backend selection (dOcKeR)",
args: []string{"backend", "dOcKeR"},
expectedBackedType: prm.DOCKER,
expectedBackedType: backend.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", backend.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", backend.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", backend.DOCKER),
expectError: true,
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package status

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

"github.com/puppetlabs/prm/pkg/prm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,7 +37,7 @@ 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
}
Expand Down
20 changes: 11 additions & 9 deletions cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package validate

import (
"fmt"
"github.com/puppetlabs/prm/pkg/backend"
"github.com/puppetlabs/prm/pkg/backend/docker"
"os"
"os/user"
"path"
Expand Down Expand Up @@ -127,10 +129,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 backend.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 @@ -233,16 +235,16 @@ 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)
err := prmApi.Validate([]backend.ToolInfo{toolInfo}, 1, settings)
if err != nil {
return err
}
Expand All @@ -264,14 +266,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 +283,7 @@ 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})
err = prmApi.Validate(toolList, workerCount, backend.OutputSettings{ResultsView: resultsView, OutputDir: outputDir})
if err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions internal/pkg/mock/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mock

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

"github.com/puppetlabs/prm/pkg/prm"
)
Expand All @@ -14,8 +16,8 @@ type MockBackend struct {
ValidateReturn string
}

func (m *MockBackend) Status() prm.BackendStatus {
return prm.BackendStatus{IsAvailable: m.StatusIsAvailable, StatusMsg: m.StatusMessageString}
func (m *MockBackend) Status() backend.BackendStatus {
return backend.BackendStatus{IsAvailable: m.StatusIsAvailable, StatusMsg: m.StatusMessageString}
}

func (m *MockBackend) GetTool(tool *prm.Tool, prmConfig prm.Config) error {
Expand All @@ -27,7 +29,7 @@ func (m *MockBackend) GetTool(tool *prm.Tool, prmConfig prm.Config) error {
}

// Implement when needed
func (m *MockBackend) Validate(toolInfo prm.ToolInfo, prmConfig prm.Config, paths prm.DirectoryPaths) (prm.ValidateExitCode, string, error) {
func (m *MockBackend) Validate(toolInfo backend.ToolInfo, prmConfig prm.Config, paths backend.DirectoryPaths) (prm.ValidateExitCode, string, error) {
switch m.ValidateReturn {
case "PASS":
return prm.VALIDATION_PASS, "", nil
Expand All @@ -40,7 +42,7 @@ func (m *MockBackend) Validate(toolInfo prm.ToolInfo, prmConfig prm.Config, path
}
}

func (m *MockBackend) Exec(tool *prm.Tool, args []string, prmConfig prm.Config, paths prm.DirectoryPaths) (prm.ToolExitCode, error) {
func (m *MockBackend) Exec(tool *prm.Tool, args []string, prmConfig prm.Config, paths backend.DirectoryPaths) (prm.ToolExitCode, error) {
switch m.ExecReturn {
case "SUCCESS":
return prm.SUCCESS, nil
Expand All @@ -51,6 +53,6 @@ func (m *MockBackend) Exec(tool *prm.Tool, args []string, prmConfig prm.Config,
case "TOOL_NOT_FOUND":
return prm.TOOL_NOT_FOUND, nil
default:
return prm.FAILURE, prm.ErrDockerNotRunning
return prm.FAILURE, docker.ErrDockerNotRunning
}
}
12 changes: 7 additions & 5 deletions pkg/prm/backend.go → pkg/backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//nolint:structcheck,unused
package prm
package backend

import "github.com/puppetlabs/prm/pkg/prm"

type BackendType string

Expand All @@ -8,9 +10,9 @@ const (
)

type BackendI interface {
GetTool(tool *Tool, prmConfig Config) error
Validate(toolInfo ToolInfo, prmConfig Config, paths DirectoryPaths) (ValidateExitCode, string, error)
Exec(tool *Tool, args []string, prmConfig Config, paths DirectoryPaths) (ToolExitCode, error)
GetTool(tool *prm.Tool, prmConfig prm.Config) error
Validate(toolInfo ToolInfo, prmConfig prm.Config, paths DirectoryPaths) (ValidateExitCode, string, error)
Exec(tool *prm.Tool, args []string, prmConfig prm.Config, paths DirectoryPaths) (prm.ToolExitCode, error)
Status() BackendStatus
}

Expand All @@ -33,7 +35,7 @@ type OutputSettings struct {
}

type ToolInfo struct {
Tool *Tool
Tool *prm.Tool
Args []string
}

Expand Down
Loading

0 comments on commit a33eef2

Please sign in to comment.