[#1379] neofs-cli: Move exitOnErr to internal package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-18 14:03:40 +03:00 committed by fyrchik
parent 094534e31a
commit 56f33436dd
12 changed files with 233 additions and 220 deletions

View file

@ -0,0 +1,50 @@
package common
import (
"errors"
"fmt"
"os"
sdkstatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
"github.com/spf13/cobra"
)
// ExitOnErr prints error and exits with a code that matches
// one of the common errors from sdk library. If no errors
// found, exits with 1 code.
// Does nothing if passed error in nil.
func ExitOnErr(cmd *cobra.Command, errFmt string, err error) {
if err == nil {
return
}
if errFmt != "" {
err = fmt.Errorf(errFmt, err)
}
const (
_ = iota
internal
aclDenied
)
var (
code int
internalErr = new(sdkstatus.ServerInternal)
accessErr = new(sdkstatus.ObjectAccessDenied)
)
switch {
case errors.As(err, &internalErr):
code = internal
case errors.As(err, &accessErr):
code = aclDenied
err = fmt.Errorf("%w: %s", err, accessErr.Reason())
default:
code = internal
}
cmd.PrintErrln(err)
os.Exit(code)
}

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/util/precision" "github.com/nspcc-dev/neofs-node/pkg/util/precision"
"github.com/nspcc-dev/neofs-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/accounting"
@ -39,13 +40,13 @@ var accountingBalanceCmd = &cobra.Command{
var oid *owner.ID var oid *owner.ID
key, err := getKey() key, err := getKey()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
if balanceOwner == "" { if balanceOwner == "" {
oid = owner.NewIDFromPublicKey(&key.PublicKey) oid = owner.NewIDFromPublicKey(&key.PublicKey)
} else { } else {
oid, err = ownerFromString(balanceOwner) oid, err = ownerFromString(balanceOwner)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
} }
var prm internalclient.BalanceOfPrm var prm internalclient.BalanceOfPrm
@ -54,7 +55,7 @@ var accountingBalanceCmd = &cobra.Command{
prm.SetAccount(*oid) prm.SetAccount(*oid)
res, err := internalclient.BalanceOf(prm) res, err := internalclient.BalanceOf(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
// print to stdout // print to stdout
prettyPrintDecimal(cmd, res.Balance()) prettyPrintDecimal(cmd, res.Balance())

View file

@ -13,6 +13,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/core/version" "github.com/nspcc-dev/neofs-node/pkg/core/version"
"github.com/nspcc-dev/neofs-sdk-go/acl" "github.com/nspcc-dev/neofs-sdk-go/acl"
@ -116,13 +117,13 @@ var listContainersCmd = &cobra.Command{
var oid *owner.ID var oid *owner.ID
key, err := getKey() key, err := getKey()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
if containerOwner == "" { if containerOwner == "" {
oid = owner.NewIDFromPublicKey(&key.PublicKey) oid = owner.NewIDFromPublicKey(&key.PublicKey)
} else { } else {
oid, err = ownerFromString(containerOwner) oid, err = ownerFromString(containerOwner)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
} }
var prm internalclient.ListContainersPrm var prm internalclient.ListContainersPrm
@ -131,7 +132,7 @@ var listContainersCmd = &cobra.Command{
prm.SetAccount(*oid) prm.SetAccount(*oid)
res, err := internalclient.ListContainers(prm) res, err := internalclient.ListContainers(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
// print to stdout // print to stdout
prettyPrintContainerList(cmd, res.IDList()) prettyPrintContainerList(cmd, res.IDList())
@ -145,27 +146,27 @@ var createContainerCmd = &cobra.Command{
It will be stored in sidechain when inner ring will accepts it.`, It will be stored in sidechain when inner ring will accepts it.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
placementPolicy, err := parseContainerPolicy(containerPolicy) placementPolicy, err := parseContainerPolicy(containerPolicy)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
subnetID, err := parseSubnetID(containerSubnet) subnetID, err := parseSubnetID(containerSubnet)
exitOnErr(cmd, errf("could not parse subnetID: %w", err)) common.ExitOnErr(cmd, "could not parse subnetID: %w", err)
placementPolicy.SetSubnetID(subnetID) placementPolicy.SetSubnetID(subnetID)
attributes, err := parseAttributes(containerAttributes) attributes, err := parseAttributes(containerAttributes)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
basicACL, err := parseBasicACL(containerACL) basicACL, err := parseBasicACL(containerACL)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
nonce, err := parseNonce(containerNonce) nonce, err := parseNonce(containerNonce)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
tok, err := getSessionToken(sessionTokenPath) tok, err := getSessionToken(sessionTokenPath)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
key, err := getKey() key, err := getKey()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var idOwner *owner.ID var idOwner *owner.ID
@ -193,7 +194,7 @@ It will be stored in sidechain when inner ring will accepts it.`,
putPrm.SetContainer(*cnr) putPrm.SetContainer(*cnr)
res, err := internalclient.PutContainer(putPrm) res, err := internalclient.PutContainer(putPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
id := res.ID() id := res.ID()
@ -214,7 +215,7 @@ It will be stored in sidechain when inner ring will accepts it.`,
} }
} }
exitOnErr(cmd, errCreateTimeout) common.ExitOnErr(cmd, "", errCreateTimeout)
} }
}, },
} }
@ -226,10 +227,10 @@ var deleteContainerCmd = &cobra.Command{
Only owner of the container has a permission to remove container.`, Only owner of the container has a permission to remove container.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
id, err := parseContainerID(containerID) id, err := parseContainerID(containerID)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
tok, err := getSessionToken(sessionTokenPath) tok, err := getSessionToken(sessionTokenPath)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var ( var (
delPrm internalclient.DeleteContainerPrm delPrm internalclient.DeleteContainerPrm
@ -244,7 +245,7 @@ Only owner of the container has a permission to remove container.`,
} }
_, err = internalclient.DeleteContainer(delPrm) _, err = internalclient.DeleteContainer(delPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
cmd.Println("container delete method invoked") cmd.Println("container delete method invoked")
@ -263,7 +264,7 @@ Only owner of the container has a permission to remove container.`,
} }
} }
exitOnErr(cmd, errDeleteTimeout) common.ExitOnErr(cmd, "", errDeleteTimeout)
} }
}, },
} }
@ -274,7 +275,7 @@ var listContainerObjectsCmd = &cobra.Command{
Long: `List existing objects in container`, Long: `List existing objects in container`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
id, err := parseContainerID(containerID) id, err := parseContainerID(containerID)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
filters := new(object.SearchFilters) filters := new(object.SearchFilters)
filters.AddRootFilter() // search only user created objects filters.AddRootFilter() // search only user created objects
@ -289,7 +290,7 @@ var listContainerObjectsCmd = &cobra.Command{
prm.SetFilters(*filters) prm.SetFilters(*filters)
res, err := internalclient.SearchObjects(prm) res, err := internalclient.SearchObjects(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
objectIDs := res.IDList() objectIDs := res.IDList()
@ -308,14 +309,14 @@ var getContainerInfoCmd = &cobra.Command{
if containerPathFrom != "" { if containerPathFrom != "" {
data, err := os.ReadFile(containerPathFrom) data, err := os.ReadFile(containerPathFrom)
exitOnErr(cmd, errf("can't read file: %w", err)) common.ExitOnErr(cmd, "can't read file: %w", err)
cnr = container.New() cnr = container.New()
err = cnr.Unmarshal(data) err = cnr.Unmarshal(data)
exitOnErr(cmd, errf("can't unmarshal container: %w", err)) common.ExitOnErr(cmd, "can't unmarshal container: %w", err)
} else { } else {
id, err := parseContainerID(containerID) id, err := parseContainerID(containerID)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var prm internalclient.GetContainerPrm var prm internalclient.GetContainerPrm
@ -323,7 +324,7 @@ var getContainerInfoCmd = &cobra.Command{
prm.SetContainer(*id) prm.SetContainer(*id)
res, err := internalclient.GetContainer(prm) res, err := internalclient.GetContainer(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
cnr = res.Container() cnr = res.Container()
} }
@ -338,14 +339,14 @@ var getContainerInfoCmd = &cobra.Command{
if containerJSON { if containerJSON {
data, err = cnr.MarshalJSON() data, err = cnr.MarshalJSON()
exitOnErr(cmd, errf("can't JSON encode container: %w", err)) common.ExitOnErr(cmd, "can't JSON encode container: %w", err)
} else { } else {
data, err = cnr.Marshal() data, err = cnr.Marshal()
exitOnErr(cmd, errf("can't binary encode container: %w", err)) common.ExitOnErr(cmd, "can't binary encode container: %w", err)
} }
err = os.WriteFile(containerPathTo, data, 0644) err = os.WriteFile(containerPathTo, data, 0644)
exitOnErr(cmd, errf("can't write container to file: %w", err)) common.ExitOnErr(cmd, "can't write container to file: %w", err)
} }
}, },
} }
@ -356,7 +357,7 @@ var getExtendedACLCmd = &cobra.Command{
Long: `Get extended ACL talbe of container`, Long: `Get extended ACL talbe of container`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
id, err := parseContainerID(containerID) id, err := parseContainerID(containerID)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var eaclPrm internalclient.EACLPrm var eaclPrm internalclient.EACLPrm
@ -364,7 +365,7 @@ var getExtendedACLCmd = &cobra.Command{
eaclPrm.SetContainer(*id) eaclPrm.SetContainer(*id)
res, err := internalclient.EACL(eaclPrm) res, err := internalclient.EACL(eaclPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
eaclTable := res.EACL() eaclTable := res.EACL()
@ -391,10 +392,10 @@ var getExtendedACLCmd = &cobra.Command{
if containerJSON { if containerJSON {
data, err = eaclTable.MarshalJSON() data, err = eaclTable.MarshalJSON()
exitOnErr(cmd, errf("can't encode to JSON: %w", err)) common.ExitOnErr(cmd, "can't encode to JSON: %w", err)
} else { } else {
data, err = eaclTable.Marshal() data, err = eaclTable.Marshal()
exitOnErr(cmd, errf("can't encode to binary: %w", err)) common.ExitOnErr(cmd, "can't encode to binary: %w", err)
} }
cmd.Println("dumping data to file:", containerPathTo) cmd.Println("dumping data to file:", containerPathTo)
@ -403,7 +404,7 @@ var getExtendedACLCmd = &cobra.Command{
printJSONMarshaler(cmd, &sigV2, "signature") printJSONMarshaler(cmd, &sigV2, "signature")
err = os.WriteFile(containerPathTo, data, 0644) err = os.WriteFile(containerPathTo, data, 0644)
exitOnErr(cmd, errf("could not write eACL to file: %w", err)) common.ExitOnErr(cmd, "could not write eACL to file: %w", err)
}, },
} }
@ -414,13 +415,13 @@ var setExtendedACLCmd = &cobra.Command{
Container ID in EACL table will be substituted with ID from the CLI.`, Container ID in EACL table will be substituted with ID from the CLI.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
id, err := parseContainerID(containerID) id, err := parseContainerID(containerID)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
eaclTable, err := parseEACL(eaclPathFrom) eaclTable, err := parseEACL(eaclPathFrom)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
tok, err := getSessionToken(sessionTokenPath) tok, err := getSessionToken(sessionTokenPath)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
eaclTable.SetCID(*id) eaclTable.SetCID(*id)
eaclTable.SetSessionToken(tok) eaclTable.SetSessionToken(tok)
@ -434,11 +435,11 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
setEACLPrm.SetTable(*eaclTable) setEACLPrm.SetTable(*eaclTable)
_, err = internalclient.SetEACL(setEACLPrm) _, err = internalclient.SetEACL(setEACLPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
if containerAwait { if containerAwait {
exp, err := eaclTable.Marshal() exp, err := eaclTable.Marshal()
exitOnErr(cmd, errf("broken EACL table: %w", err)) common.ExitOnErr(cmd, "broken EACL table: %w", err)
cmd.Println("awaiting...") cmd.Println("awaiting...")
@ -462,7 +463,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
} }
} }
exitOnErr(cmd, errSetEACLTimeout) common.ExitOnErr(cmd, "", errSetEACLTimeout)
} }
}, },
} }

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir" ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
@ -220,11 +221,11 @@ func verifyResponseControl(cmd *cobra.Command,
}, },
) { ) {
if sigControl == nil { if sigControl == nil {
exitOnErr(cmd, errors.New("missing response signature")) common.ExitOnErr(cmd, "", errors.New("missing response signature"))
} }
bodyData, err := body.StableMarshal(nil) bodyData, err := body.StableMarshal(nil)
exitOnErr(cmd, errf("marshal response body: %w", err)) common.ExitOnErr(cmd, "marshal response body: %w", err)
// TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion // TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion
var sigV2 refs.Signature var sigV2 refs.Signature
@ -236,16 +237,16 @@ func verifyResponseControl(cmd *cobra.Command,
sig.ReadFromV2(sigV2) sig.ReadFromV2(sigV2)
if !sig.Verify(bodyData) { if !sig.Verify(bodyData) {
exitOnErr(cmd, errors.New("invalid response signature")) common.ExitOnErr(cmd, "", errors.New("invalid response signature"))
} }
} }
func healthCheck(cmd *cobra.Command, _ []string) { func healthCheck(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
if healthCheckIRVar { if healthCheckIRVar {
healthCheckIR(cmd, key, cli) healthCheckIR(cmd, key, cli)
@ -257,14 +258,14 @@ func healthCheck(cmd *cobra.Command, _ []string) {
req.SetBody(new(control.HealthCheckRequest_Body)) req.SetBody(new(control.HealthCheckRequest_Body))
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign message: %w", err)) common.ExitOnErr(cmd, "could not sign message: %w", err)
var resp *control.HealthCheckResponse var resp *control.HealthCheckResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.HealthCheck(client, req) resp, err = control.HealthCheck(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -278,14 +279,14 @@ func healthCheckIR(cmd *cobra.Command, key *ecdsa.PrivateKey, c *client.Client)
req.SetBody(new(ircontrol.HealthCheckRequest_Body)) req.SetBody(new(ircontrol.HealthCheckRequest_Body))
err := ircontrolsrv.SignMessage(key, req) err := ircontrolsrv.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
var resp *ircontrol.HealthCheckResponse var resp *ircontrol.HealthCheckResponse
err = c.ExecRaw(func(client *rawclient.Client) error { err = c.ExecRaw(func(client *rawclient.Client) error {
resp, err = ircontrol.HealthCheck(client, req) resp, err = ircontrol.HealthCheck(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -294,13 +295,13 @@ func healthCheckIR(cmd *cobra.Command, key *ecdsa.PrivateKey, c *client.Client)
func setNetmapStatus(cmd *cobra.Command, _ []string) { func setNetmapStatus(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var status control.NetmapStatus var status control.NetmapStatus
switch netmapStatus { switch netmapStatus {
default: default:
exitOnErr(cmd, fmt.Errorf("unsupported status %s", netmapStatus)) common.ExitOnErr(cmd, "", fmt.Errorf("unsupported status %s", netmapStatus))
case netmapStatusOnline: case netmapStatusOnline:
status = control.NetmapStatus_ONLINE status = control.NetmapStatus_ONLINE
case netmapStatusOffline: case netmapStatusOffline:
@ -317,17 +318,17 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) {
body.SetStatus(status) body.SetStatus(status)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.SetNetmapStatusResponse var resp *control.SetNetmapStatusResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.SetNetmapStatus(client, req) resp, err = control.SetNetmapStatus(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -344,7 +345,7 @@ var dropObjectsCmd = &cobra.Command{
Long: "Drop objects from the node's local storage", Long: "Drop objects from the node's local storage",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
binAddrList := make([][]byte, 0, len(dropObjectsList)) binAddrList := make([][]byte, 0, len(dropObjectsList))
@ -353,11 +354,11 @@ var dropObjectsCmd = &cobra.Command{
err := a.Parse(dropObjectsList[i]) err := a.Parse(dropObjectsList[i])
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("could not parse address #%d: %w", i, err)) common.ExitOnErr(cmd, "", fmt.Errorf("could not parse address #%d: %w", i, err))
} }
binAddr, err := a.Marshal() binAddr, err := a.Marshal()
exitOnErr(cmd, errf("could not marshal the address: %w", err)) common.ExitOnErr(cmd, "could not marshal the address: %w", err)
binAddrList = append(binAddrList, binAddr) binAddrList = append(binAddrList, binAddr)
} }
@ -370,17 +371,17 @@ var dropObjectsCmd = &cobra.Command{
body.SetAddressList(binAddrList) body.SetAddressList(binAddrList)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.DropObjectsResponse var resp *control.DropObjectsResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.DropObjects(client, req) resp, err = control.DropObjects(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -394,23 +395,23 @@ var snapshotCmd = &cobra.Command{
Long: "Get network map snapshot", Long: "Get network map snapshot",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
req := new(control.NetmapSnapshotRequest) req := new(control.NetmapSnapshotRequest)
req.SetBody(new(control.NetmapSnapshotRequest_Body)) req.SetBody(new(control.NetmapSnapshotRequest_Body))
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.NetmapSnapshotResponse var resp *control.NetmapSnapshotResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.NetmapSnapshot(client, req) resp, err = control.NetmapSnapshot(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -420,23 +421,23 @@ var snapshotCmd = &cobra.Command{
func listShards(cmd *cobra.Command, _ []string) { func listShards(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
req := new(control.ListShardsRequest) req := new(control.ListShardsRequest)
req.SetBody(new(control.ListShardsRequest_Body)) req.SetBody(new(control.ListShardsRequest_Body))
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.ListShardsResponse var resp *control.ListShardsResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.ListShards(client, req) resp, err = control.ListShards(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
@ -484,13 +485,13 @@ func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) {
func setShardMode(cmd *cobra.Command, _ []string) { func setShardMode(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var mode control.ShardMode var mode control.ShardMode
switch shardMode { switch shardMode {
default: default:
exitOnErr(cmd, fmt.Errorf("unsupported mode %s", mode)) common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", mode))
case shardModeReadWrite: case shardModeReadWrite:
mode = control.ShardMode_READ_WRITE mode = control.ShardMode_READ_WRITE
case shardModeReadOnly: case shardModeReadOnly:
@ -505,7 +506,7 @@ func setShardMode(cmd *cobra.Command, _ []string) {
req.SetBody(body) req.SetBody(body)
rawID, err := base58.Decode(shardID) rawID, err := base58.Decode(shardID)
exitOnErr(cmd, errf("incorrect shard ID encoding: %w", err)) common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
body.SetMode(mode) body.SetMode(mode)
body.SetShardID(rawID) body.SetShardID(rawID)
@ -514,17 +515,17 @@ func setShardMode(cmd *cobra.Command, _ []string) {
body.ClearErrorCounter(reset) body.ClearErrorCounter(reset)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.SetShardModeResponse var resp *control.SetShardModeResponse
err = cli.ExecRaw(func(client *rawclient.Client) error { err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.SetShardMode(client, req) resp, err = control.SetShardMode(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
@ -23,12 +24,12 @@ var dumpShardCmd = &cobra.Command{
func dumpShard(cmd *cobra.Command, _ []string) { func dumpShard(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
body := new(control.DumpShardRequest_Body) body := new(control.DumpShardRequest_Body)
rawID, err := base58.Decode(shardID) rawID, err := base58.Decode(shardID)
exitOnErr(cmd, errf("incorrect shard ID encoding: %w", err)) common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
body.SetShardID(rawID) body.SetShardID(rawID)
p, _ := cmd.Flags().GetString(dumpFilepathFlag) p, _ := cmd.Flags().GetString(dumpFilepathFlag)
@ -41,17 +42,17 @@ func dumpShard(cmd *cobra.Command, _ []string) {
req.SetBody(body) req.SetBody(body)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.DumpShardResponse var resp *control.DumpShardResponse
err = cli.ExecRaw(func(client *client.Client) error { err = cli.ExecRaw(func(client *client.Client) error {
resp, err = control.DumpShard(client, req) resp, err = control.DumpShard(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object" objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
@ -22,7 +23,7 @@ var cmdObjectLock = &cobra.Command{
var cnr cid.ID var cnr cid.ID
err := cnr.DecodeString(args[0]) err := cnr.DecodeString(args[0])
exitOnErr(cmd, errf("Incorrect container arg: %v", err)) common.ExitOnErr(cmd, "Incorrect container arg: %v", err)
argsList := args[1:] argsList := args[1:]
@ -30,14 +31,14 @@ var cmdObjectLock = &cobra.Command{
for i := range argsList { for i := range argsList {
err = lockList[i].DecodeString(argsList[i]) err = lockList[i].DecodeString(argsList[i])
exitOnErr(cmd, errf(fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)) common.ExitOnErr(cmd, fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)
} }
key, err := getKey() key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err)) common.ExitOnErr(cmd, "can't fetch private key: %w", err)
idOwner, err := getOwnerID(key) idOwner, err := getOwnerID(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var lock object.Lock var lock object.Lock
lock.WriteMembers(lockList) lock.WriteMembers(lockList)
@ -55,7 +56,7 @@ var cmdObjectLock = &cobra.Command{
prm.SetHeader(obj) prm.SetHeader(obj)
_, err = internalclient.PutObject(prm) _, err = internalclient.PutObject(prm)
exitOnErr(cmd, errf("Store lock object in NeoFS: %w", err)) common.ExitOnErr(cmd, "Store lock object in NeoFS: %w", err)
cmd.Println("Objects successfully locked.") cmd.Println("Objects successfully locked.")
}, },

View file

@ -7,6 +7,7 @@ import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/config/netmode"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
@ -67,7 +68,7 @@ var getEpochCmd = &cobra.Command{
prepareAPIClient(cmd, &prm) prepareAPIClient(cmd, &prm)
res, err := internalclient.NetworkInfo(prm) res, err := internalclient.NetworkInfo(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
netInfo := res.NetworkInfo() netInfo := res.NetworkInfo()
@ -85,7 +86,7 @@ var localNodeInfoCmd = &cobra.Command{
prepareAPIClient(cmd, &prm) prepareAPIClient(cmd, &prm)
res, err := internalclient.NodeInfo(prm) res, err := internalclient.NodeInfo(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
prettyPrintNodeInfo(cmd, res.NodeInfo(), nodeInfoJSON) prettyPrintNodeInfo(cmd, res.NodeInfo(), nodeInfoJSON)
}, },
@ -157,7 +158,7 @@ var netInfoCmd = &cobra.Command{
prepareAPIClient(cmd, &prm) prepareAPIClient(cmd, &prm)
res, err := internalclient.NetworkInfo(prm) res, err := internalclient.NetworkInfo(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
netInfo := res.NetworkInfo() netInfo := res.NetworkInfo()
@ -182,7 +183,7 @@ var netInfoCmd = &cobra.Command{
return err return err
}) })
exitOnErr(cmd, errf("read config: %w", err)) common.ExitOnErr(cmd, "read config: %w", err)
}, },
} }

View file

@ -18,6 +18,7 @@ import (
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/bearer"
@ -318,14 +319,14 @@ type clientKeySession interface {
func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) { func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) {
key, err := getKey() key, err := getKey()
exitOnErr(cmd, errf("get private key: %w", err)) common.ExitOnErr(cmd, "get private key: %w", err)
prepareSessionPrmWithKey(cmd, addr, key, prms...) prepareSessionPrmWithKey(cmd, addr, key, prms...)
} }
func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) { func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) {
ownerID, err := getOwnerID(key) ownerID, err := getOwnerID(key)
exitOnErr(cmd, errf("owner ID from key: %w", err)) common.ExitOnErr(cmd, "owner ID from key: %w", err)
prepareSessionPrmWithOwner(cmd, addr, key, ownerID, prms...) prepareSessionPrmWithOwner(cmd, addr, key, ownerID, prms...)
} }
@ -338,21 +339,21 @@ func prepareSessionPrmWithOwner(
prms ...clientKeySession, prms ...clientKeySession,
) { ) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC)
exitOnErr(cmd, errf("create API client: %w", err)) common.ExitOnErr(cmd, "create API client: %w", err)
var sessionToken *session.Token var sessionToken *session.Token
if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 { if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 {
data, err := ioutil.ReadFile(tokenPath) data, err := ioutil.ReadFile(tokenPath)
exitOnErr(cmd, errf("can't read session token: %w", err)) common.ExitOnErr(cmd, "can't read session token: %w", err)
sessionToken = session.NewToken() sessionToken = session.NewToken()
if err := sessionToken.Unmarshal(data); err != nil { if err := sessionToken.Unmarshal(data); err != nil {
err = sessionToken.UnmarshalJSON(data) err = sessionToken.UnmarshalJSON(data)
exitOnErr(cmd, errf("can't unmarshal session token: %w", err)) common.ExitOnErr(cmd, "can't unmarshal session token: %w", err)
} }
} else { } else {
sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime) sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
} }
for i := range prms { for i := range prms {
@ -387,7 +388,7 @@ func prepareSessionPrmWithOwner(
tok.SetNbf(sessionToken.Nbf()) tok.SetNbf(sessionToken.Nbf())
err = tok.Sign(key) err = tok.Sign(key)
exitOnErr(cmd, errf("session token signing: %w", err)) common.ExitOnErr(cmd, "session token signing: %w", err)
prms[i].SetClient(cli) prms[i].SetClient(cli)
prms[i].SetSessionToken(tok) prms[i].SetSessionToken(tok)
@ -421,21 +422,21 @@ func prepareObjectPrmRaw(cmd *cobra.Command, prm interface {
func putObject(cmd *cobra.Command, _ []string) { func putObject(cmd *cobra.Command, _ []string) {
key, err := getKey() key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err)) common.ExitOnErr(cmd, "can't fetch private key: %w", err)
ownerID, err := getOwnerID(key) ownerID, err := getOwnerID(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
filename := cmd.Flag("file").Value.String() filename := cmd.Flag("file").Value.String()
f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm) f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
} }
attrs, err := parseObjectAttrs(cmd) attrs, err := parseObjectAttrs(cmd)
exitOnErr(cmd, errf("can't parse object attributes: %w", err)) common.ExitOnErr(cmd, "can't parse object attributes: %w", err)
expiresOn, _ := cmd.Flags().GetUint64(putExpiresOnFlag) expiresOn, _ := cmd.Flags().GetUint64(putExpiresOnFlag)
if expiresOn > 0 { if expiresOn > 0 {
@ -464,7 +465,7 @@ func putObject(cmd *cobra.Command, _ []string) {
obj.SetAttributes(attrs...) obj.SetAttributes(attrs...)
notificationInfo, err := parseObjectNotifications(cmd) notificationInfo, err := parseObjectNotifications(cmd)
exitOnErr(cmd, errf("can't parse object notification information: %w", err)) common.ExitOnErr(cmd, "can't parse object notification information: %w", err)
if notificationInfo != nil { if notificationInfo != nil {
obj.SetNotification(*notificationInfo) obj.SetNotification(*notificationInfo)
@ -497,7 +498,7 @@ func putObject(cmd *cobra.Command, _ []string) {
} }
res, err := internalclient.PutObject(prm) res, err := internalclient.PutObject(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
if p != nil { if p != nil {
p.Finish() p.Finish()
@ -508,7 +509,7 @@ func putObject(cmd *cobra.Command, _ []string) {
func deleteObject(cmd *cobra.Command, _ []string) { func deleteObject(cmd *cobra.Command, _ []string) {
objAddr, err := getObjectAddress(cmd) objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var prm internalclient.DeleteObjectPrm var prm internalclient.DeleteObjectPrm
@ -517,7 +518,7 @@ func deleteObject(cmd *cobra.Command, _ []string) {
prm.SetAddress(objAddr) prm.SetAddress(objAddr)
res, err := internalclient.DeleteObject(prm) res, err := internalclient.DeleteObject(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
tombstoneAddr := res.TombstoneAddress() tombstoneAddr := res.TombstoneAddress()
@ -545,7 +546,7 @@ func deleteObject(cmd *cobra.Command, _ []string) {
func getObject(cmd *cobra.Command, _ []string) { func getObject(cmd *cobra.Command, _ []string) {
objAddr, err := getObjectAddress(cmd) objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var out io.Writer var out io.Writer
filename := cmd.Flag("file").Value.String() filename := cmd.Flag("file").Value.String()
@ -554,7 +555,7 @@ func getObject(cmd *cobra.Command, _ []string) {
} else { } else {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm) f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
} }
defer f.Close() defer f.Close()
@ -589,7 +590,7 @@ func getObject(cmd *cobra.Command, _ []string) {
return return
} }
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
} }
hdrFile := cmd.Flag("header").Value.String() hdrFile := cmd.Flag("header").Value.String()
@ -605,13 +606,13 @@ func getObject(cmd *cobra.Command, _ []string) {
// Print header only if file is not streamed to stdout. // Print header only if file is not streamed to stdout.
if filename != "" || hdrFile != "" { if filename != "" || hdrFile != "" {
err = saveAndPrintHeader(cmd, res.Header(), hdrFile) err = saveAndPrintHeader(cmd, res.Header(), hdrFile)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
} }
} }
func getObjectHeader(cmd *cobra.Command, _ []string) { func getObjectHeader(cmd *cobra.Command, _ []string) {
objAddr, err := getObjectAddress(cmd) objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
mainOnly, _ := cmd.Flags().GetBool("main-only") mainOnly, _ := cmd.Flags().GetBool("main-only")
@ -628,19 +629,19 @@ func getObjectHeader(cmd *cobra.Command, _ []string) {
return return
} }
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
} }
err = saveAndPrintHeader(cmd, res.Header(), cmd.Flag("file").Value.String()) err = saveAndPrintHeader(cmd, res.Header(), cmd.Flag("file").Value.String())
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
} }
func searchObject(cmd *cobra.Command, _ []string) { func searchObject(cmd *cobra.Command, _ []string) {
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
sf, err := parseSearchFilters(cmd) sf, err := parseSearchFilters(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var prm internalclient.SearchObjectsPrm var prm internalclient.SearchObjectsPrm
@ -652,7 +653,7 @@ func searchObject(cmd *cobra.Command, _ []string) {
prm.SetFilters(sf) prm.SetFilters(sf)
res, err := internalclient.SearchObjects(prm) res, err := internalclient.SearchObjects(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
ids := res.IDList() ids := res.IDList()
@ -664,16 +665,16 @@ func searchObject(cmd *cobra.Command, _ []string) {
func getObjectHash(cmd *cobra.Command, _ []string) { func getObjectHash(cmd *cobra.Command, _ []string) {
objAddr, err := getObjectAddress(cmd) objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
ranges, err := getRangeList(cmd) ranges, err := getRangeList(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
typ, err := getHashType(cmd) typ, err := getHashType(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
strSalt := strings.TrimPrefix(cmd.Flag(getRangeHashSaltFlag).Value.String(), "0x") strSalt := strings.TrimPrefix(cmd.Flag(getRangeHashSaltFlag).Value.String(), "0x")
salt, err := hex.DecodeString(strSalt) salt, err := hex.DecodeString(strSalt)
exitOnErr(cmd, errf("could not decode salt: %w", err)) common.ExitOnErr(cmd, "could not decode salt: %w", err)
var ( var (
hashPrm internalclient.HashPayloadRangesPrm hashPrm internalclient.HashPayloadRangesPrm
@ -699,7 +700,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
// get hash of full payload through HEAD (may be user can do it through dedicated command?) // get hash of full payload through HEAD (may be user can do it through dedicated command?)
res, err := internalclient.HeadObject(headPrm) res, err := internalclient.HeadObject(headPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
var cs checksum.Checksum var cs checksum.Checksum
var csSet bool var csSet bool
@ -728,7 +729,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
} }
res, err := internalclient.HashPayloadRanges(hashPrm) res, err := internalclient.HashPayloadRanges(hashPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
hs := res.HashList() hs := res.HashList()
@ -1134,13 +1135,13 @@ func getBearerToken(cmd *cobra.Command, flagname string) (*bearer.Token, error)
func getObjectRange(cmd *cobra.Command, _ []string) { func getObjectRange(cmd *cobra.Command, _ []string) {
objAddr, err := getObjectAddress(cmd) objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
ranges, err := getRangeList(cmd) ranges, err := getRangeList(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
if len(ranges) != 1 { if len(ranges) != 1 {
exitOnErr(cmd, fmt.Errorf("exactly one range must be specified, got: %d", len(ranges))) common.ExitOnErr(cmd, "", fmt.Errorf("exactly one range must be specified, got: %d", len(ranges)))
} }
var out io.Writer var out io.Writer
@ -1151,7 +1152,7 @@ func getObjectRange(cmd *cobra.Command, _ []string) {
} else { } else {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm) f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
} }
defer f.Close() defer f.Close()
@ -1173,7 +1174,7 @@ func getObjectRange(cmd *cobra.Command, _ []string) {
return return
} }
exitOnErr(cmd, fmt.Errorf("can't get object payload range: %w", err)) common.ExitOnErr(cmd, "can't get object payload range: %w", err)
} }
if filename != "" { if filename != "" {
@ -1196,7 +1197,7 @@ func printSplitInfoErr(cmd *cobra.Command, err error) bool {
func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) { func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) {
bs, err := marshalSplitInfo(cmd, info) bs, err := marshalSplitInfo(cmd, info)
exitOnErr(cmd, errf("can't marshal split info: %w", err)) common.ExitOnErr(cmd, "can't marshal split info: %w", err)
cmd.Println(string(bs)) cmd.Println(string(bs))
} }

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
@ -23,12 +24,12 @@ var restoreShardCmd = &cobra.Command{
func restoreShard(cmd *cobra.Command, _ []string) { func restoreShard(cmd *cobra.Command, _ []string) {
key, err := getKeyNoGenerate() key, err := getKeyNoGenerate()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
body := new(control.RestoreShardRequest_Body) body := new(control.RestoreShardRequest_Body)
rawID, err := base58.Decode(shardID) rawID, err := base58.Decode(shardID)
exitOnErr(cmd, errf("incorrect shard ID encoding: %w", err)) common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
body.SetShardID(rawID) body.SetShardID(rawID)
p, _ := cmd.Flags().GetString(restoreFilepathFlag) p, _ := cmd.Flags().GetString(restoreFilepathFlag)
@ -41,17 +42,17 @@ func restoreShard(cmd *cobra.Command, _ []string) {
req.SetBody(body) req.SetBody(body)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) common.ExitOnErr(cmd, "could not sign request: %w", err)
cli, err := getControlSDKClient(key) cli, err := getControlSDKClient(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var resp *control.RestoreShardResponse var resp *control.RestoreShardResponse
err = cli.ExecRaw(func(client *client.Client) error { err = cli.ExecRaw(func(client *client.Client) error {
resp, err = control.RestoreShard(client, req) resp, err = control.RestoreShard(client, req)
return err return err
}) })
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody()) verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())

View file

@ -10,6 +10,7 @@ import (
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
@ -65,7 +66,7 @@ and much more!`,
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
exitOnErr(rootCmd, err) common.ExitOnErr(rootCmd, "", err)
} }
func init() { func init() {
@ -116,7 +117,7 @@ func initConfig() {
} else { } else {
// Find home directory. // Find home directory.
home, err := homedir.Dir() home, err := homedir.Dir()
exitOnErr(rootCmd, err) common.ExitOnErr(rootCmd, "", err)
// Search config in `$HOME/.config/neofs-cli/` with name "config.yaml" // Search config in `$HOME/.config/neofs-cli/` with name "config.yaml"
viper.AddConfigPath(filepath.Join(home, ".config", "neofs-cli")) viper.AddConfigPath(filepath.Join(home, ".config", "neofs-cli"))
@ -149,7 +150,7 @@ type clientWithKey interface {
// reads private key from command args and call prepareAPIClientWithKey with it. // reads private key from command args and call prepareAPIClientWithKey with it.
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
p, err := getKey() p, err := getKey()
exitOnErr(cmd, errf("get private key: %w", err)) common.ExitOnErr(cmd, "get private key: %w", err)
prepareAPIClientWithKey(cmd, p, dst...) prepareAPIClientWithKey(cmd, p, dst...)
} }
@ -157,7 +158,7 @@ func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
// creates NeoFS API client and writes it to target along with the private key. // creates NeoFS API client and writes it to target along with the private key.
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) { func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC)
exitOnErr(cmd, errf("create API client: %w", err)) common.ExitOnErr(cmd, "create API client: %w", err)
for _, d := range dst { for _, d := range dst {
d.SetClient(cli) d.SetClient(cli)
@ -170,7 +171,7 @@ type bearerPrm interface {
func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
btok, err := getBearerToken(cmd, bearerTokenFlag) btok, err := getBearerToken(cmd, bearerTokenFlag)
exitOnErr(cmd, errf("bearer token: %w", err)) common.ExitOnErr(cmd, "bearer token: %w", err)
prm.SetBearerToken(btok) prm.SetBearerToken(btok)
} }

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
@ -163,19 +164,19 @@ func (c sgHeadReceiver) Head(addr *addressSDK.Address) (interface{}, error) {
func putSG(cmd *cobra.Command, _ []string) { func putSG(cmd *cobra.Command, _ []string) {
key, err := getKey() key, err := getKey()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
ownerID, err := getOwnerID(key) ownerID, err := getOwnerID(key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
members := make([]oidSDK.ID, len(sgMembers)) members := make([]oidSDK.ID, len(sgMembers))
for i := range sgMembers { for i := range sgMembers {
err = members[i].DecodeString(sgMembers[i]) err = members[i].DecodeString(sgMembers[i])
exitOnErr(cmd, errf("could not parse object ID: %w", err)) common.ExitOnErr(cmd, "could not parse object ID: %w", err)
} }
var ( var (
@ -196,10 +197,10 @@ func putSG(cmd *cobra.Command, _ []string) {
ownerID: ownerID, ownerID: ownerID,
prm: headPrm, prm: headPrm,
}, cnr, members) }, cnr, members)
exitOnErr(cmd, errf("could not collect storage group members: %w", err)) common.ExitOnErr(cmd, "could not collect storage group members: %w", err)
sgContent, err := sg.Marshal() sgContent, err := sg.Marshal()
exitOnErr(cmd, errf("could not marshal storage group: %w", err)) common.ExitOnErr(cmd, "could not marshal storage group: %w", err)
obj := object.New() obj := object.New()
obj.SetContainerID(*cnr) obj.SetContainerID(*cnr)
@ -210,7 +211,7 @@ func putSG(cmd *cobra.Command, _ []string) {
putPrm.SetPayloadReader(bytes.NewReader(sgContent)) putPrm.SetPayloadReader(bytes.NewReader(sgContent))
res, err := internalclient.PutObject(putPrm) res, err := internalclient.PutObject(putPrm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
cmd.Println("Storage group successfully stored") cmd.Println("Storage group successfully stored")
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cnr) cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cnr)
@ -228,10 +229,10 @@ func getSGID() (*oidSDK.ID, error) {
func getSG(cmd *cobra.Command, _ []string) { func getSG(cmd *cobra.Command, _ []string) {
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
id, err := getSGID() id, err := getSGID()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
addr := addressSDK.NewAddress() addr := addressSDK.NewAddress()
addr.SetContainerID(*cnr) addr.SetContainerID(*cnr)
@ -247,12 +248,12 @@ func getSG(cmd *cobra.Command, _ []string) {
prm.SetPayloadWriter(buf) prm.SetPayloadWriter(buf)
_, err = internalclient.GetObject(prm) _, err = internalclient.GetObject(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
var sg storagegroupAPI.StorageGroup var sg storagegroupAPI.StorageGroup
err = sg.Unmarshal(buf.Bytes()) err = sg.Unmarshal(buf.Bytes())
exitOnErr(cmd, errf("could not unmarshal storage group: %w", err)) common.ExitOnErr(cmd, "could not unmarshal storage group: %w", err)
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch()) cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
cmd.Printf("Group size: %d\n", sg.ValidationDataSize()) cmd.Printf("Group size: %d\n", sg.ValidationDataSize())
@ -269,7 +270,7 @@ func getSG(cmd *cobra.Command, _ []string) {
func listSG(cmd *cobra.Command, _ []string) { func listSG(cmd *cobra.Command, _ []string) {
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var prm internalclient.SearchObjectsPrm var prm internalclient.SearchObjectsPrm
@ -281,7 +282,7 @@ func listSG(cmd *cobra.Command, _ []string) {
prm.SetFilters(storagegroup.SearchQuery()) prm.SetFilters(storagegroup.SearchQuery())
res, err := internalclient.SearchObjects(prm) res, err := internalclient.SearchObjects(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
ids := res.IDList() ids := res.IDList()
@ -294,10 +295,10 @@ func listSG(cmd *cobra.Command, _ []string) {
func delSG(cmd *cobra.Command, _ []string) { func delSG(cmd *cobra.Command, _ []string) {
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
id, err := getSGID() id, err := getSGID()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
addr := addressSDK.NewAddress() addr := addressSDK.NewAddress()
addr.SetContainerID(*cnr) addr.SetContainerID(*cnr)
@ -310,7 +311,7 @@ func delSG(cmd *cobra.Command, _ []string) {
prm.SetAddress(addr) prm.SetAddress(addr)
res, err := internalclient.DeleteObject(prm) res, err := internalclient.DeleteObject(prm)
exitOnErr(cmd, errf("rpc error: %w", err)) common.ExitOnErr(cmd, "rpc error: %w", err)
tombstone := res.TombstoneAddress() tombstone := res.TombstoneAddress()

View file

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/util/keyer" "github.com/nspcc-dev/neofs-node/pkg/util/keyer"
locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db" locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db"
@ -17,7 +18,6 @@ import (
locodebolt "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/boltdb" locodebolt "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/boltdb"
continentsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/continents/geojson" continentsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/continents/geojson"
csvlocode "github.com/nspcc-dev/neofs-node/pkg/util/locode/table/csv" csvlocode "github.com/nspcc-dev/neofs-node/pkg/util/locode/table/csv"
sdkstatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -126,7 +126,7 @@ var (
}) })
err := targetDB.Open() err := targetDB.Open()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
defer targetDB.Close() defer targetDB.Close()
@ -136,7 +136,7 @@ var (
} }
err = locodedb.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB) err = locodedb.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
}, },
} }
) )
@ -159,12 +159,12 @@ var (
}, locodebolt.ReadOnly()) }, locodebolt.ReadOnly())
err := targetDB.Open() err := targetDB.Open()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
defer targetDB.Close() defer targetDB.Close()
record, err := locodedb.LocodeRecord(targetDB, locodeInfoCode) record, err := locodedb.LocodeRecord(targetDB, locodeInfoCode)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
cmd.Printf("Country: %s\n", record.CountryName()) cmd.Printf("Country: %s\n", record.CountryName())
cmd.Printf("Location: %s\n", record.LocationName()) cmd.Printf("Location: %s\n", record.LocationName())
@ -281,13 +281,13 @@ func init() {
func signBearerToken(cmd *cobra.Command, _ []string) { func signBearerToken(cmd *cobra.Command, _ []string) {
btok, err := getBearerToken(cmd, "from") btok, err := getBearerToken(cmd, "from")
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
key, err := getKey() key, err := getKey()
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
err = btok.Sign(*key) err = btok.Sign(*key)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
to := cmd.Flag("to").Value.String() to := cmd.Flag("to").Value.String()
jsonFlag, _ := cmd.Flags().GetBool("json") jsonFlag, _ := cmd.Flags().GetBool("json")
@ -295,7 +295,7 @@ func signBearerToken(cmd *cobra.Command, _ []string) {
var data []byte var data []byte
if jsonFlag || len(to) == 0 { if jsonFlag || len(to) == 0 {
data, err = btok.MarshalJSON() data, err = btok.MarshalJSON()
exitOnErr(cmd, errf("can't JSON encode bearer token: %w", err)) common.ExitOnErr(cmd, "can't JSON encode bearer token: %w", err)
} else { } else {
data = btok.Marshal() data = btok.Marshal()
} }
@ -307,28 +307,28 @@ func signBearerToken(cmd *cobra.Command, _ []string) {
} }
err = os.WriteFile(to, data, 0644) err = os.WriteFile(to, data, 0644)
exitOnErr(cmd, errf("can't write signed bearer token to file: %w", err)) common.ExitOnErr(cmd, "can't write signed bearer token to file: %w", err)
cmd.Printf("signed bearer token was successfully dumped to %s\n", to) cmd.Printf("signed bearer token was successfully dumped to %s\n", to)
} }
func signSessionToken(cmd *cobra.Command, _ []string) { func signSessionToken(cmd *cobra.Command, _ []string) {
path, err := cmd.Flags().GetString("from") path, err := cmd.Flags().GetString("from")
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
stok, err := getSessionToken(path) stok, err := getSessionToken(path)
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("can't read session token from %s: %w", path, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't read session token from %s: %w", path, err))
} }
key, err := getKey() key, err := getKey()
exitOnErr(cmd, errf("can't get private key, make sure it is provided: %w", err)) common.ExitOnErr(cmd, "can't get private key, make sure it is provided: %w", err)
err = stok.Sign(key) err = stok.Sign(key)
exitOnErr(cmd, errf("can't sign token: %w", err)) common.ExitOnErr(cmd, "can't sign token: %w", err)
data, err := stok.MarshalJSON() data, err := stok.MarshalJSON()
exitOnErr(cmd, errf("can't encode session token: %w", err)) common.ExitOnErr(cmd, "can't encode session token: %w", err)
to := cmd.Flag("to").Value.String() to := cmd.Flag("to").Value.String()
if len(to) == 0 { if len(to) == 0 {
@ -338,7 +338,7 @@ func signSessionToken(cmd *cobra.Command, _ []string) {
err = os.WriteFile(to, data, 0644) err = os.WriteFile(to, data, 0644)
if err != nil { if err != nil {
exitOnErr(cmd, fmt.Errorf("can't write signed session token to %s: %w", to, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't write signed session token to %s: %w", to, err))
} }
cmd.Printf("signed session token saved in %s\n", to) cmd.Printf("signed session token saved in %s\n", to)
@ -350,24 +350,23 @@ func convertEACLTable(cmd *cobra.Command, _ []string) {
jsonFlag, _ := cmd.Flags().GetBool("json") jsonFlag, _ := cmd.Flags().GetBool("json")
table, err := parseEACL(pathFrom) table, err := parseEACL(pathFrom)
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
var data []byte var data []byte
if jsonFlag || len(to) == 0 { if jsonFlag || len(to) == 0 {
data, err = table.MarshalJSON() data, err = table.MarshalJSON()
exitOnErr(cmd, errf("can't JSON encode extended ACL table: %w", err)) common.ExitOnErr(cmd, "can't JSON encode extended ACL table: %w", err)
} else { } else {
data, err = table.Marshal() data, err = table.Marshal()
exitOnErr(cmd, errf("can't binary encode extended ACL table: %w", err)) common.ExitOnErr(cmd, "can't binary encode extended ACL table: %w", err)
} }
if len(to) == 0 { if len(to) == 0 {
prettyPrintJSON(cmd, data) prettyPrintJSON(cmd, data)
return return
} }
err = os.WriteFile(to, data, 0644) err = os.WriteFile(to, data, 0644)
exitOnErr(cmd, errf("can't write exteded ACL table to file: %w", err)) common.ExitOnErr(cmd, "can't write exteded ACL table to file: %w", err)
cmd.Printf("extended ACL table was successfully dumped to %s\n", to) cmd.Printf("extended ACL table was successfully dumped to %s\n", to)
} }
@ -387,7 +386,7 @@ func processKeyer(cmd *cobra.Command, args []string) {
err = result.ParseMultiSig(args) err = result.ParseMultiSig(args)
} else { } else {
if len(args) > 1 { if len(args) > 1 {
exitOnErr(cmd, errKeyerSingleArgument) common.ExitOnErr(cmd, "", errKeyerSingleArgument)
} }
var argument string var argument string
@ -405,7 +404,7 @@ func processKeyer(cmd *cobra.Command, args []string) {
} }
} }
exitOnErr(cmd, err) common.ExitOnErr(cmd, "", err)
result.PrettyPrint(uncompressed, useHex) result.PrettyPrint(uncompressed, useHex)
} }
@ -467,49 +466,3 @@ func keyerParseFile(filename string, d *keyer.Dashboard) error {
return d.ParseBinary(data) return d.ParseBinary(data)
} }
// 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 prints error and exits with a code that matches
// one of the common errors from sdk library. If no errors
// found, exits with 1 code.
// Does nothing if passed error in nil.
func exitOnErr(cmd *cobra.Command, err error) {
if err == nil {
return
}
const (
_ = iota
internal
aclDenied
)
var (
code int
internalErr = new(sdkstatus.ServerInternal)
accessErr = new(sdkstatus.ObjectAccessDenied)
)
switch {
case errors.As(err, &internalErr):
code = internal
case errors.As(err, &accessErr):
code = aclDenied
err = fmt.Errorf("%w: %s", err, accessErr.Reason())
default:
code = internal
}
cmd.PrintErrln(err)
os.Exit(code)
}