login / register cmd

This commit is contained in:
celogeek 2022-03-05 15:51:02 +01:00
parent b5e171739c
commit 79b084a83f
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
3 changed files with 59 additions and 18 deletions

View File

@ -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
} }

View File

@ -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 {

View File

@ -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", "", &register) parser.AddCommand("register", "Register", "", &registerCommand)
} }