2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-06-24 12:31:50 +00:00
|
|
|
"path/filepath"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
accountingCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/accounting"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/acl"
|
|
|
|
bearerCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/bearer"
|
|
|
|
containerCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/container"
|
|
|
|
controlCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/control"
|
|
|
|
netmapCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/netmap"
|
|
|
|
objectCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/object"
|
|
|
|
sessionCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/session"
|
|
|
|
sgCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/storagegroup"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/tree"
|
|
|
|
utilCli "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/util"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/misc"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util/gendoc"
|
2020-10-12 13:57:37 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
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
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// rootCmd represents the base command when called without any subcommands.
|
2020-08-04 14:46:12 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
2022-12-23 17:35:35 +00:00
|
|
|
Use: "frostfs-cli",
|
2020-08-04 14:46:12 +00:00
|
|
|
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
|
2022-12-23 17:35:35 +00:00
|
|
|
of frostfs-api and some useful utilities for compiling ACL rules from JSON
|
2020-08-04 14:46:12 +00:00
|
|
|
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.
|
2022-12-23 17:35:35 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default is $HOME/.config/frostfs-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-31 06:53:54 +00:00
|
|
|
rootCmd.AddCommand(netmapCli.Cmd)
|
2022-06-01 12:42:28 +00:00
|
|
|
rootCmd.AddCommand(objectCli.Cmd)
|
2022-06-02 12:07:30 +00:00
|
|
|
rootCmd.AddCommand(sgCli.Cmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
rootCmd.AddCommand(containerCli.Cmd)
|
2022-10-10 15:35:40 +00:00
|
|
|
rootCmd.AddCommand(tree.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 {
|
2022-07-08 12:45:21 +00:00
|
|
|
cmd.Print(misc.BuildInfo("NeoFS CLI"))
|
2021-06-20 16:11:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// Search config in `$HOME/.config/frostfs-cli/` with name "config.yaml"
|
|
|
|
viper.AddConfigPath(filepath.Join(home, ".config", "frostfs-cli"))
|
2021-06-24 12:31:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|