From e7b8eb56012dacb5f7e6bd1bf04b09e748b712de Mon Sep 17 00:00:00 2001 From: Archer Fox Date: Tue, 18 Mar 2025 22:04:43 +0300 Subject: [PATCH] Add environment variables and encryptor package --- go-demo-4/account/vault.go | 13 +++++++++---- go-demo-4/encrypter/encrypter.go | 25 +++++++++++++++++++++++++ go-demo-4/go.mod | 5 ++++- go-demo-4/go.sum | 2 ++ go-demo-4/main.go | 12 ++++++++---- 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 go-demo-4/encrypter/encrypter.go diff --git a/go-demo-4/account/vault.go b/go-demo-4/account/vault.go index 0bd1b78..f4e6ecb 100644 --- a/go-demo-4/account/vault.go +++ b/go-demo-4/account/vault.go @@ -2,6 +2,7 @@ package account import ( "encoding/json" + "password/encrypter" "password/output" "strings" "time" @@ -28,10 +29,11 @@ type Vault struct { type VaultWithDb struct { Vault - db Db + db Db + enc encrypter.Encrypter } -func NewVault(db Db) *VaultWithDb { +func NewVault(db Db, enc encrypter.Encrypter) *VaultWithDb { file, err := db.Read() if err != nil { return &VaultWithDb{ @@ -39,7 +41,8 @@ func NewVault(db Db) *VaultWithDb { Accounts: []Account{}, UpdatedAt: time.Now(), }, - db: db, + db: db, + enc: enc, } } var vault Vault @@ -51,12 +54,14 @@ func NewVault(db Db) *VaultWithDb { Accounts: []Account{}, UpdatedAt: time.Now(), }, - db: db, + db: db, + enc: enc, } } return &VaultWithDb{ Vault: vault, db: db, + enc: enc, } } diff --git a/go-demo-4/encrypter/encrypter.go b/go-demo-4/encrypter/encrypter.go new file mode 100644 index 0000000..d94eaff --- /dev/null +++ b/go-demo-4/encrypter/encrypter.go @@ -0,0 +1,25 @@ +package encrypter + +import "os" + +type Encrypter struct { + Key string +} + +func NewEncrypter() *Encrypter { + key := os.Getenv("KEY") + if key == "" { + panic("Не передан параметр KEY в переменные окружения") + } + return &Encrypter{ + Key: key, + } +} + +func (enc *Encrypter) Encrypt(plainStr string) string { + return "" +} + +func (enc *Encrypter) Decrypt(encryptedStr string) string { + return "" +} diff --git a/go-demo-4/go.mod b/go-demo-4/go.mod index 995d79b..a63f91a 100644 --- a/go-demo-4/go.mod +++ b/go-demo-4/go.mod @@ -2,7 +2,10 @@ module password go 1.23.6 -require github.com/fatih/color v1.18.0 +require ( + github.com/fatih/color v1.18.0 + github.com/joho/godotenv v1.5.1 +) require ( github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go-demo-4/go.sum b/go-demo-4/go.sum index 33148a4..2820568 100644 --- a/go-demo-4/go.sum +++ b/go-demo-4/go.sum @@ -1,5 +1,7 @@ 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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 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= diff --git a/go-demo-4/main.go b/go-demo-4/main.go index bc53c67..66f57dc 100644 --- a/go-demo-4/main.go +++ b/go-demo-4/main.go @@ -2,13 +2,14 @@ package main import ( "fmt" - "os" "password/account" + "password/encrypter" "password/files" "password/output" "strings" "github.com/fatih/color" + "github.com/joho/godotenv" ) var menu = map[string]func(*account.VaultWithDb){ @@ -37,9 +38,12 @@ func menuCounter() func() { func main() { color.Blue("__Менеджер паролей__") - res := os.Getenv("PWD") - fmt.Println(res) - vault := account.NewVault(files.NewJsonDb("data.json")) + // Load env file + err := godotenv.Load() + if err != nil { + output.PrintError("Не удалось найти файл .env") + } + vault := account.NewVault(files.NewJsonDb("data.json"), *encrypter.NewEncrypter()) counter := menuCounter() Menu: for {