|
|
package main
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
|
|
|
"b612.me/starcrypto"
|
|
|
"b612.me/starlog"
|
|
|
"github.com/spf13/cobra"
|
|
|
)
|
|
|
|
|
|
var b64cmd = &cobra.Command{
|
|
|
Use: "base64",
|
|
|
Short: "使用base64处理文件或字符串",
|
|
|
Long: "使用base64处理文件或字符串",
|
|
|
Run: func(this *cobra.Command, args []string) {
|
|
|
var err error
|
|
|
ok, _ := this.Flags().GetBool("file")
|
|
|
de, _ := this.Flags().GetBool("decode")
|
|
|
if len(args) != 1 {
|
|
|
starlog.Criticalln("参数不足,请输入文件地址或字符串")
|
|
|
this.Help()
|
|
|
return
|
|
|
}
|
|
|
shell := func(pect float64) {
|
|
|
if pect == 100 {
|
|
|
fmt.Println("已处理:100.000000%")
|
|
|
} else {
|
|
|
fmt.Printf("已处理:%f%%\r", pect)
|
|
|
}
|
|
|
}
|
|
|
if ok {
|
|
|
path, _ := this.Flags().GetString("path")
|
|
|
if !de {
|
|
|
err = starcrypto.Base64EncodeFile(args[0], path, shell)
|
|
|
} else {
|
|
|
err = starcrypto.Base64DecodeFile(args[0], path, shell)
|
|
|
}
|
|
|
} else {
|
|
|
if !de {
|
|
|
data := starcrypto.Base64Encode([]byte(args[0]))
|
|
|
fmt.Println(data)
|
|
|
} else {
|
|
|
var data []byte
|
|
|
data, err = starcrypto.Base64Decode(args[0])
|
|
|
fmt.Println(string(data))
|
|
|
}
|
|
|
}
|
|
|
if err != nil {
|
|
|
starlog.Criticalln(err)
|
|
|
return
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
b64cmd.Flags().BoolP("file", "f", false, "base64处理文件")
|
|
|
b64cmd.Flags().StringP("path", "p", "./b64.encode", "指定处理地址,默认为./b64.encode")
|
|
|
b64cmd.Flags().BoolP("decode", "d", false, "base64解码")
|
|
|
}
|