From 4d7915a3665763d3501b8e1c1d5091fe59f5cbc3 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 14 Sep 2021 19:24:10 +0300 Subject: [PATCH] [#791] neofs-lens: Implement object listing Implement `list` command for object listing. Support listing of the objects from write-cache and blobovnicza. Signed-off-by: Leonard Lyubich --- cmd/neofs-lens/internal/commands/list/list.go | 72 +++++++++++++++++++ cmd/neofs-lens/internal/common.go | 31 ++++++++ cmd/neofs-lens/root.go | 3 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 cmd/neofs-lens/internal/commands/list/list.go create mode 100644 cmd/neofs-lens/internal/common.go diff --git a/cmd/neofs-lens/internal/commands/list/list.go b/cmd/neofs-lens/internal/commands/list/list.go new file mode 100644 index 000000000..88c9cfa6e --- /dev/null +++ b/cmd/neofs-lens/internal/commands/list/list.go @@ -0,0 +1,72 @@ +package cmdlist + +import ( + "io" + + "github.com/nspcc-dev/neofs-api-go/pkg/object" + common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" + "github.com/spf13/cobra" +) + +const ( + flagFile = "path" + flagWriteCache = "writecache" +) + +var ( + vPath string + vWriteCache bool +) + +func init() { + Command.Flags().StringVar(&vPath, flagFile, "", + "Path to storage engine component", + ) + _ = Command.MarkFlagFilename(flagFile) + _ = Command.MarkFlagRequired(flagFile) + + Command.Flags().BoolVar(&vWriteCache, flagWriteCache, false, + "Process write-cache", + ) +} + +var Command = &cobra.Command{ + Use: "list", + Short: "Object listing", + Long: `List all objects stored in storage engine component.`, + Run: func(cmd *cobra.Command, args []string) { + // other targets can be supported + w := cmd.OutOrStderr() + + wAddr := func(addr *object.Address) error { + _, err := io.WriteString(w, addr.String()+"\n") + return err + } + + if vWriteCache { + db, err := writecache.OpenDB(vPath, true) + common.ExitOnErr(cmd, common.Errf("could not open write-cache db: %w", err)) + + defer db.Close() + + err = writecache.IterateDB(db, wAddr) + common.ExitOnErr(cmd, common.Errf("write-cache iterator failure: %w", err)) + + return + } + + blz := blobovnicza.New( + blobovnicza.WithPath(vPath), + blobovnicza.ReadOnly(), + ) + + common.ExitOnErr(cmd, blz.Open()) + + defer blz.Close() + + err := blobovnicza.IterateAddresses(blz, wAddr) + common.ExitOnErr(cmd, common.Errf("blobovnicza iterator failure: %w", err)) + }, +} diff --git a/cmd/neofs-lens/internal/common.go b/cmd/neofs-lens/internal/common.go new file mode 100644 index 000000000..536ba2031 --- /dev/null +++ b/cmd/neofs-lens/internal/common.go @@ -0,0 +1,31 @@ +package common + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// Errf returns formatted error in errFmt format if err is not nil. +func Errf(errFmt string, err error) error { + if err == nil { + return nil + } + + return fmt.Errorf(errFmt, err) +} + +// ExitOnErr calls exitOnErrCode with code 1. +func ExitOnErr(cmd *cobra.Command, err error) { + exitOnErrCode(cmd, err, 1) +} + +// exitOnErrCode prints error via cmd and calls os.Exit with passed exit code. +// Does nothing if err is nil. +func exitOnErrCode(cmd *cobra.Command, err error, code int) { + if err != nil { + cmd.PrintErrln(err) + os.Exit(code) + } +} diff --git a/cmd/neofs-lens/root.go b/cmd/neofs-lens/root.go index e3c1792cf..ba1f6bd7d 100644 --- a/cmd/neofs-lens/root.go +++ b/cmd/neofs-lens/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + cmdlist "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal/commands/list" "github.com/nspcc-dev/neofs-node/misc" "github.com/spf13/cobra" ) @@ -32,7 +33,7 @@ func entryPoint(cmd *cobra.Command, _ []string) error { func init() { command.AddCommand( - // sub-commands + cmdlist.Command, ) }