Add function transformation data on JSON
This commit is contained in:
@@ -1,34 +1,38 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
login string
|
Login string `json:"login"`
|
||||||
password string
|
Password string `json:"password"`
|
||||||
url string
|
Url string `json:"url"`
|
||||||
}
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
type AccountWithTimeStamp struct {
|
|
||||||
createdAt time.Time
|
|
||||||
updatedAt time.Time
|
|
||||||
Account
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// метод вывода пароля
|
// метод вывода пароля
|
||||||
func (acc Account) outputPassword() {
|
func (acc *Account) OutputPassword() {
|
||||||
fmt.Println(acc.login, acc.password, acc.url)
|
c := color.New(color.FgRed, color.Italic, color.Bold)
|
||||||
|
c.Printf("password: %v\n", acc.Password)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (acc AccountWithTimeStamp) OutputPassword() {
|
func (acc *Account) ToBytes() ([]byte, error) {
|
||||||
fmt.Println(acc.login, acc.password, acc.url)
|
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 {
|
for i := range res {
|
||||||
res[i] = letterRunes[rand.IntN(len(letterRunes))]
|
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)
|
_, err := url.ParseRequestURI(urlString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("INVALID_URL")
|
return nil, errors.New("INVALID_URL")
|
||||||
@@ -49,14 +53,12 @@ func NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTim
|
|||||||
if login == "" {
|
if login == "" {
|
||||||
return nil, errors.New("INVALID_LOGIN")
|
return nil, errors.New("INVALID_LOGIN")
|
||||||
}
|
}
|
||||||
newAcc := &AccountWithTimeStamp{
|
newAcc := &Account{
|
||||||
createdAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
updatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
Account: Account{
|
Login: login,
|
||||||
login: login,
|
Password: password,
|
||||||
password: password,
|
Url: urlString,
|
||||||
url: urlString,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if password == "" {
|
if password == "" {
|
||||||
newAcc.generatePassword(18)
|
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
|
module password
|
||||||
|
|
||||||
go 1.23.6
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"password/account"
|
"password/account"
|
||||||
|
"password/files"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
createdAccount()
|
||||||
|
}
|
||||||
|
|
||||||
|
func createdAccount() {
|
||||||
login := account.PromptData("Введите логин: ")
|
login := account.PromptData("Введите логин: ")
|
||||||
password := account.PromptData("Введите пароль: ")
|
password := account.PromptData("Введите пароль: ")
|
||||||
url := account.PromptData("Введите URL: ")
|
url := account.PromptData("Введите URL: ")
|
||||||
|
|
||||||
myAccount, err := account.NewAccountWithTimeStamp(login, password, url)
|
myAccount, err := account.NewAccount(login, password, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ОШИБКА: Неверный формат URL")
|
fmt.Println("ОШИБКА: Неверный формат URL")
|
||||||
return
|
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