Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use Storage interface instead of direct struct #113

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ issues:
- path: internal/info
linters:
- gochecknoglobals
- source: "// .* #\\d+"
# Ignore TODO lines if they have a GitHub Issue reference.
linters:
- godox

linters:
enable-all: true
Expand Down
10 changes: 3 additions & 7 deletions internal/cmd/add_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ func AddTokenCommand() *cli.Command {
ctx.Args().Get(argSetPrefixPositionAccount),
)

storage, err := s.PrepareStorage()
if err != nil {
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

namespace, err = storage.FindNamespace(nsName)
if err != nil {
namespace = &s.Namespace{Name: nsName}
storage.Namespaces = append(storage.Namespaces, namespace)
}
namespace, _ = storage.AddNamespace(&s.Namespace{Name: nsName})

account, err = namespace.FindAccount(accName)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/change_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func ChangePasswordCommand() *cli.Command {
Action: func(_ *cli.Context) error {
var (
err error
storage *s.Storage
newPasswordIn string
newPasswordConfirmIn string
)

if storage, err = s.PrepareStorage(); err != nil {
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

Expand All @@ -42,7 +42,7 @@ func ChangePasswordCommand() *cli.Command {
return CommandError{Message: "new password and the confirm mismatch"}
}

storage.Password = newPasswordIn
storage.SetPassword(newPasswordIn)

return storage.Save()
},
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func DeleteCommand() *cli.Command {
return CommandError{Message: "namespace is not defined"}
}

storage, err := s.PrepareStorage()
if err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func DumpCommand() *cli.Command {
}
}

storage, err := s.PrepareStorage()
if err != nil {
storage := s.NewFileStorage()
if err := storage.Prepare(); err != nil {
return err
}

out, err := yaml.Marshal(storage.Namespaces)
out, err := yaml.Marshal(storage.ListNamespaces())
if err != nil {
return fmt.Errorf("failed to marshal storage: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func GenerateCommand() *cli.Command {

follow := ctx.Bool("follow")

storage, err := s.PrepareStorage()
if err != nil {
storage := s.NewFileStorage()
if err := storage.Prepare(); err != nil {
return err
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func GenerateCommand() *cli.Command {
}
}

func getAccount(storage *s.Storage, namespaceName, accountName string) (*s.Account, error) {
func getAccount(storage s.Storage, namespaceName, accountName string) (*s.Account, error) {
namespace, err := storage.FindNamespace(namespaceName)
if err != nil {
return nil, err
Expand Down
19 changes: 6 additions & 13 deletions internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func ImportCommand() *cli.Command {
},
},
Action: func(ctx *cli.Context) (err error) {
var (
file []byte
storage *s.Storage
)
var file []byte

if file, err = os.ReadFile(ctx.String("input")); err != nil {
return
Expand All @@ -41,8 +38,9 @@ func ImportCommand() *cli.Command {
return
}

if storage, err = s.PrepareStorage(); err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand All @@ -60,16 +58,11 @@ func ImportCommand() *cli.Command {
}
}

func importNamespaces(storage *s.Storage, nsList []*s.Namespace) {
func importNamespaces(storage s.Storage, nsList []*s.Namespace) {
term := terminal.New(os.Stdin, os.Stdout, os.Stderr)

for _, ns := range nsList {
internalNS, err := storage.FindNamespace(ns.Name)
if err != nil {
storage.Namespaces = append(storage.Namespaces, ns)

continue
}
internalNS, _ := storage.AddNamespace(ns)

for _, acc := range ns.Accounts {
internalAcc, err := internalNS.FindAccount(acc.Name)
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func ListCommand() *cli.Command {
Usage: "List all available namespaces or accounts under a namespace.",
ArgsUsage: "[namespace]",
Action: func(ctx *cli.Context) error {
storage, err := s.PrepareStorage()
if err != nil {
storage := s.NewFileStorage()
if err := storage.Prepare(); err != nil {
return err
}

ns := ctx.Args().Get(argSetPrefixPositionNamespace)
if len(ns) < 1 {
for _, namespace := range storage.Namespaces {
for _, namespace := range storage.ListNamespaces() {
fmt.Printf("%s (Number of accounts: %d)\n", namespace.Name, len(namespace.Accounts))
}

Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func renameNamespaceCommand() *cli.Command {
ctx.Args().Get(argRenameNamespacePositionNewName),
)

storage, err := s.PrepareStorage()
if err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand Down Expand Up @@ -76,9 +76,9 @@ func renameAccountCommand() *cli.Command {
ctx.Args().Get(argRenameAccountPositionNewName),
)

storage, err := s.PrepareStorage()
if err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/set_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func SetLengthCommand() *cli.Command {
ctx.Args().Get(argSetLengthPositionPrefix),
)

storage, err := s.PrepareStorage()
if err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/set_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func SetPrefixCommand() *cli.Command {
ctx.Bool("clear"),
)

storage, err := s.PrepareStorage()
if err != nil {
return
storage := s.NewFileStorage()
if err = storage.Prepare(); err != nil {
return err
}

defer func() {
Expand Down
Loading
Loading