You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/base64/base64.go

69 lines
1.5 KiB
Go

2 years ago
package base64
import (
"fmt"
"path/filepath"
"b612.me/starcrypto"
"b612.me/starlog"
"github.com/spf13/cobra"
)
var Cmd = &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("out")
if path == "" {
ext := filepath.Ext(args[0])
if ext != "" {
path = args[0][:len(args[0])-len(ext)] + ".b64" + ext
} else {
path = args[0] + ".b64"
}
}
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() {
Cmd.Flags().BoolP("file", "f", false, "base64处理文件")
Cmd.Flags().StringP("out", "o", "", "指定加解码输出地址")
Cmd.Flags().BoolP("decode", "d", false, "base64解码")
}