Files
purpleschool/go-demo-4/account/account.go
2025-03-08 21:18:39 +03:00

66 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package account
import (
"errors"
"fmt"
"github.com/fatih/color"
"math/rand/v2"
"net/url"
"time"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
type Account struct {
Login string `json:"login"`
Password string `json:"password"`
Url string `json:"url"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// метод вывода пароля
func (acc *Account) Output() {
c := color.New(color.FgRed, color.Italic, color.Bold)
c.Printf("password: %v, login: %v, URL: %v\n", acc.Password, acc.Login, acc.Url)
}
// метод генерации пароля
func (acc *Account) generatePassword(n int) {
res := make([]rune, n)
for i := range res {
res[i] = letterRunes[rand.IntN(len(letterRunes))]
}
acc.Password = string(res)
}
// Создание аккаунта с таймстамп
func NewAccount(login, password, urlString string) (*Account, error) {
_, err := url.ParseRequestURI(urlString)
if err != nil {
return nil, errors.New("INVALID_URL")
}
if login == "" {
return nil, errors.New("INVALID_LOGIN")
}
newAcc := &Account{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Login: login,
Password: password,
Url: urlString,
}
if password == "" {
newAcc.generatePassword(18)
}
return newAcc, nil
}
// функция введения данных
func PromptData(prompt string) string {
fmt.Print(prompt)
var res string
fmt.Scanln(&res)
return res
}