Add function transformation data on JSON
This commit is contained in:
@@ -1,34 +1,38 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
|
||||
|
||||
type Account struct {
|
||||
login string
|
||||
password string
|
||||
url string
|
||||
}
|
||||
|
||||
type AccountWithTimeStamp struct {
|
||||
createdAt time.Time
|
||||
updatedAt time.Time
|
||||
Account
|
||||
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) outputPassword() {
|
||||
fmt.Println(acc.login, acc.password, acc.url)
|
||||
func (acc *Account) OutputPassword() {
|
||||
c := color.New(color.FgRed, color.Italic, color.Bold)
|
||||
c.Printf("password: %v\n", acc.Password)
|
||||
}
|
||||
|
||||
func (acc AccountWithTimeStamp) OutputPassword() {
|
||||
fmt.Println(acc.login, acc.password, acc.url)
|
||||
func (acc *Account) ToBytes() ([]byte, error) {
|
||||
file, err := json.Marshal(acc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// метод генерации пароля
|
||||
@@ -37,11 +41,11 @@ func (acc *Account) generatePassword(n int) {
|
||||
for i := range res {
|
||||
res[i] = letterRunes[rand.IntN(len(letterRunes))]
|
||||
}
|
||||
acc.password = string(res)
|
||||
acc.Password = string(res)
|
||||
}
|
||||
|
||||
// Создание аккаунта с таймстамп
|
||||
func NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTimeStamp, error) {
|
||||
func NewAccount(login, password, urlString string) (*Account, error) {
|
||||
_, err := url.ParseRequestURI(urlString)
|
||||
if err != nil {
|
||||
return nil, errors.New("INVALID_URL")
|
||||
@@ -49,14 +53,12 @@ func NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTim
|
||||
if login == "" {
|
||||
return nil, errors.New("INVALID_LOGIN")
|
||||
}
|
||||
newAcc := &AccountWithTimeStamp{
|
||||
createdAt: time.Now(),
|
||||
updatedAt: time.Now(),
|
||||
Account: Account{
|
||||
login: login,
|
||||
password: password,
|
||||
url: urlString,
|
||||
},
|
||||
newAcc := &Account{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Login: login,
|
||||
Password: password,
|
||||
Url: urlString,
|
||||
}
|
||||
if password == "" {
|
||||
newAcc.generatePassword(18)
|
||||
|
||||
7
go-demo-4/data.json
Normal file
7
go-demo-4/data.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"login": "w@w.cc",
|
||||
"password": "LppJ-1#ki9-t0asxkx",
|
||||
"url": "https://w.cc",
|
||||
"createdAt": "2025-03-06T23:49:30.384007351+03:00",
|
||||
"updatedAt": "2025-03-06T23:49:30.384007481+03:00"
|
||||
}
|
||||
30
go-demo-4/files/files.go
Normal file
30
go-demo-4/files/files.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func FileWrite(content []byte, name string) {
|
||||
file, err := os.Create(name)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = file.Write(content)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Файл успешно записан")
|
||||
}
|
||||
|
||||
func FileRead() {
|
||||
data, err := os.ReadFile("test.txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
@@ -1,3 +1,11 @@
|
||||
module password
|
||||
|
||||
go 1.23.6
|
||||
|
||||
require github.com/fatih/color v1.18.0
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
)
|
||||
|
||||
11
go-demo-4/go.sum
Normal file
11
go-demo-4/go.sum
Normal file
@@ -0,0 +1,11 @@
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
@@ -3,17 +3,27 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"password/account"
|
||||
"password/files"
|
||||
)
|
||||
|
||||
func main() {
|
||||
createdAccount()
|
||||
}
|
||||
|
||||
func createdAccount() {
|
||||
login := account.PromptData("Введите логин: ")
|
||||
password := account.PromptData("Введите пароль: ")
|
||||
url := account.PromptData("Введите URL: ")
|
||||
|
||||
myAccount, err := account.NewAccountWithTimeStamp(login, password, url)
|
||||
myAccount, err := account.NewAccount(login, password, url)
|
||||
if err != nil {
|
||||
fmt.Println("ОШИБКА: Неверный формат URL")
|
||||
return
|
||||
}
|
||||
myAccount.OutputPassword()
|
||||
file, err := myAccount.ToBytes()
|
||||
if err != nil {
|
||||
fmt.Println("Не удалось преобразовать в JSON")
|
||||
return
|
||||
}
|
||||
files.FileWrite(file, "data.json")
|
||||
}
|
||||
|
||||
1
go-demo-4/test.txt
Normal file
1
go-demo-4/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
Hello! I'm file!
|
||||
Reference in New Issue
Block a user