Add interface

This commit is contained in:
Archie Fox
2025-03-12 22:37:08 +03:00
parent 46170ac5da
commit b16d0da418

View File

@@ -2,13 +2,17 @@ package account
import ( import (
"encoding/json" "encoding/json"
"password/files"
"strings" "strings"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
) )
type Db interface {
Read() ([]byte, error)
Write([]byte)
}
type Vault struct { type Vault struct {
Accounts []Account `json:"accounts"` Accounts []Account `json:"accounts"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
@@ -16,10 +20,10 @@ type Vault struct {
type VaultWithDb struct { type VaultWithDb struct {
Vault Vault
db files.JsonDb db Db
} }
func NewVault(db *files.JsonDb) *VaultWithDb { func NewVault(db Db) *VaultWithDb {
file, err := db.Read() file, err := db.Read()
if err != nil { if err != nil {
return &VaultWithDb{ return &VaultWithDb{
@@ -27,7 +31,7 @@ func NewVault(db *files.JsonDb) *VaultWithDb {
Accounts: []Account{}, Accounts: []Account{},
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}, },
db: *db, db: db,
} }
} }
var vault Vault var vault Vault
@@ -39,12 +43,12 @@ func NewVault(db *files.JsonDb) *VaultWithDb {
Accounts: []Account{}, Accounts: []Account{},
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}, },
db: *db, db: db,
} }
} }
return &VaultWithDb{ return &VaultWithDb{
Vault: vault, Vault: vault,
db: *db, db: db,
} }
} }