From f45e200c6efe6dc713e576e24c4717f790979d04 Mon Sep 17 00:00:00 2001 From: Archie Fox Date: Sat, 8 Mar 2025 21:18:39 +0300 Subject: [PATCH] Add func read and write to JSON --- .gitignore | 1 + go-demo-4/.gitignore | 1 + go-demo-4/account/account.go | 16 ++------- go-demo-4/account/vault.go | 64 ++++++++++++++++++++++++++++++++++++ go-demo-4/files/files.go | 9 +++-- go-demo-4/main.go | 56 ++++++++++++++++++++++++++----- 6 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 go-demo-4/.gitignore create mode 100644 go-demo-4/account/vault.go diff --git a/.gitignore b/.gitignore index ec940e3..f3cc7ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ go-demo/app-1 +go-demo-4/data.json diff --git a/go-demo-4/.gitignore b/go-demo-4/.gitignore new file mode 100644 index 0000000..54cf287 --- /dev/null +++ b/go-demo-4/.gitignore @@ -0,0 +1 @@ +./data.json diff --git a/go-demo-4/account/account.go b/go-demo-4/account/account.go index f038831..e183f95 100644 --- a/go-demo-4/account/account.go +++ b/go-demo-4/account/account.go @@ -1,14 +1,12 @@ package account import ( - "encoding/json" "errors" "fmt" + "github.com/fatih/color" "math/rand/v2" "net/url" "time" - - "github.com/fatih/color" ) var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&") @@ -22,17 +20,9 @@ type Account struct { } // метод вывода пароля -func (acc *Account) OutputPassword() { +func (acc *Account) Output() { c := color.New(color.FgRed, color.Italic, color.Bold) - c.Printf("password: %v\n", acc.Password) -} - -func (acc *Account) ToBytes() ([]byte, error) { - file, err := json.Marshal(acc) - if err != nil { - return nil, err - } - return file, nil + c.Printf("password: %v, login: %v, URL: %v\n", acc.Password, acc.Login, acc.Url) } // метод генерации пароля diff --git a/go-demo-4/account/vault.go b/go-demo-4/account/vault.go new file mode 100644 index 0000000..fb34b7e --- /dev/null +++ b/go-demo-4/account/vault.go @@ -0,0 +1,64 @@ +package account + +import ( + "encoding/json" + "password/files" + "strings" + "time" + + "github.com/fatih/color" +) + +type Vault struct { + Accounts []Account `json:"accounts"` + UpdatedAt time.Time `json:"updatedAt"` +} + +func NewVault() *Vault { + file, err := files.FileRead("data.json") + if err != nil { + return &Vault{ + Accounts: []Account{}, + UpdatedAt: time.Now(), + } + } + var vault Vault + err = json.Unmarshal(file, &vault) + if err != nil { + color.Red("Не удалось разобрать файл data.json") + return &Vault{ + Accounts: []Account{}, + UpdatedAt: time.Now(), + } + } + return &vault +} + +func (vault *Vault) AddAccount(acc Account) { + vault.Accounts = append(vault.Accounts, acc) + vault.UpdatedAt = time.Now() + data, err := vault.ToBytes() + if err != nil { + color.Red("Не удалось преобразовать") + } + files.FileWrite(data, "data.json") +} + +func (vault *Vault) ToBytes() ([]byte, error) { + file, err := json.Marshal(vault) + if err != nil { + return nil, err + } + return file, nil +} + +func (vault *Vault) FindAccountsByUrl(url string) []Account { + var accounts []Account + for _, account := range vault.Accounts { + isMatched := strings.Contains(account.Url, url) + if isMatched { + accounts = append(accounts, account) + } + } + return accounts +} diff --git a/go-demo-4/files/files.go b/go-demo-4/files/files.go index 4dd3ef5..7878b65 100644 --- a/go-demo-4/files/files.go +++ b/go-demo-4/files/files.go @@ -20,11 +20,10 @@ func FileWrite(content []byte, name string) { fmt.Println("Файл успешно записан") } -func FileRead() { - data, err := os.ReadFile("test.txt") +func FileRead(name string) ([]byte, error) { + data, err := os.ReadFile(name) if err != nil { - fmt.Println(err) - return + return nil, err } - fmt.Println(string(data)) + return data, nil } diff --git a/go-demo-4/main.go b/go-demo-4/main.go index 8fb3ad1..03f292d 100644 --- a/go-demo-4/main.go +++ b/go-demo-4/main.go @@ -3,14 +3,56 @@ package main import ( "fmt" "password/account" - "password/files" + + "github.com/fatih/color" ) func main() { - createdAccount() + fmt.Println("__Менеджер паролей__") + vault := account.NewVault() +Menu: + for { + variant := getMenu() + switch variant { + case 1: + createAccount(vault) + case 2: + findAccount(vault) + case 3: + deleteAccount() + default: + break Menu + } + } } -func createdAccount() { +func getMenu() int { + var variant int + fmt.Println("Выберите вариант:") + fmt.Println("1. Создать аккаунт") + fmt.Println("2. Найти аккаунт") + fmt.Println("3. Удалить аккаунт") + fmt.Println("4. Выход") + fmt.Scan(&variant) + return variant +} + +func findAccount(vault *account.Vault) { + url := account.PromptData("Введите URL для поиска: ") + accounts := vault.FindAccountsByUrl(url) + if len(accounts) == 0 { + color.Red("Аккаунт не найден!") + } + for _, account := range accounts { + account.Output() + } +} + +func deleteAccount() { + +} + +func createAccount(vault *account.Vault) { login := account.PromptData("Введите логин: ") password := account.PromptData("Введите пароль: ") url := account.PromptData("Введите URL: ") @@ -20,10 +62,6 @@ func createdAccount() { fmt.Println("ОШИБКА: Неверный формат URL") return } - file, err := myAccount.ToBytes() - if err != nil { - fmt.Println("Не удалось преобразовать в JSON") - return - } - files.FileWrite(file, "data.json") + vault = account.NewVault() + vault.AddAccount(*myAccount) }