Split main file on two packet
This commit is contained in:
73
go-demo-4/account/account.go
Normal file
73
go-demo-4/account/account.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package account
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/rand/v2"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
|
||||||
|
|
||||||
|
type Account struct {
|
||||||
|
login string
|
||||||
|
password string
|
||||||
|
url string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AccountWithTimeStamp struct {
|
||||||
|
createdAt time.Time
|
||||||
|
updatedAt time.Time
|
||||||
|
Account
|
||||||
|
}
|
||||||
|
|
||||||
|
// метод вывода пароля
|
||||||
|
func (acc Account) outputPassword() {
|
||||||
|
fmt.Println(acc.login, acc.password, acc.url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (acc AccountWithTimeStamp) OutputPassword() {
|
||||||
|
fmt.Println(acc.login, acc.password, 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 NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTimeStamp, error) {
|
||||||
|
_, err := url.ParseRequestURI(urlString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("INVALID_URL")
|
||||||
|
}
|
||||||
|
if login == "" {
|
||||||
|
return nil, errors.New("INVALID_LOGIN")
|
||||||
|
}
|
||||||
|
newAcc := &AccountWithTimeStamp{
|
||||||
|
createdAt: time.Now(),
|
||||||
|
updatedAt: time.Now(),
|
||||||
|
Account: Account{
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,106 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand/v2"
|
"password/account"
|
||||||
"net/url"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type account struct {
|
|
||||||
login string
|
|
||||||
password string
|
|
||||||
url string
|
|
||||||
}
|
|
||||||
|
|
||||||
type accountWithTimeStamp struct {
|
|
||||||
createdAt time.Time
|
|
||||||
updatedAt time.Time
|
|
||||||
account
|
|
||||||
}
|
|
||||||
|
|
||||||
// метод вывода пароля
|
|
||||||
func (acc account) outputPassword() {
|
|
||||||
fmt.Println(acc.login, acc.password, acc.url)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (acc accountWithTimeStamp) outputPassword() {
|
|
||||||
fmt.Println(acc.login, acc.password, acc.url, acc.createdAt, acc.updatedAt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// метод генерации пароля
|
|
||||||
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 newAccountWithTimeStamp(login, password, urlString string) (*accountWithTimeStamp, error) {
|
|
||||||
_, err := url.ParseRequestURI(urlString)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("INVALID_URL")
|
|
||||||
}
|
|
||||||
if login == "" {
|
|
||||||
return nil, errors.New("INVALID_LOGIN")
|
|
||||||
}
|
|
||||||
newAcc := &accountWithTimeStamp{
|
|
||||||
createdAt: time.Now(),
|
|
||||||
updatedAt: time.Now(),
|
|
||||||
account: account{
|
|
||||||
login: login,
|
|
||||||
password: password,
|
|
||||||
url: urlString,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if password == "" {
|
|
||||||
newAcc.generatePassword(18)
|
|
||||||
}
|
|
||||||
return newAcc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// создание аккаунта
|
|
||||||
/* 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{
|
|
||||||
login: login,
|
|
||||||
password: password,
|
|
||||||
url: urlString,
|
|
||||||
}
|
|
||||||
if password == "" {
|
|
||||||
newAcc.generatePassword(18)
|
|
||||||
}
|
|
||||||
return newAcc, nil
|
|
||||||
} */
|
|
||||||
|
|
||||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
login := promptData("Введите логин: ")
|
login := account.PromptData("Введите логин: ")
|
||||||
password := promptData("Введите пароль: ")
|
password := account.PromptData("Введите пароль: ")
|
||||||
url := promptData("Введите URL: ")
|
url := account.PromptData("Введите URL: ")
|
||||||
|
|
||||||
myAccount, err := newAccountWithTimeStamp(login, password, url)
|
myAccount, err := account.NewAccountWithTimeStamp(login, password, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ОШИБКА: Неверный формат URL")
|
fmt.Println("ОШИБКА: Неверный формат URL")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
myAccount.outputPassword()
|
myAccount.OutputPassword()
|
||||||
}
|
|
||||||
|
|
||||||
// функция введения данных
|
|
||||||
func promptData(prompt string) string {
|
|
||||||
fmt.Print(prompt)
|
|
||||||
var res string
|
|
||||||
fmt.Scanln(&res)
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user