ag_tools/cmd/base64.go

46 lines
1017 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"archgrid.xyz/ag/toolsbox/serialize/base64"
"github.com/spf13/cobra"
)
var base64Cmd = &cobra.Command{
Use: "base64",
Short: "对给定内容进行base64编码或解码",
}
var base64EncodeCmd = &cobra.Command{
Use: "encode",
Short: "对给定内容进行base64编码",
Args: cobra.MinimumNArgs(1),
Run: base64EncodeExecute,
}
func base64EncodeExecute(cmd *cobra.Command, args []string) {
fmt.Printf("Base64编码结果%s\n", base64.ToBase64([]byte(args[0])))
}
var base64DecodeCmd = &cobra.Command{
Use: "decode",
Short: "对给定内容进行base64解码",
Args: cobra.MinimumNArgs(1),
Run: base64DecodeExecute,
}
func base64DecodeExecute(cmd *cobra.Command, args []string) {
decoded, err := base64.FromBase64(args[0])
if err != nil {
fmt.Printf("Base64解码失败%s\n", err)
return
}
fmt.Printf("Base64解码结果%s\n", decoded)
}
func init() {
base64Cmd.AddCommand(base64EncodeCmd, base64DecodeCmd)
rootCmd.AddCommand(base64Cmd)
}