Skip to content

Commit

Permalink
refactor: use Storage interface instead of direct struct
Browse files Browse the repository at this point in the history
It can get quiet hard to test structs in Go and functions using them.
With this, the rest of the code can be mocked easier with a TestBackend
which is just sits in memory and no encryption. The encryption should be
tested on storage layer, but the rest of the application should care
only about the exposed functions.

related to #8 and #37

References:
- #8
- #37

Signed-off-by: Victoria Nadasdi <victoria@efertone.me>
  • Loading branch information
yitsushi committed Jan 28, 2024
1 parent 5067ad0 commit 28cd785
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 427 deletions.
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

0 comments on commit 28cd785

Please sign in to comment.