login / register cmd
This commit is contained in:
parent
b5e171739c
commit
79b084a83f
@ -1,7 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoginCommand struct {
|
type LoginCommand struct {
|
||||||
@ -12,10 +15,44 @@ type LoginCommand struct {
|
|||||||
|
|
||||||
var loginCommand LoginCommand
|
var loginCommand LoginCommand
|
||||||
|
|
||||||
func (c *LoginCommand) Execute(args []string) error {
|
type LoginRequest struct {
|
||||||
fmt.Printf("Login on %s...\n", c.Url)
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Login succeed!")
|
type LoginError struct {
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginResponse struct {
|
||||||
|
Token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LoginCommand) Execute(args []string) error {
|
||||||
|
logger.Printf("Login on %s...\n", c.Url)
|
||||||
|
|
||||||
|
cli := resty.New().SetBaseURL(loginCommand.Url)
|
||||||
|
|
||||||
|
resp, err := cli.
|
||||||
|
R().
|
||||||
|
SetBody(&LoginRequest{loginCommand.Login, loginCommand.Password}).
|
||||||
|
SetResult(&LoginResponse{}).
|
||||||
|
SetError(&LoginError{}).
|
||||||
|
Post("/account/login")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := resp.Error().(*LoginError); ok {
|
||||||
|
logger.Printf("Login failed!")
|
||||||
|
return errors.New(err.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Println("Login succeed!")
|
||||||
|
if result, ok := resp.Result().(*LoginResponse); ok {
|
||||||
|
fmt.Println(result.Token)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
@ -9,9 +10,11 @@ import (
|
|||||||
type Options struct {
|
type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var options Options
|
var (
|
||||||
|
options Options
|
||||||
var parser = flags.NewParser(&options, flags.Default)
|
parser = flags.NewParser(&options, flags.Default)
|
||||||
|
logger = log.New(os.Stderr, "", 0)
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if _, err := parser.Parse(); err != nil {
|
if _, err := parser.Parse(); err != nil {
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
@ -13,7 +12,7 @@ type RegisterCommand struct {
|
|||||||
Password string `short:"p" long:"password" description:"Password" required:"true"`
|
Password string `short:"p" long:"password" description:"Password" required:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var register RegisterCommand
|
var registerCommand RegisterCommand
|
||||||
|
|
||||||
type RegisterRequest struct {
|
type RegisterRequest struct {
|
||||||
Login string `json:"login"`
|
Login string `json:"login"`
|
||||||
@ -21,36 +20,38 @@ type RegisterRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RegisterResponse struct {
|
type RegisterResponse struct {
|
||||||
Error string
|
}
|
||||||
Status string
|
|
||||||
|
type RegisterError struct {
|
||||||
|
Error string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RegisterCommand) Execute(args []string) error {
|
func (c *RegisterCommand) Execute(args []string) error {
|
||||||
fmt.Printf("Registering on %s...\n", c.Url)
|
logger.Printf("Registering on %s...\n", c.Url)
|
||||||
|
|
||||||
cli := resty.New().SetBaseURL(register.Url)
|
cli := resty.New().SetBaseURL(registerCommand.Url)
|
||||||
|
|
||||||
resp, err := cli.
|
resp, err := cli.
|
||||||
R().
|
R().
|
||||||
SetBody(&RegisterRequest{register.Login, register.Password}).
|
SetBody(&RegisterRequest{registerCommand.Login, registerCommand.Password}).
|
||||||
SetResult(&RegisterResponse{}).
|
SetResult(&RegisterResponse{}).
|
||||||
SetError(&RegisterResponse{}).
|
SetError(&RegisterError{}).
|
||||||
Post("/account/signup")
|
Post("/account/signup")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err, ok := resp.Error().(*RegisterResponse); ok {
|
if err, ok := resp.Error().(*RegisterError); ok {
|
||||||
fmt.Println("Registering failed!")
|
logger.Println("Registering failed!")
|
||||||
return errors.New(err.Error)
|
return errors.New(err.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Registering succeed!")
|
logger.Println("Registering succeed!")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
parser.AddCommand("register", "Register", "", ®ister)
|
parser.AddCommand("register", "Register", "", ®isterCommand)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user