Skip to content

Commit

Permalink
Merge pull request #333 from LerianStudio/feature/MIDAZ-307
Browse files Browse the repository at this point in the history
Feature/MIDAZ-307
  • Loading branch information
MartinezAvellan authored Nov 29, 2024
2 parents eda5c5b + 040790e commit 14c2bf4
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/update-env-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ jobs:
run: |
sed -i "s/^VERSION=.*/VERSION=${{ steps.latest_release.outputs.tag }}/" ./components/transaction/.env.example
- name: Update MDZ Env
if: steps.latest_release.outputs.tag != steps.env_release.outputs.version
run: |
sed -i "s/^VERSION=.*/VERSION=${{ steps.latest_release.outputs.tag }}/" ./components/mdz/.env.example
- name: Git Diff
if: steps.latest_release.outputs.tag != steps.env_release.outputs.version
run: |
Expand Down
1 change: 1 addition & 0 deletions components/mdz/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ CLIENT_ID=9670e0ca55a29a466d31
CLIENT_SECRET=dd03f916cacf4a98c6a413d9c38ba102dce436a9
URL_API_AUTH=http://127.0.0.1:8080
URL_API_LEDGER=http://127.0.0.1:3000
VERSION=v1.29.0
3 changes: 2 additions & 1 deletion components/mdz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ endif
LDFLAGS=-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.ClientID=$(CLIENT_ID)'\
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.ClientSecret=$(CLIENT_SECRET)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.URLAPIAuth=$(URL_API_AUTH)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.URLAPILedger=$(URL_API_LEDGER)'
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.URLAPILedger=$(URL_API_LEDGER)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.Version=$(VERSION)'

.PHONY: get-lint-deps
get-lint-deps:
Expand Down
152 changes: 152 additions & 0 deletions components/mdz/pkg/cmd/configure/configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package configure

import (
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/setting"
"github.com/LerianStudio/midaz/components/mdz/pkg/tui"
"github.com/fatih/color"
"github.com/rodaine/table"

"github.com/spf13/cobra"
)

type factoryConfigure struct {
factory *factory.Factory
tuiInput func(message string) (string, error)
read func() (*setting.Setting, error)
save func(sett setting.Setting) error
flagsConfigure
}

type flagsConfigure struct {
ClientID string
ClientSecret string
URLAPIAuth string
URLAPILedger string
JSONFile string
}

func (f *factoryConfigure) runE(cmd *cobra.Command, _ []string) error {
sett, err := f.read()
if err != nil {
return err
}

if !cmd.Flags().Changed("client-id") {
clientID, err := f.tuiInput("Enter your client-id")
if err != nil {
return err
}

f.ClientID = clientID
}

if len(f.ClientID) > 0 {
sett.ClientID = f.ClientID
}

if !cmd.Flags().Changed("client-secret") {
clientSecret, err := f.tuiInput("Enter your client-secret")
if err != nil {
return err
}

f.ClientSecret = clientSecret
}

if len(f.ClientSecret) > 0 {
sett.ClientSecret = f.ClientSecret
}

if !cmd.Flags().Changed("url-api-auth") {
urlAPIAuth, err := f.tuiInput("Enter your url-api-auth")
if err != nil {
return err
}

f.URLAPIAuth = urlAPIAuth
}

if len(f.URLAPIAuth) > 0 {
sett.URLAPIAuth = f.URLAPIAuth
}

if !cmd.Flags().Changed("url-api-ledger") {
urlAPILedger, err := f.tuiInput("Enter your url-api-ledger")
if err != nil {
return err
}

f.URLAPILedger = urlAPILedger
}

if len(f.URLAPILedger) > 0 {
sett.URLAPILedger = f.URLAPILedger
}

err = f.save(*sett)
if err != nil {
return err
}

tbl := table.New("FIELDS", "VALUES")

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)

tbl.AddRow("client-id:", f.ClientID)
tbl.AddRow("client-secret:", f.ClientSecret)
tbl.AddRow("url-api-auth:", f.URLAPIAuth)
tbl.AddRow("url-api-ledger:", f.URLAPILedger)

tbl.Print()

return nil
}

func (f *factoryConfigure) setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.ClientID, "client-id", "", "Unique client identifier used for authentication.")
cmd.Flags().StringVar(&f.ClientSecret, "client-secret", "", "Secret key used to validate the client's identity.")
cmd.Flags().StringVar(&f.URLAPIAuth, "url-api-auth", "", "URL of the authentication service.")
cmd.Flags().StringVar(&f.URLAPILedger, "url-api-ledger", "", "URL of the service responsible for the ledger.")
cmd.Flags().BoolP("help", "h", false, "Displays more information about the Mdz CLI")
}

func NewInjectFacConfigure(f *factory.Factory) *factoryConfigure {
return &factoryConfigure{
factory: f,
tuiInput: tui.Input,
read: setting.Read,
save: setting.Save,
}
}

func NewCmdConfigure(f *factoryConfigure) *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "Defines the service URLs and credentials for the mdz CLI environment (config: ~/.config/mdz/mdz.toml).",
Long: utils.Format(
"The mdz CLI configure command allows you to define the URL of the",
"service endpoint and the authentication credentials required to",
"access the Ledger environment. It offers simple and secure",
"configuration, which can be done interactively or directly via",
"command line arguments. Ideal for ensuring efficient integration",
"with the service.",
),
Example: utils.Format(
"$ mdz configure",
"$ mdz configure -h",
),
RunE: f.runE,
}

f.setFlags(cmd)

return cmd
}
63 changes: 63 additions & 0 deletions components/mdz/pkg/cmd/configure/configure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package configure

import (
"bytes"
"testing"

"github.com/LerianStudio/midaz/components/mdz/pkg/environment"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
"github.com/LerianStudio/midaz/components/mdz/pkg/setting"
"github.com/stretchr/testify/assert"
"gotest.tools/golden"
)

func TestNewCmdConfigure(t *testing.T) {
vlClientSecret := "secret123"
vlClientID := "1234"
vlURLAPIAuth := "http://localhost:8080"
vlURLAPILedger := "http://localhost:3000"

confFactory := factoryConfigure{
factory: &factory.Factory{IOStreams: &iostreams.IOStreams{
Out: &bytes.Buffer{},
Err: &bytes.Buffer{},
}},
tuiInput: func(message string) (string, error) {
return vlClientSecret, nil
},
read: func() (*setting.Setting, error) {
return &setting.Setting{
Env: environment.Env{
ClientID: "",
ClientSecret: "",
URLAPIAuth: "",
URLAPILedger: "",
},
}, nil
},
save: func(sett setting.Setting) error {
return nil
},
flagsConfigure: flagsConfigure{
ClientID: vlClientID,
ClientSecret: vlClientSecret,
URLAPIAuth: vlURLAPIAuth,
URLAPILedger: vlURLAPILedger,
},
}

cmd := NewCmdConfigure(&confFactory)
cmd.SetArgs([]string{
"--client-id", vlClientID,
"--url-api-auth", vlURLAPIAuth,
"--url-api-ledger", vlURLAPILedger,
})

err := cmd.Execute()
assert.NoError(t, err)

output := confFactory.factory.IOStreams.Out.(*bytes.Buffer).Bytes()
golden.AssertBytes(t, output, "output_configure.golden")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FIELDS VALUES
client-id: 1234
client-secret: secret123
url-api-auth: http://localhost:8080
url-api-ledger: http://localhost:3000
8 changes: 4 additions & 4 deletions components/mdz/pkg/cmd/ledger/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package ledger

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

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

"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"
"gotest.tools/golden"
)

func Test_newCmdLedgerDescribe(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions components/mdz/pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ func (l *factoryLogin) runE(cmd *cobra.Command, _ []string) error {
}
}

sett := setting.Setting{
Token: l.token,
sett, err := setting.Read()
if err != nil {
return err
}

if err := setting.Save(sett); err != nil {
sett.Token = l.token

if err := setting.Save(*sett); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion components/mdz/pkg/cmd/root/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (f *factoryRoot) buildHelpEntries(command *cobra.Command, baseCommands, sub
longText = command.Short
}

helpEntries = append(helpEntries, helpEntry{"", color.New(color.Bold).Sprint(f.factory.CLIVersion)})
helpEntries = append(helpEntries, helpEntry{"", color.New(color.Bold).Sprint(f.factory.Env.Version)})
if longText != "" {
helpEntries = append(helpEntries, helpEntry{
Title: color.New(color.Bold).Sprint("DESCRIPTION"),
Expand Down
45 changes: 36 additions & 9 deletions components/mdz/pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/account"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/asset"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/configure"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/ledger"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/login"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/organization"
Expand Down Expand Up @@ -32,6 +33,7 @@ func (f *factoryRoot) setCmds(cmd *cobra.Command) {
cmd.AddCommand(portfolio.NewCmdPortfolio(f.factory))
cmd.AddCommand(product.NewCmdProduct(f.factory))
cmd.AddCommand(account.NewCmdAccount(f.factory))
cmd.AddCommand(configure.NewCmdConfigure(configure.NewInjectFacConfigure(f.factory)))
}

func (f *factoryRoot) setFlags(cmd *cobra.Command) {
Expand All @@ -40,14 +42,39 @@ func (f *factoryRoot) setFlags(cmd *cobra.Command) {
}

func (f *factoryRoot) persistentPreRunE(cmd *cobra.Command, _ []string) error {
if cmd.Name() != "login" &&
cmd.Name() != "completion" &&
cmd.Name() != "version" {
sett, err := setting.Read()
if err != nil {
return errors.New("Try the login command first " + err.Error())
}
name := cmd.Name()

if name == "completion" ||
name == "bash" ||
name == "fish" ||
name == "powershell" ||
name == "zsh" ||
name == "version" {
return nil
}

sett, err := setting.Read()
if err != nil {
return errors.New("Try the login command first 'mdz login -h' " + err.Error())
}

if len(sett.Env.ClientID) > 0 {
f.factory.Env.ClientID = sett.ClientID
}

if len(sett.Env.ClientSecret) > 0 {
f.factory.Env.ClientSecret = sett.ClientSecret
}

if len(sett.Env.URLAPIAuth) > 0 {
f.factory.Env.URLAPIAuth = sett.URLAPIAuth
}

if len(sett.Env.URLAPILedger) > 0 {
f.factory.Env.URLAPILedger = sett.URLAPILedger
}

if len(sett.Token) > 0 {
f.factory.Token = sett.Token
}

Expand All @@ -66,7 +93,7 @@ func NewCmdRoot(f *factory.Factory) *cobra.Command {
"Midaz is an open-source ledger designed to offer multi-asset and multi-currency",
"transaction capabilities within a single, natively immutable and fully auditable platform.",
),
Version: fRoot.factory.CLIVersion,
Version: fRoot.factory.Env.Version,
Example: utils.Format(
"$ mdz",
"$ mdz -h",
Expand All @@ -85,7 +112,7 @@ func NewCmdRoot(f *factory.Factory) *cobra.Command {
fRoot.help(cmd, args)
})

cmd.SetVersionTemplate(color.New(color.Bold).Sprint(fRoot.factory.CLIVersion))
cmd.SetVersionTemplate(color.New(color.Bold).Sprint(fRoot.factory.Env.Version))
fRoot.setCmds(cmd)
fRoot.setFlags(cmd)

Expand Down
3 changes: 2 additions & 1 deletion components/mdz/pkg/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"github.com/LerianStudio/midaz/components/mdz/pkg/environment"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"

Expand Down Expand Up @@ -35,7 +36,7 @@ func TestNewCmdRoot(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
f := &factory.Factory{
CLIVersion: tt.version,
Env: &environment.Env{Version: tt.version},
IOStreams: &iostreams.IOStreams{
Out: outBuf,
Err: errBuf,
Expand Down
Loading

0 comments on commit 14c2bf4

Please sign in to comment.