Skip to content

Commit

Permalink
2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
devemlight committed Mar 28, 2023
1 parent 4dbe2aa commit 6b24b4b
Show file tree
Hide file tree
Showing 44 changed files with 759 additions and 720 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.20

- name: Lint
uses: golangci/golangci-lint-action@v2
Expand All @@ -28,4 +28,4 @@ jobs:
run: go test -race -coverprofile=coverage.out -covermode=atomic -tags test ./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v2
46 changes: 6 additions & 40 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
linters:
enable:
- contextcheck
- depguard
- dogsled
- dupl
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- gofumpt
- goimports
- goprintffuncname
- gosec
- importas
- ireturn
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nolintlint
- prealloc
- revive
- stylecheck
- tparallel
- unconvert
- unparam
- whitespace
- wsl
enable-all: true

linters-settings:
gofumpt:
module-path: docker-color-output
module-path: docker-color-output
varnamelen:
ignore-names: [ i, in, j, m, sb, tt, v ]
wrapcheck:
ignorePackageGlobs: [ docker-color-output/internal/* ]
5 changes: 1 addition & 4 deletions bash/decorator.sh → bash/execute.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bash

# Usage:
# alias docker='bash decorator.sh docker'
# alias docker-compose='bash decorator.sh docker-compose'
# alias docker='bash execute.sh docker'
if [[ "$1" == "docker" ]]; then
if [[ "$2" == "ps" || "$2" == "images" ]]; then
"$@" | docker-color-output
Expand All @@ -11,8 +10,6 @@ if [[ "$1" == "docker" ]]; then
else
"$@"
fi
elif [[ "$1" == "docker-compose" && "$2" == "ps" ]]; then
"$@" | docker-color-output
else
"$@"
fi
23 changes: 17 additions & 6 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
package main

import (
"docker-color-output/internal/cli"
"os"

"docker-color-output/internal/app"
"docker-color-output/internal/stdin"
"docker-color-output/internal/stdout"
)

func main() {
if err := run(); err != nil {
app.Usage(err)
os.Exit(1)
}
}

func run() error {
in, err := stdin.Get()
if err != nil {
cli.Exit(err)
return err
}

out, err := cli.Execute(in)
rows, err := app.Run(in)
if err != nil {
cli.Exit(err)
return err
}

for _, v := range out {
stdout.Println(v)
for _, row := range rows {
stdout.Println(row)
}

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module docker-color-output

go 1.19
go 1.20
57 changes: 57 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package app

import (
"errors"
"strings"

"docker-color-output/internal/cmd"
"docker-color-output/internal/layout"
"docker-color-output/internal/util"
"docker-color-output/pkg/color"
)

var (
ErrNoFirstLine = errors.New("no first line")
ErrNullableColumns = errors.New("nullable columns more than one")
)

func Run(in []string) ([]string, error) {
if len(in) == 0 {
return nil, ErrNoFirstLine
}

header := layout.ParseHeader(in)
rows := layout.ParseRows(in, &header)

if header.NullableCols() > 1 {
return nil, ErrNullableColumns
}

command, err := cmd.Parse(header)
if err != nil {
return nil, err
}

res := make([]string, len(in))

// First line
var sb strings.Builder
for _, col := range header {
sb.WriteString(util.Pad(color.LightBlue(string(col.Name)), col.MaxLength))
}

res[0] = sb.String()

// Rows
for i, row := range rows {
sb.Reset()

for _, col := range header {
sb.WriteString(util.Pad(command.Format(row, col.Name), col.MaxLength))
}

res[i+1] = sb.String()
}

return res, nil
}
58 changes: 58 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package app_test

import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"docker-color-output/internal/app"
"docker-color-output/pkg/util/assert"
)

func TestRun(t *testing.T) {
t.Parallel()

read := func(filename string) []string {
_, b, _, _ := runtime.Caller(0)
path := filepath.Dir(b)
bytes, _ := os.ReadFile(path + "/../../test/data/" + filename)
res := strings.Split(string(bytes), "\n")
res = res[:len(res)-1]

if len(res) == 0 {
return nil
}

return res
}

tests := map[string]struct {
in string
want string
wantErr bool
}{
"no_stdin": {wantErr: true},
"no_first_line": {in: "no_first_line.out", wantErr: true},
"invalid_cols": {in: "invalid_cols.out", wantErr: true},
"docker_ps": {in: "docker_ps.out", want: "docker_ps.out"},
"docker_ps:custom_cols": {in: "docker_ps_custom_cols.out", want: "docker_ps_custom_cols.out"},
"docker_ps:nullable_col": {in: "docker_ps_nullable_col.out", want: "docker_ps_nullable_col.out"},
"docker_ps:nullable_cols": {in: "docker_ps_nullable_cols.out", wantErr: true},
"docker_images": {in: "docker_images.out", want: "docker_images.out"},
"docker_compose_ps": {in: "docker_compose_ps.out", want: "docker_compose_ps.out"},
"docker_compose_ps_1": {in: "docker_compose_ps_1.out", want: "docker_compose_ps_1.out"},
}

for name, tt := range tests {
tt := tt

t.Run(name, func(t *testing.T) {
t.Parallel()
rows, err := app.Run(read("in/" + tt.in))
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, read("out/"+tt.want), rows)
})
}
}
6 changes: 6 additions & 0 deletions internal/app/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

const (
Ver = "2.3.0"
Name = "docker-color-output"
)
19 changes: 19 additions & 0 deletions internal/app/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !test

package app

import (
"fmt"

"docker-color-output/pkg/color"
)

//nolint:forbidigo
func Usage(err error) {
fmt.Println(color.LightRed("💡 Error: " + err.Error()))
fmt.Println("💥 Version: " + color.Green(Ver))
fmt.Println("👌 Usage:")
fmt.Println(" " + color.Green("docker compose ps") + " [-a] | " + color.Brown(Name))
fmt.Println(" " + color.Green("docker images") + " [--format] | " + color.Brown(Name))
fmt.Println(" " + color.Green("docker ps") + " [-a] [--format] | " + color.Brown(Name))
}
31 changes: 0 additions & 31 deletions internal/cli/app.go

This file was deleted.

61 changes: 0 additions & 61 deletions internal/cli/app_test.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/cli/constants.go

This file was deleted.

21 changes: 0 additions & 21 deletions internal/cli/shutdown.go

This file was deleted.

Loading

0 comments on commit 6b24b4b

Please sign in to comment.