-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from LerianStudio/feature/MIDAZ-307
Feature/MIDAZ-307
- Loading branch information
Showing
16 changed files
with
306 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
components/mdz/pkg/cmd/configure/testdata/output_configure.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.