package postbank import ( "context" "fmt" "github.com/spf13/cobra" ) // Cmd go run main.go postbank var Cmd = &cobra.Command{ Use: "postbank", Short: "邮储国密SM辅助工具", } // merchantId, privateKey, sopPublicKey, inputJson, true const ( merchantIdKey = "merchantId" privateKeyKey = "priKey" sopPublicKeyKey = "sopPublicKey" inputKey = "input" isRequestKey = "isRequest" accessTokenKey = "accessToken" shortMerchantIdKey = "m" shortPrivateKeyKey = "p" shortSopPublicKeyKey = "s" shortInputKey = "i" shortIsRequestKey = "r" shortAccessTokenKey = "a" merchantIdContextKey = "merchantId" privateKeyContextKey = "priKey" sopPublicKeyContextKey = "sopPublicKey" inputContextKey = "input" isRequestContextKey = "isRequest" accessTokenContextKey = "accessToken" ) func init() { encryptCmd.PersistentFlags().StringP(merchantIdKey, shortMerchantIdKey, "", "商户号") encryptCmd.PersistentFlags().StringP(privateKeyKey, shortPrivateKeyKey, "", "私钥") encryptCmd.PersistentFlags().StringP(sopPublicKeyKey, shortSopPublicKeyKey, "", "对方公钥") encryptCmd.PersistentFlags().StringP(inputKey, shortInputKey, "", "待加密的JSON字符串") encryptCmd.PersistentFlags().BoolP(isRequestKey, shortIsRequestKey, false, "请求加密") encryptCmd.PersistentFlags().StringP(accessTokenKey, shortAccessTokenKey, "", "accessToken") decryptCmd.PersistentFlags().StringP(merchantIdKey, shortMerchantIdKey, "", "商户号") decryptCmd.PersistentFlags().StringP(privateKeyKey, shortPrivateKeyKey, "", "私钥") decryptCmd.PersistentFlags().StringP(sopPublicKeyKey, shortSopPublicKeyKey, "", "对方公钥") decryptCmd.PersistentFlags().StringP(inputKey, shortInputKey, "", "待解密的JSON字符串") decryptCmd.PersistentFlags().BoolP(isRequestKey, shortIsRequestKey, false, "请求加密") Cmd.AddCommand(encryptCmd, decryptCmd, generateCmd) } func forceArgsToStringContext(cmd *cobra.Command, key, ctxKey string) error { data, err := cmd.Flags().GetString(key) if err != nil { return fmt.Errorf("缺少参数:<%s>%w", key, err) } cmd.SetContext(context.WithValue(cmd.Context(), ctxKey, data)) return nil } func argsToStringContext(cmd *cobra.Command, key, ctxKey string) error { data, err := cmd.Flags().GetString(key) if err != nil { data = "" } cmd.SetContext(context.WithValue(cmd.Context(), ctxKey, data)) return nil } func forceArgsToBoolContext(cmd *cobra.Command, key, ctxKey string) error { data, err := cmd.Flags().GetBool(key) if err != nil { return fmt.Errorf("缺少参数:<%s>%w", key, err) } cmd.SetContext(context.WithValue(cmd.Context(), ctxKey, data)) return nil }