From 26e125577d0e34065d4a34324c59e5bc4d4b82a8 Mon Sep 17 00:00:00 2001 From: Archie Fox Date: Thu, 6 Mar 2025 23:58:58 +0300 Subject: [PATCH] Add function transformation data on JSON --- go-demo-4/account/account.go | 48 +++++++++++++++++++----------------- go-demo-4/data.json | 7 ++++++ go-demo-4/files/files.go | 30 ++++++++++++++++++++++ go-demo-4/go.mod | 8 ++++++ go-demo-4/go.sum | 11 +++++++++ go-demo-4/main.go | 14 +++++++++-- go-demo-4/test.txt | 1 + 7 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 go-demo-4/data.json create mode 100644 go-demo-4/files/files.go create mode 100644 go-demo-4/go.sum create mode 100644 go-demo-4/test.txt diff --git a/go-demo-4/account/account.go b/go-demo-4/account/account.go index 7244722..f038831 100644 --- a/go-demo-4/account/account.go +++ b/go-demo-4/account/account.go @@ -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) diff --git a/go-demo-4/data.json b/go-demo-4/data.json new file mode 100644 index 0000000..2d8c278 --- /dev/null +++ b/go-demo-4/data.json @@ -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" +} diff --git a/go-demo-4/files/files.go b/go-demo-4/files/files.go new file mode 100644 index 0000000..4dd3ef5 --- /dev/null +++ b/go-demo-4/files/files.go @@ -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)) +} diff --git a/go-demo-4/go.mod b/go-demo-4/go.mod index 01405c6..995d79b 100644 --- a/go-demo-4/go.mod +++ b/go-demo-4/go.mod @@ -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 +) diff --git a/go-demo-4/go.sum b/go-demo-4/go.sum new file mode 100644 index 0000000..33148a4 --- /dev/null +++ b/go-demo-4/go.sum @@ -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= diff --git a/go-demo-4/main.go b/go-demo-4/main.go index 5a1c445..8fb3ad1 100644 --- a/go-demo-4/main.go +++ b/go-demo-4/main.go @@ -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") } diff --git a/go-demo-4/test.txt b/go-demo-4/test.txt new file mode 100644 index 0000000..faa5c5d --- /dev/null +++ b/go-demo-4/test.txt @@ -0,0 +1 @@ +Hello! I'm file! \ No newline at end of file