diff --git a/src/cliAction/login/handler.go b/src/cliAction/login/handler.go index 4f5f1cba..53c3bb2b 100644 --- a/src/cliAction/login/handler.go +++ b/src/cliAction/login/handler.go @@ -27,7 +27,7 @@ type Config struct { } type RunConfig struct { - ZeropsLogin string + ZeropsEmail string ZeropsPassword string ZeropsToken string } @@ -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) } @@ -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 @@ -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 diff --git a/src/cmd/login.go b/src/cmd/login.go index fd633358..46d1da89 100644 --- a/src/cmd/login.go +++ b/src/cmd/login.go @@ -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" @@ -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) { @@ -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), @@ -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, }) }, } @@ -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 +} diff --git a/src/i18n/en.go b/src/i18n/en.go index f07cef04..e73015d8 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -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" diff --git a/src/zeropsApiProtocol/zeropsApiProtocol.pb.go b/src/zeropsApiProtocol/zeropsApiProtocol.pb.go index ca1d44b5..c41e6985 100644 --- a/src/zeropsApiProtocol/zeropsApiProtocol.pb.go +++ b/src/zeropsApiProtocol/zeropsApiProtocol.pb.go @@ -8,14 +8,15 @@ package zeropsApiProtocol import ( context "context" + reflect "reflect" + sync "sync" + proto "github.com/golang/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" ) const (