generated from TrueCloudLab/basic
83 lines
2 KiB
Go
83 lines
2 KiB
Go
package modules
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"git.frostfs.info/dkirillov/policy-reader/internal/version"
|
|
"git.frostfs.info/dkirillov/policy-reader/modules/chains"
|
|
"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()
|
|
viper.SetEnvPrefix("POLICY_READER")
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
viper.AllowEmptyEnv(true)
|
|
viper.SetConfigType("yaml")
|
|
|
|
if err := viper.BindPFlags(cmd.Flags()); err != nil {
|
|
return err
|
|
}
|
|
|
|
if viper.IsSet(configFlag) {
|
|
return readConfig(viper.GetViper(), viper.GetString(configFlag))
|
|
}
|
|
|
|
return nil
|
|
},
|
|
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() {
|
|
cobra.EnableTraverseRunHooks = true
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
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)
|
|
rootCmd.AddCommand(chains.Cmd)
|
|
}
|
|
|
|
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)
|
|
}
|