Skip to content

Commit

Permalink
Merge pull request #38 from zeropsio/login-rewrite
Browse files Browse the repository at this point in the history
Make login command to take positional arguments
  • Loading branch information
vnarek authored Jan 19, 2022
2 parents 5c3f358 + 1f91222 commit e4d9d21
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
14 changes: 3 additions & 11 deletions src/cliAction/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
}

type RunConfig struct {
ZeropsLogin string
ZeropsEmail string
ZeropsPassword string
ZeropsToken string
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func New(
func (h *Handler) Run(ctx context.Context, runConfig RunConfig) error {

if runConfig.ZeropsPassword == "" &&
runConfig.ZeropsLogin == "" &&
runConfig.ZeropsEmail == "" &&
runConfig.ZeropsToken == "" {
return errors.New(i18n.LoginParamsMissing)
}
Expand All @@ -68,7 +68,7 @@ func (h *Handler) Run(ctx context.Context, runConfig RunConfig) error {
if runConfig.ZeropsToken != "" {
err = h.loginWithToken(ctx, runConfig.ZeropsToken)
} else {
err = h.loginWithPassword(ctx, runConfig.ZeropsLogin, runConfig.ZeropsPassword)
err = h.loginWithPassword(ctx, runConfig.ZeropsEmail, runConfig.ZeropsPassword)
}
if err != nil {
return err
Expand All @@ -95,14 +95,6 @@ func (h *Handler) Run(ctx context.Context, runConfig RunConfig) error {
}

func (h *Handler) loginWithPassword(_ context.Context, login, password string) error {

if login == "" {
return errors.New(i18n.LoginZeropsLoginMissing)
}
if password == "" {
return errors.New(i18n.LoginZeropsPasswordMissing)
}

loginData, err := json.Marshal(struct {
Email string
Password string
Expand Down
30 changes: 25 additions & 5 deletions src/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"time"

"github.com/spf13/cobra"
"github.com/zerops-io/zcli/src/cliAction/login"

"github.com/spf13/cobra"
"github.com/zerops-io/zcli/src/constants"
"github.com/zerops-io/zcli/src/grpcApiClientFactory"
"github.com/zerops-io/zcli/src/grpcDaemonClientFactory"
Expand All @@ -15,7 +16,7 @@ import (

func loginCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Use: "login {token | username password}",
Short: i18n.CmdLogin,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -35,6 +36,8 @@ func loginCmd() *cobra.Command {
CaCertificateUrl: params.GetPersistentString(constants.PersistentParamCaCertificateUrl),
})

email, password, token := getCredentials(cmd, args)

return login.New(
login.Config{
RestApiAddress: params.GetPersistentString(constants.PersistentParamRestApiAddress),
Expand All @@ -45,9 +48,9 @@ func loginCmd() *cobra.Command {
apiClientFactory,
grpcDaemonClientFactory.New(),
).Run(ctx, login.RunConfig{
ZeropsLogin: params.GetString(cmd, "zeropsLogin"),
ZeropsPassword: params.GetString(cmd, "zeropsPassword"),
ZeropsToken: params.GetString(cmd, "zeropsToken"),
ZeropsEmail: email,
ZeropsPassword: password,
ZeropsToken: token,
})
},
}
Expand All @@ -58,3 +61,20 @@ func loginCmd() *cobra.Command {

return cmd
}

func getCredentials(cmd *cobra.Command, args []string) (login, password, token string) {
login = params.GetString(cmd, "zeropsLogin")
password = params.GetString(cmd, "zeropsPassword")
token = params.GetString(cmd, "zeropsToken")
if len(args) == 2 {
login = args[0]
password = args[1]
token = ""
}
if len(args) == 1 {
token = args[0]
login = ""
password = ""
}
return
}
8 changes: 3 additions & 5 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ const (
ZipClientPackingFile = "packing file: %s"

// login
LoginParamsMissing = "either zeropsLogin + zeropsPassword or zeropsToken params must be set"
LoginZeropsLoginMissing = "param zeropsLogin must be set"
LoginZeropsPasswordMissing = "param zeropsPassword must be set"
LoginSuccess = "you are logged in"
LoginVpnClosed = "vpn connection was closed"
LoginParamsMissing = "either login with password or token must be passed"
LoginSuccess = "you are logged in"
LoginVpnClosed = "vpn connection was closed"

// deploy
BuildDeployProjectNameMissing = "project name must be filled"
Expand Down
5 changes: 3 additions & 2 deletions src/zeropsApiProtocol/zeropsApiProtocol.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e4d9d21

Please sign in to comment.