Skip to content

Commit

Permalink
errors are moved to a separate file (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 authored Feb 15, 2025
1 parent 80b03b3 commit 1f46d83
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/account/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func lsCmd() *cobra.Command {
}

if len(conf.GetAccounts()) == 0 {
fmt.Println("No accounts found")
fmt.Println(internal.NoAccountsFound.Error())
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/module/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func lsCmd() *cobra.Command {
}

if len(conf.GetModules()) == 0 {
fmt.Println("No modules found")
fmt.Println(internal.NoModulesFound.Error())
return nil
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func lsCmd() *cobra.Command {
}

if j == 0 {
fmt.Println("No modules found")
fmt.Println(internal.NoModulesFound.Error())
}
}

Expand Down
15 changes: 15 additions & 0 deletions internal/err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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")
)
24 changes: 15 additions & 9 deletions internal/helpers.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package internal

import (
"errors"

"github.com/charmbracelet/huh"

"github.com/pixel365/bx/internal/model"
)

type Cfg string

var NoConfigError = errors.New("no config found in context")

const (
CfgContextKey Cfg = "config"

Yes = "Yes"
No = "No"
)

type Printer interface {
Expand All @@ -37,7 +36,7 @@ type ConfigManager interface {

func AccountIndexByLogin(accounts []model.Account, login string) (int, error) {
if len(accounts) == 0 {
return 0, errors.New("no accounts found")
return 0, NoAccountsFound
}

for i, account := range accounts {
Expand All @@ -46,14 +45,14 @@ func AccountIndexByLogin(accounts []model.Account, login string) (int, error) {
}
}

return 0, errors.New("account not found")
return 0, NoAccountFound
}

func Confirmation(flag *bool, title string) error {
if err := huh.NewConfirm().
Title(title).
Affirmative("Yes").
Negative("No").
Affirmative(Yes).
Negative(No).
Value(flag).
Run(); err != nil {
return err
Expand All @@ -64,7 +63,14 @@ func Confirmation(flag *bool, title string) error {

func Choose[T OptionProvider](items []T, value *string, title string) error {
if len(items) == 0 {
return errors.New("no items found")
switch any(items).(type) {
case []model.Account:
return NoAccountsFound
case []model.Module:
return NoModulesFound
default:
return NoItemsFound
}
}

var options []huh.Option[string]
Expand Down
9 changes: 4 additions & 5 deletions internal/validators.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package internal

import (
"errors"
"strings"
)

func ValidateAccountLogin(login string, conf ConfigManager) error {
value := strings.TrimSpace(login)
if value == "" {
return errors.New("login is empty")
return EmptyLogin
}

if len(conf.GetAccounts()) > 0 {
for _, account := range conf.GetAccounts() {
if account.Login == value {
return errors.New("an account with this login already exists")
return AccountAlreadyExists
}
}
}
Expand All @@ -25,11 +24,11 @@ func ValidateAccountLogin(login string, conf ConfigManager) error {
func ValidatePassword(password string) error {
value := strings.TrimSpace(password)
if value == "" {
return errors.New("password is empty")
return EmptyPassword
}

if len(value) < 6 {
return errors.New("password is too short")
return PasswordTooShort
}

return nil
Expand Down

0 comments on commit 1f46d83

Please sign in to comment.