Add environment variables and encryptor package
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
25
go-demo-4/encrypter/encrypter.go
Normal file
25
go-demo-4/encrypter/encrypter.go
Normal file
@@ -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 ""
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user