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
import (
"errors"
"fmt"
"github.com/go-resty/resty/v2"
)
type LoginCommand struct {
@ -12,10 +15,44 @@ type LoginCommand struct {
var loginCommand LoginCommand
func (c *LoginCommand) Execute(args []string) error {
fmt.Printf("Login on %s...\n", c.Url)
type LoginRequest struct {
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
}

View File

@ -1,6 +1,7 @@
package main
import (
"log"
"os"
"github.com/jessevdk/go-flags"
@ -9,9 +10,11 @@ import (
type Options struct {
}
var options Options
var parser = flags.NewParser(&options, flags.Default)
var (
options Options
parser = flags.NewParser(&options, flags.Default)
logger = log.New(os.Stderr, "", 0)
)
func main() {
if _, err := parser.Parse(); err != nil {

View File

@ -2,7 +2,6 @@ package main
import (
"errors"
"fmt"
"github.com/go-resty/resty/v2"
)
@ -13,7 +12,7 @@ type RegisterCommand struct {
Password string `short:"p" long:"password" description:"Password" required:"true"`
}
var register RegisterCommand
var registerCommand RegisterCommand
type RegisterRequest struct {
Login string `json:"login"`
@ -21,36 +20,38 @@ type RegisterRequest struct {
}
type RegisterResponse struct {
}
type RegisterError struct {
Error string
Status string
}
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.
R().
SetBody(&RegisterRequest{register.Login, register.Password}).
SetBody(&RegisterRequest{registerCommand.Login, registerCommand.Password}).
SetResult(&RegisterResponse{}).
SetError(&RegisterResponse{}).
SetError(&RegisterError{}).
Post("/account/signup")
if err != nil {
return err
}
if err, ok := resp.Error().(*RegisterResponse); ok {
fmt.Println("Registering failed!")
if err, ok := resp.Error().(*RegisterError); ok {
logger.Println("Registering failed!")
return errors.New(err.Error)
}
fmt.Println("Registering succeed!")
logger.Println("Registering succeed!")
return nil
}
func init() {
parser.AddCommand("register", "Register", "", &register)
parser.AddCommand("register", "Register", "", &registerCommand)
}