2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-12 13:57:37 +00:00
|
|
|
"crypto/ecdsa"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-06-24 12:31:50 +00:00
|
|
|
"path/filepath"
|
2020-12-24 11:25:36 +00:00
|
|
|
"strings"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2022-03-28 09:36:04 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-03-28 10:23:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-05-18 11:17:31 +00:00
|
|
|
accountingCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/accounting"
|
2022-01-20 15:34:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
|
2022-03-28 09:09:49 +00:00
|
|
|
bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer"
|
2022-05-23 16:26:27 +00:00
|
|
|
controlCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/control"
|
2022-03-28 10:32:18 +00:00
|
|
|
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
|
2022-05-25 16:15:27 +00:00
|
|
|
utilCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util"
|
2021-06-20 16:11:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/misc"
|
2022-05-18 15:21:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/gendoc"
|
2022-05-12 07:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
const (
|
|
|
|
envPrefix = "NEOFS_CLI"
|
|
|
|
)
|
2020-10-12 14:35:04 +00:00
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
// Global scope flags.
|
|
|
|
var (
|
2020-10-12 15:14:17 +00:00
|
|
|
cfgFile string
|
2020-10-12 13:57:37 +00:00
|
|
|
)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "neofs-cli",
|
|
|
|
Short: "Command Line Tool to work with NeoFS",
|
|
|
|
Long: `NeoFS CLI provides all basic interactions with NeoFS and it's services.
|
|
|
|
|
|
|
|
It contains commands for interaction with NeoFS nodes using different versions
|
|
|
|
of neofs-api and some useful utilities for compiling ACL rules from JSON
|
|
|
|
notation, managing container access through protocol gates, querying network map
|
|
|
|
and much more!`,
|
2021-06-20 16:11:49 +00:00
|
|
|
Run: entryPoint,
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
2021-07-06 12:27:54 +00:00
|
|
|
err := rootCmd.Execute()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(rootCmd, "", err)
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
2020-11-05 13:49:05 +00:00
|
|
|
// use stdout as default output for cmd.Print()
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
2020-10-12 13:57:37 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)")
|
2022-05-18 09:22:02 +00:00
|
|
|
rootCmd.PersistentFlags().BoolP(commonflags.Verbose, commonflags.VerboseShorthand,
|
|
|
|
false, commonflags.VerboseUsage)
|
2022-01-28 18:03:44 +00:00
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.Verbose, rootCmd.PersistentFlags().Lookup(commonflags.Verbose))
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
2021-06-20 16:11:49 +00:00
|
|
|
rootCmd.Flags().Bool("version", false, "Application version and NeoFS API compatibility")
|
2022-01-20 15:34:53 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(acl.Cmd)
|
2022-03-28 09:09:49 +00:00
|
|
|
rootCmd.AddCommand(bearerCli.Cmd)
|
2022-03-28 10:32:18 +00:00
|
|
|
rootCmd.AddCommand(sessionCli.Cmd)
|
2022-05-18 11:17:31 +00:00
|
|
|
rootCmd.AddCommand(accountingCli.Cmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
rootCmd.AddCommand(controlCli.Cmd)
|
2022-05-25 16:15:27 +00:00
|
|
|
rootCmd.AddCommand(utilCli.Cmd)
|
2022-05-18 15:21:54 +00:00
|
|
|
rootCmd.AddCommand(gendoc.Command(rootCmd))
|
2021-06-20 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func entryPoint(cmd *cobra.Command, _ []string) {
|
|
|
|
printVersion, _ := cmd.Flags().GetBool("version")
|
|
|
|
if printVersion {
|
|
|
|
cmd.Printf(
|
|
|
|
"Version: %s \nBuild: %s \nDebug: %s\n",
|
|
|
|
misc.Version,
|
|
|
|
misc.Build,
|
|
|
|
misc.Debug,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = cmd.Usage()
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := homedir.Dir()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(rootCmd, "", err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-06-25 00:23:10 +00:00
|
|
|
// Search config in `$HOME/.config/neofs-cli/` with name "config.yaml"
|
2021-06-24 12:31:50 +00:00
|
|
|
viper.AddConfigPath(filepath.Join(home, ".config", "neofs-cli"))
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
viper.SetEnvPrefix(envPrefix)
|
2020-08-04 14:46:12 +00:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2022-05-23 15:48:01 +00:00
|
|
|
common.PrintVerbose("Using config file: %s", viper.ConfigFileUsed())
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:57:37 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
type clientWithKey interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
SetClient(*client.Client)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads private key from command args and call prepareAPIClientWithKey with it.
|
|
|
|
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
|
2022-05-24 08:22:23 +00:00
|
|
|
p := key.GetOrGenerate(cmd)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2022-03-28 10:23:45 +00:00
|
|
|
prepareAPIClientWithKey(cmd, p, dst...)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// creates NeoFS API client and writes it to target along with the private key.
|
|
|
|
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
|
2022-05-24 08:38:45 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
for _, d := range dst {
|
|
|
|
d.SetClient(cli)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bearerPrm interface {
|
2022-05-12 07:22:02 +00:00
|
|
|
SetBearerToken(prm *bearer.Token)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
|
2022-05-25 16:15:27 +00:00
|
|
|
btok := common.ReadBearerToken(cmd, bearerTokenFlag)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
prm.SetBearerToken(btok)
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
func getTTL() uint32 {
|
2022-05-30 13:04:40 +00:00
|
|
|
ttl := viper.GetUint32(commonflags.TTL)
|
2022-05-23 15:48:01 +00:00
|
|
|
common.PrintVerbose("TTL: %d", ttl)
|
2020-11-16 08:25:42 +00:00
|
|
|
|
|
|
|
return ttl
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
// userFromString decodes user ID from string input.
|
|
|
|
func userFromString(id *user.ID, s string) error {
|
|
|
|
err := id.DecodeString(s)
|
2020-10-15 08:45:00 +00:00
|
|
|
if err != nil {
|
2022-05-17 13:59:46 +00:00
|
|
|
return fmt.Errorf("invalid user ID: %w", err)
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
return nil
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
2020-10-14 12:36:27 +00:00
|
|
|
|
2022-05-30 13:04:40 +00:00
|
|
|
func parseXHeaders(cmd *cobra.Command) []string {
|
|
|
|
xHeaders, _ := cmd.Flags().GetStringSlice(commonflags.XHeadersKey)
|
2022-05-18 15:20:08 +00:00
|
|
|
xs := make([]string, 0, 2*len(xHeaders))
|
2020-12-24 11:25:36 +00:00
|
|
|
|
|
|
|
for i := range xHeaders {
|
|
|
|
kv := strings.SplitN(xHeaders[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
panic(fmt.Errorf("invalid X-Header format: %s", xHeaders[i]))
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
xs = append(xs, kv[0], kv[1])
|
2020-12-24 11:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return xs
|
|
|
|
}
|