Skip to content

Commit

Permalink
Merge pull request #8 from nixxxon/fix-linter-errors
Browse files Browse the repository at this point in the history
Fix linter errors
  • Loading branch information
nixxxon committed Jun 22, 2023
2 parents 1d3a105 + 6cbe337 commit b3c937f
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 51 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/pr_test.yml → .github/workflows/pr_audit.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: pr_test
name: pr_audit
on:
pull_request:
branches: [main]
jobs:
build:
name: Run tests
audit:
name: Audit
runs-on: ubuntu-latest
steps:
- name: Check out source code
Expand All @@ -24,5 +24,19 @@ jobs:
run: go install github.com/vektra/mockery/v2@v2.20.0
- name: Run go generate
run: go generate ./...
- name: Run go test
run: go test ./...
- name: Verify dependencies
run: go mod verify
- name: Build
run: go build -v ./...
- name: Run go vet
run: go vet ./...
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Run staticcheck
run: staticcheck ./...
- name: Install golint
run: go install golang.org/x/lint/golint@latest
- name: Run golint
run: golint -set_exit_status ./...
- name: Run tests
run: go test -race -vet=off ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
internal/prompt/mocks
internal/container/mocks
15 changes: 7 additions & 8 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"dex/internal/container"
"dex/internal/prompt"
"fmt"
"os"
"strings"

"github.com/containerd/console"

"dex/internal/container"
"dex/internal/prompt"
)

func main() {
Expand All @@ -35,8 +34,8 @@ func main() {
}

func run(
containerService container.ContainerService,
promptService prompt.PromptService,
containerService container.Service,
promptService prompt.Service,
) error {
containers, err := containerService.GetAll()
if err != nil {
Expand Down Expand Up @@ -74,8 +73,8 @@ func run(

func containersToPromptOptions(
dockerContainers []container.Container,
) []prompt.PromptOption {
var options []prompt.PromptOption
) []prompt.Option {
var options []prompt.Option

for _, container := range dockerContainers {
var names []string
Expand All @@ -90,7 +89,7 @@ func containersToPromptOptions(
container.ID[0:12],
)

option := prompt.PromptOption{
option := prompt.Option{
Label: label,
Value: container.ID,
}
Expand Down
52 changes: 26 additions & 26 deletions cmd/main/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import (
func TestRun(t *testing.T) {
t.Run("FailsIfErrorWhenGettingContainers", func(t *testing.T) {
expectedError := errors.New("Mock Error")
mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return([]container.Container{}, expectedError)

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)

actualError := run(mockContainerService, mockPromptService)

assert.Equal(t, expectedError, actualError)
})

t.Run("FailsSilentlyIfErrorWhenDisplayingContainerSelectPrompt", func(t *testing.T) {
mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return(containersData(), nil)

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)
mockPromptService.On("DisplaySelect", mock.Anything, promptOptionsData()).
Return(prompt.PromptOption{}, errors.New("Mock Error"))
Return(prompt.Option{}, errors.New("Mock Error"))
mockPromptService.AssertNotCalled(t, "DisplayPrompt", mock.Anything)

actualError := run(mockContainerService, mockPromptService)
Expand All @@ -42,13 +42,13 @@ func TestRun(t *testing.T) {
})

t.Run("FailsSilentlyIfErrorWhenDisplayingCommandPrompt", func(t *testing.T) {
mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return(containersData(), nil)
mockContainerService.AssertNotCalled(t, "RunCommand", mock.Anything, mock.Anything)

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)
mockPromptService.On("DisplaySelect", mock.Anything, promptOptionsData()).
Return(prompt.PromptOption{}, nil)
Return(prompt.Option{}, nil)
mockPromptService.On("DisplayPrompt", mock.Anything).
Return("", errors.New("Mock Error"))

Expand All @@ -60,16 +60,16 @@ func TestRun(t *testing.T) {
t.Run("NonEmptyCommandRunsSuccessfully", func(t *testing.T) {
expectedCommand := "ls -la"
promptOptions := promptOptionsData()
selectedPromptOption := promptOptions[0]
selectedOption := promptOptions[0]

mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return(containersData(), nil)
mockContainerService.On("RunCommand", selectedPromptOption.Value, expectedCommand).
mockContainerService.On("RunCommand", selectedOption.Value, expectedCommand).
Return(nil)

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)
mockPromptService.On("DisplaySelect", mock.Anything, promptOptions).
Return(selectedPromptOption, nil)
Return(selectedOption, nil)
mockPromptService.On("DisplayPrompt", mock.Anything).
Return(expectedCommand, nil)

Expand All @@ -81,16 +81,16 @@ func TestRun(t *testing.T) {
t.Run("EmptyCommandDefaultsToBash", func(t *testing.T) {
expectedCommand := "bash"
promptOptions := promptOptionsData()
selectedPromptOption := promptOptions[0]
selectedOption := promptOptions[0]

mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return(containersData(), nil)
mockContainerService.On("RunCommand", selectedPromptOption.Value, expectedCommand).
mockContainerService.On("RunCommand", selectedOption.Value, expectedCommand).
Return(nil)

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)
mockPromptService.On("DisplaySelect", mock.Anything, promptOptions).
Return(selectedPromptOption, nil)
Return(selectedOption, nil)
mockPromptService.On("DisplayPrompt", mock.Anything).
Return("", nil)

Expand All @@ -102,18 +102,18 @@ func TestRun(t *testing.T) {
t.Run("EmptyCommandAndFailedBashDefaultsToSh", func(t *testing.T) {
expectedCommand := "sh"
promptOptions := promptOptionsData()
selectedPromptOption := promptOptions[0]
selectedOption := promptOptions[0]

mockContainerService := containermocks.NewContainerService(t)
mockContainerService := containermocks.NewService(t)
mockContainerService.On("GetAll").Return(containersData(), nil)
mockContainerService.On("RunCommand", selectedPromptOption.Value, "bash").
mockContainerService.On("RunCommand", selectedOption.Value, "bash").
Return(errors.New("Mock Error")).Once()
mockContainerService.On("RunCommand", selectedPromptOption.Value, expectedCommand).
mockContainerService.On("RunCommand", selectedOption.Value, expectedCommand).
Return(nil).Once()

mockPromptService := promptmocks.NewPromptService(t)
mockPromptService := promptmocks.NewService(t)
mockPromptService.On("DisplaySelect", mock.Anything, promptOptions).
Return(selectedPromptOption, nil)
Return(selectedOption, nil)
mockPromptService.On("DisplayPrompt", mock.Anything).
Return("", nil)

Expand All @@ -123,8 +123,8 @@ func TestRun(t *testing.T) {
})
}

func promptOptionsData() []prompt.PromptOption {
return []prompt.PromptOption{
func promptOptionsData() []prompt.Option {
return []prompt.Option{
{
Label: "[containerName1] (containerImage1) aa64bf12226b",
Value: "aa64bf12226bc4dff14310a8fec7d5c5f0439ed2e69b3b590c413220650c0174",
Expand Down
7 changes: 5 additions & 2 deletions internal/container/container.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package container

// Container holds container information
type Container struct {
ID string
Names []string
Image string
}

//go:generate mockery --name ContainerService
type ContainerService interface {
// Service interface for getting containers and running commands against them
//
//go:generate mockery --name Service
type Service interface {
GetAll() ([]Container, error)
RunCommand(containerID, command string) error
Close()
Expand Down
7 changes: 6 additions & 1 deletion internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/docker/docker/client"
)

// DockerService for getting Docker containers and running commands against them
type DockerService struct {
client *client.Client
}

// GetAll Docker containers
func (s *DockerService) GetAll() ([]Container, error) {
var containers []Container

Expand All @@ -34,6 +36,7 @@ func (s *DockerService) GetAll() ([]Container, error) {
return containers, nil
}

// RunCommand runs a command in a Docker container
func (s *DockerService) RunCommand(containerID string, command string) error {
arguments, err := splitCommand(command)
if err != nil {
Expand Down Expand Up @@ -82,6 +85,7 @@ func (s *DockerService) RunCommand(containerID string, command string) error {
return err
}

// Close closes the client
func (s *DockerService) Close() {
s.client.Close()
}
Expand All @@ -96,7 +100,8 @@ func splitCommand(command string) ([]string, error) {
return arguments, nil
}

func NewDockerService() (ContainerService, error) {
// NewDockerService returns a new DockerService pointer
func NewDockerService() (Service, error) {
dockerClient, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
Expand Down
11 changes: 7 additions & 4 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package prompt

type PromptOption struct {
// Option for the select prompt
type Option struct {
Label string
Value string
}

//go:generate mockery --name PromptService
type PromptService interface {
DisplaySelect(label string, options []PromptOption) (PromptOption, error)
// Service interface for displaying prompts
//
//go:generate mockery --name Service
type Service interface {
DisplaySelect(label string, options []Option) (Option, error)
DisplayPrompt(label string) (string, error)
}
12 changes: 8 additions & 4 deletions internal/prompt/promptui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package prompt

import "github.com/manifoldco/promptui"

// PromptuiService for displaying prompts
type PromptuiService struct{}

// DisplaySelect displays a select prompt
func (p *PromptuiService) DisplaySelect(
label string,
options []PromptOption,
) (PromptOption, error) {
options []Option,
) (Option, error) {
labels := []string{}
for _, option := range options {
labels = append(labels, option.Label)
Expand All @@ -19,12 +21,13 @@ func (p *PromptuiService) DisplaySelect(
}
i, _, err := prompt.Run()
if err != nil {
return PromptOption{}, err
return Option{}, err
}

return options[i], nil
}

// DisplayPrompt displays a normal prompt
func (p *PromptuiService) DisplayPrompt(label string) (string, error) {
prompt := promptui.Prompt{
Label: label,
Expand All @@ -37,6 +40,7 @@ func (p *PromptuiService) DisplayPrompt(label string) (string, error) {
return result, nil
}

func NewPromptuiService() PromptService {
// NewPromptuiService returns a new PromptuiService pointer
func NewPromptuiService() Service {
return &PromptuiService{}
}
2 changes: 1 addition & 1 deletion release.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "1.0.1",
"version": "1.0.2",
"go_version": "1.20.4"
}

0 comments on commit b3c937f

Please sign in to comment.