Add environment variables and encryptor package

This commit is contained in:
Archer Fox
2025-03-18 22:04:43 +03:00
parent 69be01a1db
commit e7b8eb5601
5 changed files with 48 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package account
import ( import (
"encoding/json" "encoding/json"
"password/encrypter"
"password/output" "password/output"
"strings" "strings"
"time" "time"
@@ -28,10 +29,11 @@ type Vault struct {
type VaultWithDb struct { type VaultWithDb struct {
Vault 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() file, err := db.Read()
if err != nil { if err != nil {
return &VaultWithDb{ return &VaultWithDb{
@@ -39,7 +41,8 @@ func NewVault(db Db) *VaultWithDb {
Accounts: []Account{}, Accounts: []Account{},
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}, },
db: db, db: db,
enc: enc,
} }
} }
var vault Vault var vault Vault
@@ -51,12 +54,14 @@ func NewVault(db Db) *VaultWithDb {
Accounts: []Account{}, Accounts: []Account{},
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}, },
db: db, db: db,
enc: enc,
} }
} }
return &VaultWithDb{ return &VaultWithDb{
Vault: vault, Vault: vault,
db: db, db: db,
enc: enc,
} }
} }

View 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 ""
}

View File

@@ -2,7 +2,10 @@ module password
go 1.23.6 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 ( require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect

View File

@@ -1,5 +1,7 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= 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/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 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 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.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=

View File

@@ -2,13 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"os"
"password/account" "password/account"
"password/encrypter"
"password/files" "password/files"
"password/output" "password/output"
"strings" "strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/joho/godotenv"
) )
var menu = map[string]func(*account.VaultWithDb){ var menu = map[string]func(*account.VaultWithDb){
@@ -37,9 +38,12 @@ func menuCounter() func() {
func main() { func main() {
color.Blue("__Менеджер паролей__") color.Blue("__Менеджер паролей__")
res := os.Getenv("PWD") // Load env file
fmt.Println(res) err := godotenv.Load()
vault := account.NewVault(files.NewJsonDb("data.json")) if err != nil {
output.PrintError("Не удалось найти файл .env")
}
vault := account.NewVault(files.NewJsonDb("data.json"), *encrypter.NewEncrypter())
counter := menuCounter() counter := menuCounter()
Menu: Menu:
for { for {