[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-09-14 19:24:10 +03:00 committed by Leonard Lyubich
parent 522cbab47c
commit 4d7915a366
3 changed files with 105 additions and 1 deletions

View file

@ -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))
},
}

View file

@ -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)
}
}

View file

@ -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,
)
}