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
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
accountingCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/acl"
|
2024-05-13 16:07:44 +00:00
|
|
|
apemanager "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/ape_manager"
|
2023-03-07 13:38:26 +00:00
|
|
|
bearerCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/bearer"
|
|
|
|
containerCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/container"
|
|
|
|
controlCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/control"
|
|
|
|
netmapCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/netmap"
|
|
|
|
objectCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/object"
|
|
|
|
sessionCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/tree"
|
|
|
|
utilCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/util"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/misc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/config"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/gendoc"
|
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 (
|
2023-02-03 14:59:15 +00:00
|
|
|
envPrefix = "FROSTFS_CLI"
|
2020-10-12 15:14:17 +00:00
|
|
|
)
|
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
|
2023-02-06 14:54:20 +00:00
|
|
|
cfgDir 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",
|
2023-01-27 10:44:39 +00:00
|
|
|
Short: "Command Line Tool to work with FrostFS",
|
|
|
|
Long: `FrostFS CLI provides all basic interactions with FrostFS and it's services.
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2023-01-27 10:44:39 +00:00
|
|
|
It contains commands for interaction with FrostFS 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,
|
2023-11-15 11:31:37 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
common.StartClientCommandSpan(cmd)
|
|
|
|
},
|
|
|
|
PersistentPostRun: common.StopClientCommandSpan,
|
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()
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(rootCmd, "", err)
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
2023-11-15 11:31:37 +00:00
|
|
|
cobra.EnableTraverseRunHooks = true
|
2020-08-04 14:46:12 +00:00
|
|
|
|
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)")
|
2023-02-06 14:54:20 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgDir, "config-dir", "", "Config directory")
|
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.
|
2023-01-27 10:44:39 +00:00
|
|
|
rootCmd.Flags().Bool("version", false, "Application version and FrostFS API compatibility")
|
2022-01-20 15:34:53 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(acl.Cmd)
|
2024-05-13 16:07:44 +00:00
|
|
|
rootCmd.AddCommand(apemanager.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:24:31 +00:00
|
|
|
rootCmd.AddCommand(containerCli.Cmd)
|
2022-10-10 15:35:40 +00:00
|
|
|
rootCmd.AddCommand(tree.Cmd)
|
2023-08-08 15:08:00 +00:00
|
|
|
rootCmd.AddCommand(gendoc.Command(rootCmd, gendoc.Options{}))
|
2021-06-20 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func entryPoint(cmd *cobra.Command, _ []string) {
|
|
|
|
printVersion, _ := cmd.Flags().GetBool("version")
|
|
|
|
if printVersion {
|
2023-01-27 10:41:03 +00:00
|
|
|
cmd.Print(misc.BuildInfo("FrostFS 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 {
|
2024-11-05 13:15:59 +00:00
|
|
|
// Find config directory.
|
|
|
|
configDir, err := os.UserConfigDir()
|
2024-10-29 13:14:40 +00:00
|
|
|
if err != nil {
|
2024-11-05 13:15:59 +00:00
|
|
|
common.PrintVerbose(rootCmd, "Get config dir: %s", err)
|
2024-10-29 13:14:40 +00:00
|
|
|
} else {
|
2024-11-05 13:15:59 +00:00
|
|
|
// Search config in `$XDG_CONFIG_HOME/frostfs-cli/` with name "config.yaml"
|
|
|
|
viper.AddConfigPath(filepath.Join(configDir, "frostfs-cli"))
|
2024-10-29 13:14:40 +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-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(rootCmd, "Using config file: %s", viper.ConfigFileUsed())
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2023-02-06 14:54:20 +00:00
|
|
|
|
|
|
|
if cfgDir != "" {
|
|
|
|
if err := config.ReadConfigDir(viper.GetViper(), cfgDir); err != nil {
|
|
|
|
commonCmd.ExitOnErr(rootCmd, "failed to read config dir: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|