46 lines
1017 B
Go
46 lines
1017 B
Go
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)
|
||
}
|