2024-08-28 11:56:55 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-28 13:19:23 +00:00
|
|
|
"os"
|
2024-08-28 11:56:55 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/dkirillov/policy-reader/internal/version"
|
|
|
|
"git.frostfs.info/dkirillov/policy-reader/modules/contract"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands.
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "policy-reader",
|
|
|
|
Version: version.Version,
|
|
|
|
Short: "FrostFS policy contract reader",
|
|
|
|
Long: "Helps reading policy information from contact in FrostFS network",
|
|
|
|
Example: "policy-reader --version",
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
viper.AutomaticEnv()
|
2024-08-28 13:19:23 +00:00
|
|
|
viper.SetEnvPrefix("POLICY_READER")
|
2024-08-28 11:56:55 +00:00
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
viper.AllowEmptyEnv(true)
|
2024-08-28 13:19:23 +00:00
|
|
|
viper.SetConfigType("yaml")
|
2024-08-28 11:56:55 +00:00
|
|
|
|
2024-08-28 13:19:23 +00:00
|
|
|
if err := viper.BindPFlags(cmd.Flags()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if viper.IsSet(configFlag) {
|
|
|
|
return readConfig(viper.GetViper(), viper.GetString(configFlag))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-08-28 11:56:55 +00:00
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return cmd.Help()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute(ctx context.Context) (*cobra.Command, error) {
|
|
|
|
return rootCmd.ExecuteContextC(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
const configFlag = "config"
|
|
|
|
|
|
|
|
func init() {
|
2024-08-28 13:19:23 +00:00
|
|
|
cobra.EnableTraverseRunHooks = true
|
2024-08-28 11:56:55 +00:00
|
|
|
rootCmd.PersistentFlags().StringP(configFlag, "c", "", "Path to config file")
|
|
|
|
|
|
|
|
cobra.AddTemplateFunc("runtimeVersion", runtime.Version)
|
|
|
|
rootCmd.SetVersionTemplate(`Frostfs policy reader
|
|
|
|
{{printf "Version: %s" .Version }}
|
|
|
|
GoVersion: {{ runtimeVersion }}
|
|
|
|
`)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(contract.Cmd)
|
|
|
|
}
|
2024-08-28 13:19:23 +00:00
|
|
|
|
|
|
|
func readConfig(v *viper.Viper, fileName string) error {
|
|
|
|
cfgFile, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if errClose := cfgFile.Close(); errClose != nil {
|
|
|
|
panic(errClose)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return v.ReadConfig(cfgFile)
|
|
|
|
}
|