2022-05-14 19:13:10 +02:00

51 lines
1.1 KiB
Go

package main
import (
"fmt"
"github.com/go-resty/resty/v2"
photosapi "gitlab.celogeek.com/photos/api/internal/photos/api"
)
type LoginCommand 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"`
}
func (c *LoginCommand) Execute(args []string) error {
logger.Printf("Login on %s...\n", c.Url)
cli := resty.New().SetBaseURL(c.Url)
resp, err := cli.
R().
SetBody(&photosapi.LoginRequest{
Login: c.Login,
Password: c.Password,
}).
SetResult(&photosapi.LoginResponse{}).
SetError(&photosapi.ErrorWithDetails{}).
Post("/account/login")
if err != nil {
return err
}
if resp.IsError() {
logger.Printf("Login failed!")
return resp.Error().(*photosapi.ErrorWithDetails)
}
logger.Println("Login succeed!")
if result, ok := resp.Result().(*photosapi.LoginResponse); ok {
fmt.Println(result.Token)
}
return nil
}
func init() {
parser.AddCommand("login", "Login", "", &LoginCommand{})
}