From 08473b0be0be4e10dd9a9474ac4ce3644f66b24c Mon Sep 17 00:00:00 2001 From: Ruslan Semagin Date: Mon, 17 Feb 2025 14:47:16 +0300 Subject: [PATCH] refactor: rename errors Signed-off-by: Ruslan Semagin --- cmd/account/ls.go | 2 +- cmd/account/modules.go | 2 +- cmd/account_test.go | 8 ++++---- cmd/module/ls.go | 4 ++-- internal/err.go | 18 +++++++++--------- internal/helpers.go | 10 +++++----- internal/validators.go | 8 ++++---- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/cmd/account/ls.go b/cmd/account/ls.go index 3f7b4d1..dbb6359 100644 --- a/cmd/account/ls.go +++ b/cmd/account/ls.go @@ -17,7 +17,7 @@ func lsCmd() *cobra.Command { } if len(conf.GetAccounts()) == 0 { - internal.ResultMessage(internal.NoAccountsFound.Error()) + internal.ResultMessage(internal.NoAccountsFoundError.Error()) return nil } diff --git a/cmd/account/modules.go b/cmd/account/modules.go index 6cef349..d9d4055 100644 --- a/cmd/account/modules.go +++ b/cmd/account/modules.go @@ -43,7 +43,7 @@ func moduleCmd() *cobra.Command { } if j == 0 { - internal.ResultMessage(internal.NoModulesFound.Error()) + internal.ResultMessage(internal.NoModulesFoundError.Error()) return nil } diff --git a/cmd/account_test.go b/cmd/account_test.go index 44cfced..e6ae5e6 100644 --- a/cmd/account_test.go +++ b/cmd/account_test.go @@ -39,7 +39,7 @@ func TestAccountAdd(t *testing.T) { t.Error("invalid result") } - if !errors.Is(err, internal.AccountAlreadyExists) { + if !errors.Is(err, internal.AccountAlreadyExistsError) { t.Errorf("invalid result: %v", err) } }) @@ -161,7 +161,7 @@ func TestAccountRmAccountNotFound(t *testing.T) { t.Error("invalid result") } - if err.Error() != internal.NoAccountFound.Error() { + if err.Error() != internal.NoAccountFoundError.Error() { t.Error(err) } }) @@ -192,7 +192,7 @@ func TestAccountNoModules(t *testing.T) { } }) - if output != fmt.Sprintf("%s\n", internal.NoModulesFound.Error()) { + if output != fmt.Sprintf("%s\n", internal.NoModulesFoundError.Error()) { t.Error("invalid result") } }) @@ -221,7 +221,7 @@ func TestAccountNoModulesNoAccount(t *testing.T) { t.Error("invalid result") } - if err.Error() != internal.NoAccountFound.Error() { + if err.Error() != internal.NoAccountFoundError.Error() { t.Error(err) } }) diff --git a/cmd/module/ls.go b/cmd/module/ls.go index c1f2db2..6d358b2 100644 --- a/cmd/module/ls.go +++ b/cmd/module/ls.go @@ -19,7 +19,7 @@ func lsCmd() *cobra.Command { } if len(conf.GetModules()) == 0 { - internal.ResultMessage(internal.NoModulesFound.Error()) + internal.ResultMessage(internal.NoModulesFoundError.Error()) return nil } @@ -55,7 +55,7 @@ func lsCmd() *cobra.Command { } if j == 0 { - internal.ResultMessage(internal.NoModulesFound.Error()) + internal.ResultMessage(internal.NoModulesFoundError.Error()) } } diff --git a/internal/err.go b/internal/err.go index ae21027..f82f8be 100644 --- a/internal/err.go +++ b/internal/err.go @@ -3,13 +3,13 @@ package internal import "errors" var ( - NoConfigError = errors.New("no config found in context") - NoAccountFound = errors.New("account not found") - NoAccountsFound = errors.New("no accounts found") - NoItemsFound = errors.New("item not found") - EmptyLogin = errors.New("login is empty") - AccountAlreadyExists = errors.New("account already exists") - EmptyPassword = errors.New("password is empty") - PasswordTooShort = errors.New("password is too short") - NoModulesFound = errors.New("no modules found") + NoConfigError = errors.New("no config found in context") + NoAccountFoundError = errors.New("account not found") + NoAccountsFoundError = errors.New("no accounts found") + NoItemsFoundError = errors.New("item not found") + EmptyLoginError = errors.New("login is empty") + AccountAlreadyExistsError = errors.New("account already exists") + EmptyPasswordError = errors.New("password is empty") + PasswordTooShortError = errors.New("password is too short") + NoModulesFoundError = errors.New("no modules found") ) diff --git a/internal/helpers.go b/internal/helpers.go index 94cd001..a8eedf5 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -40,7 +40,7 @@ type ConfigManager interface { func AccountIndexByLogin(accounts []model.Account, login string) (int, error) { if len(accounts) == 0 { - return 0, NoAccountsFound + return 0, NoAccountsFoundError } for i, account := range accounts { @@ -49,7 +49,7 @@ func AccountIndexByLogin(accounts []model.Account, login string) (int, error) { } } - return 0, NoAccountFound + return 0, NoAccountFoundError } func Confirmation(flag *bool, title string) error { @@ -69,11 +69,11 @@ func Choose[T OptionProvider](items []T, value *string, title string) error { if len(items) == 0 { switch any(items).(type) { case []model.Account: - return NoAccountsFound + return NoAccountsFoundError case []model.Module: - return NoModulesFound + return NoModulesFoundError default: - return NoItemsFound + return NoItemsFoundError } } diff --git a/internal/validators.go b/internal/validators.go index b9a792a..da82d6f 100644 --- a/internal/validators.go +++ b/internal/validators.go @@ -7,13 +7,13 @@ import ( func ValidateAccountLogin(login string, conf ConfigManager) error { value := strings.TrimSpace(login) if value == "" { - return EmptyLogin + return EmptyLoginError } if len(conf.GetAccounts()) > 0 { for _, account := range conf.GetAccounts() { if account.Login == value { - return AccountAlreadyExists + return AccountAlreadyExistsError } } } @@ -24,11 +24,11 @@ func ValidateAccountLogin(login string, conf ConfigManager) error { func ValidatePassword(password string) error { value := strings.TrimSpace(password) if value == "" { - return EmptyPassword + return EmptyPasswordError } if len(value) < 6 { - return PasswordTooShort + return PasswordTooShortError } return nil