62 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package tools
 | 
						||
 | 
						||
import (
 | 
						||
	"fmt"
 | 
						||
 | 
						||
	"b612.me/starainrt"
 | 
						||
	"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.Println("参数不足,请输入文件地址或字符串", "red", "b")
 | 
						||
			this.Help()
 | 
						||
			return
 | 
						||
		}
 | 
						||
		shell := func(pect float64) {
 | 
						||
			if pect == 100 {
 | 
						||
				fmt.Println("已处理:100.000000%")
 | 
						||
			} else {
 | 
						||
				fmt.Printf("已处理:%f%%\r", pect)
 | 
						||
			}
 | 
						||
		}
 | 
						||
		cry := new(starainrt.StarCrypto)
 | 
						||
		if ok {
 | 
						||
			path, _ := this.Flags().GetString("path")
 | 
						||
			if !de {
 | 
						||
				err = cry.Base64EncodeFile(args[0], path, shell)
 | 
						||
			} else {
 | 
						||
				err = cry.Base64DecodeFile(args[0], path, shell)
 | 
						||
			}
 | 
						||
		} else {
 | 
						||
			if !de {
 | 
						||
				data := cry.Base64Encode([]byte(args[0]))
 | 
						||
				fmt.Println(data)
 | 
						||
			} else {
 | 
						||
				var data []byte
 | 
						||
				data, err = cry.Base64Decode(args[0])
 | 
						||
				fmt.Println(string(data))
 | 
						||
			}
 | 
						||
		}
 | 
						||
		if err != nil {
 | 
						||
			starlog.Println(err, "red", "b")
 | 
						||
			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解码")
 | 
						||
	Maincmd.AddCommand(b64cmd)
 | 
						||
}
 |