Skip to content

Commit

Permalink
feat: format output colors and set flag global no-color ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed Nov 27, 2024
1 parent 4e7f54c commit 7fae4c0
Show file tree
Hide file tree
Showing 51 changed files with 203 additions and 137 deletions.
4 changes: 1 addition & 3 deletions components/mdz/pkg/cmd/account/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package account
import (
"encoding/json"
"errors"
"fmt"
"strconv"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
Expand Down Expand Up @@ -92,8 +91,7 @@ func (f *factoryAccountCreate) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Account ID %s has been successfully created", resp.ID))
output.FormatAndPrint(f.factory, resp.ID, "Account", output.Created)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions components/mdz/pkg/cmd/account/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package account

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -92,5 +93,5 @@ func Test_newCmdAccountCreate(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Account ID 01933f96-ed04-7c57-be5b-c091388830f8 has been successfully created")
assert.Contains(t, output, "The Account 01933f96-ed04-7c57-be5b-c091388830f8 has been successfully created.")
}
5 changes: 1 addition & 4 deletions components/mdz/pkg/cmd/account/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package account

import (
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils"
Expand Down Expand Up @@ -73,8 +71,7 @@ func (f *factoryAccountDelete) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Account ID %s has been successfully deleted.", f.AccountID))
output.FormatAndPrint(f.factory, f.AccountID, "Account", output.Deleted)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions components/mdz/pkg/cmd/account/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package account

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -43,5 +44,5 @@ func Test_newCmdAccountDelete(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Account ID 444 has been successfully deleted.")
assert.Contains(t, output, "The Account 444 has been successfully deleted.")
}
8 changes: 5 additions & 3 deletions components/mdz/pkg/cmd/account/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ func (f *factoryAccountDescribe) outputAccount(cmd *cobra.Command, account *mmod
func (f *factoryAccountDescribe) describePrint(account *mmodel.Account) {
tbl := table.New("FIELDS", "VALUES")

headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
if !f.factory.NoColor {
headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
}

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
tbl.WithWriter(f.factory.IOStreams.Out)

tbl.AddRow("ID:", account.ID)
Expand Down
10 changes: 6 additions & 4 deletions components/mdz/pkg/cmd/account/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ func (f *factoryAccountList) runE(cmd *cobra.Command, _ []string) error {
return nil
}

headerFmt := color.New(color.FgYellow).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New(
"ID",
"NAME",
Expand All @@ -82,7 +79,12 @@ func (f *factoryAccountList) runE(cmd *cobra.Command, _ []string) error {
"CREATED_AT",
)

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
if !f.factory.NoColor {
headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
}

tbl.WithWriter(f.factory.IOStreams.Out)

for _, i := range accounts.Items {
Expand Down
4 changes: 1 addition & 3 deletions components/mdz/pkg/cmd/account/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package account
import (
"encoding/json"
"errors"
"fmt"
"strconv"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
Expand Down Expand Up @@ -106,8 +105,7 @@ func (f *factoryAccountUpdate) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Account ID %s has been successfully updated.", resp.ID))
output.FormatAndPrint(f.factory, resp.ID, "Account", output.Updated)

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions components/mdz/pkg/cmd/account/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package account

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
"github.com/LerianStudio/midaz/components/mdz/pkg/ptr"
"github.com/LerianStudio/midaz/pkg/mmodel"

"github.com/stretchr/testify/assert"
)

func Test_newCmdLedgerUpdate(t *testing.T) {
Expand Down Expand Up @@ -71,5 +71,5 @@ func Test_newCmdLedgerUpdate(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Account ID 777 has been successfully updated.")
assert.Contains(t, output, "The Account 777 has been successfully updated.")
}
4 changes: 1 addition & 3 deletions components/mdz/pkg/cmd/asset/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package asset
import (
"encoding/json"
"errors"
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
Expand Down Expand Up @@ -76,8 +75,7 @@ func (f *factoryAssetCreate) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Asset ID %s has been successfully created", resp.ID))
output.FormatAndPrint(f.factory, resp.ID, "Asset", output.Created)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions components/mdz/pkg/cmd/asset/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package asset

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -89,5 +90,5 @@ func Test_newCmdAssetCreate(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Asset ID 01930219-2c25-7a37-a5b9-610d44ae0a27 has been successfully created")
assert.Contains(t, output, "The Asset 01930219-2c25-7a37-a5b9-610d44ae0a27 has been successfully created.")
}
5 changes: 1 addition & 4 deletions components/mdz/pkg/cmd/asset/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package asset

import (
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils"
Expand Down Expand Up @@ -63,8 +61,7 @@ func (f *factoryAssetDelete) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Asset ID %s has been successfully deleted.", f.AssetID))
output.FormatAndPrint(f.factory, f.AssetID, "Asset", output.Deleted)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions components/mdz/pkg/cmd/asset/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package asset

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -42,5 +43,5 @@ func Test_newCmdAssetDelete(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Asset ID 444 has been successfully deleted.")
assert.Contains(t, output, "The Asset 444 has been successfully deleted.")
}
8 changes: 5 additions & 3 deletions components/mdz/pkg/cmd/asset/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ func (f *factoryAssetDescribe) outputAsset(cmd *cobra.Command, asset *mmodel.Ass
func (f *factoryAssetDescribe) describePrint(asset *mmodel.Asset) {
tbl := table.New("FIELDS", "VALUES")

headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
if !f.factory.NoColor {
headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
}

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
tbl.WithWriter(f.factory.IOStreams.Out)

tbl.AddRow("ID:", asset.ID)
Expand Down
10 changes: 6 additions & 4 deletions components/mdz/pkg/cmd/asset/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func (f *factoryAssetList) runE(cmd *cobra.Command, _ []string) error {
return nil
}

headerFmt := color.New(color.FgYellow).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New(
"ID",
"NAME",
Expand All @@ -73,7 +70,12 @@ func (f *factoryAssetList) runE(cmd *cobra.Command, _ []string) error {
"CREATED_AT",
)

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
if !f.factory.NoColor {
headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
}

tbl.WithWriter(f.factory.IOStreams.Out)

for _, i := range leds.Items {
Expand Down
4 changes: 1 addition & 3 deletions components/mdz/pkg/cmd/asset/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package asset
import (
"encoding/json"
"errors"
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
Expand Down Expand Up @@ -91,8 +90,7 @@ func (f *factoryAssetUpdate) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Asset ID %s has been successfully updated.", resp.ID))
output.FormatAndPrint(f.factory, resp.ID, "Asset", output.Updated)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions components/mdz/pkg/cmd/asset/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package asset

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -70,5 +71,5 @@ func Test_newCmdLedgerUpdate(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Asset ID 412 has been successfully updated.")
assert.Contains(t, output, "The Asset 412 has been successfully updated.")
}
4 changes: 1 addition & 3 deletions components/mdz/pkg/cmd/ledger/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ledger
import (
"encoding/json"
"errors"
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
Expand Down Expand Up @@ -63,8 +62,7 @@ func (f *factoryLedgerCreate) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The ledger_id %s has been successfully created", resp.ID))
output.FormatAndPrint(f.factory, resp.ID, "Ledger", output.Created)

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions components/mdz/pkg/cmd/ledger/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ledger

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down Expand Up @@ -76,6 +77,5 @@ func Test_newCmdLedgerCreate(t *testing.T) {
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The ledger_id 0192e251-328d-7390-99f5-5c54980115ed has been successfully created")

assert.Contains(t, output, "The Ledger 0192e251-328d-7390-99f5-5c54980115ed has been successfully created.")
}
5 changes: 1 addition & 4 deletions components/mdz/pkg/cmd/ledger/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ledger

import (
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/internal/rest"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils"
Expand Down Expand Up @@ -45,8 +43,7 @@ func (f *factoryLedgerDelete) runE(cmd *cobra.Command, _ []string) error {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Ledger %s has been successfully deleted.", f.ledgerID))
output.FormatAndPrint(f.factory, f.ledgerID, "Ledger", output.Deleted)

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion components/mdz/pkg/cmd/ledger/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ledger

import (
"bytes"
"go.uber.org/mock/gomock"
"testing"

"go.uber.org/mock/gomock"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
Expand Down
8 changes: 5 additions & 3 deletions components/mdz/pkg/cmd/ledger/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ func (f *factoryLedgerDescribe) runE(cmd *cobra.Command, _ []string) error {
func (f *factoryLedgerDescribe) describePrint(led *mmodel.Ledger) {
tbl := table.New("FIELDS", "VALUES")

headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
if !f.factory.NoColor {
headerFmt := color.New(color.FgYellow).SprintfFunc()
fieldFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
}

tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(fieldFmt)
tbl.WithWriter(f.factory.IOStreams.Out)

tbl.AddRow("ID:", led.ID)
Expand Down
Loading

0 comments on commit 7fae4c0

Please sign in to comment.