2022-03-05 15:51:02 +01:00

58 lines
1.2 KiB
Go

package main
import (
"errors"
"github.com/go-resty/resty/v2"
)
type RegisterCommand struct {
Url string `short:"u" long:"url" description:"Url of the instance" required:"true"`
Login string `short:"l" long:"login" description:"Login" required:"true"`
Password string `short:"p" long:"password" description:"Password" required:"true"`
}
var registerCommand RegisterCommand
type RegisterRequest struct {
Login string `json:"login"`
Password string `json:"password"`
}
type RegisterResponse struct {
}
type RegisterError struct {
Error string
}
func (c *RegisterCommand) Execute(args []string) error {
logger.Printf("Registering on %s...\n", c.Url)
cli := resty.New().SetBaseURL(registerCommand.Url)
resp, err := cli.
R().
SetBody(&RegisterRequest{registerCommand.Login, registerCommand.Password}).
SetResult(&RegisterResponse{}).
SetError(&RegisterError{}).
Post("/account/signup")
if err != nil {
return err
}
if err, ok := resp.Error().(*RegisterError); ok {
logger.Println("Registering failed!")
return errors.New(err.Error)
}
logger.Println("Registering succeed!")
return nil
}
func init() {
parser.AddCommand("register", "Register", "", &registerCommand)
}