Add any type and output errors

This commit is contained in:
Archie Fox
2025-03-14 15:11:17 +03:00
parent debf1eec22
commit e7bb6ba60a
4 changed files with 25 additions and 8 deletions

View File

@@ -2,10 +2,9 @@ package account
import ( import (
"encoding/json" "encoding/json"
"password/output"
"strings" "strings"
"time" "time"
"github.com/fatih/color"
) )
type ByteReader interface { type ByteReader interface {
@@ -46,7 +45,7 @@ func NewVault(db Db) *VaultWithDb {
var vault Vault var vault Vault
err = json.Unmarshal(file, &vault) err = json.Unmarshal(file, &vault)
if err != nil { if err != nil {
color.Red("Не удалось разобрать файл data.json") output.PrintError("Не удалось разобрать файл data.json")
return &VaultWithDb{ return &VaultWithDb{
Vault: Vault{ Vault: Vault{
Accounts: []Account{}, Accounts: []Account{},
@@ -105,7 +104,7 @@ func (vault *VaultWithDb) save() {
vault.UpdatedAt = time.Now() vault.UpdatedAt = time.Now()
data, err := vault.Vault.ToBytes() data, err := vault.Vault.ToBytes()
if err != nil { if err != nil {
color.Red("Не удалось преобразовать") output.PrintError("Не удалось преобразовать")
} }
vault.db.Write(data) vault.db.Write(data)
} }

View File

@@ -3,6 +3,7 @@ package files
import ( import (
"fmt" "fmt"
"os" "os"
"password/output"
) )
type JsonDb struct { type JsonDb struct {
@@ -26,13 +27,13 @@ func (db *JsonDb) Read() ([]byte, error) {
func (db *JsonDb) Write(content []byte) { func (db *JsonDb) Write(content []byte) {
file, err := os.Create(db.filename) file, err := os.Create(db.filename)
if err != nil { if err != nil {
fmt.Println(err) output.PrintError(err)
} }
defer file.Close() defer file.Close()
_, err = file.Write(content) _, err = file.Write(content)
if err != nil { if err != nil {
file.Close() file.Close()
fmt.Println(err) output.PrintError(err)
return return
} }
fmt.Println("Файл успешно записан") fmt.Println("Файл успешно записан")

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"password/account" "password/account"
"password/files" "password/files"
"password/output"
"github.com/fatih/color" "github.com/fatih/color"
) )
@@ -55,7 +56,7 @@ func deleteAccount(vault *account.VaultWithDb) {
if isDeleted { if isDeleted {
color.Green("Удалено") color.Green("Удалено")
} else { } else {
color.Red("Не найдено") output.PrintError("Не найдено")
} }
} }
@@ -66,7 +67,7 @@ func createAccount(vault *account.VaultWithDb) {
myAccount, err := account.NewAccount(login, password, url) myAccount, err := account.NewAccount(login, password, url)
if err != nil { if err != nil {
fmt.Println("ОШИБКА: Неверный формат URL или Login") output.PrintError("ОШИБКА: Неверный формат URL или Login")
return return
} }
vault.AddAccount(*myAccount) vault.AddAccount(*myAccount)

View File

@@ -0,0 +1,16 @@
package output
import "github.com/fatih/color"
func PrintError(value any) {
switch t := value.(type) {
case string:
color.Red(t)
case int:
color.Red("Код ошибки: %d", t)
case error:
color.Red(t.Error())
default:
color.Red("Неизвестный тип ошибки")
}
}