From 0e9d9ef213900976bccafacd3f5598a43624eccd Mon Sep 17 00:00:00 2001 From: clouding Date: Sun, 13 Sep 2020 21:42:18 +0800 Subject: [PATCH] feat: add base64 scripts --- README.md | 2 ++ cmd/base64.go | 34 ++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ 3 files changed, 38 insertions(+) create mode 100644 cmd/base64.go diff --git a/README.md b/README.md index 44fbb11..816993f 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ jwt-decode=# | md5 | Computes the checksum | | url-encode | Encode url | | url-decode | Decode url | +| base64-encode | Base64 encode | +| base64-decode | Base64 decode | | rand-string | Generate random string of given length (characters: a-z, A-Z, 0-9) | | count | Get the characters length | | camel-case | Coverts string to [camel case](https://en.wikipedia.org/wiki/Camel_case) | diff --git a/cmd/base64.go b/cmd/base64.go new file mode 100644 index 0000000..bfa5561 --- /dev/null +++ b/cmd/base64.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "encoding/base64" + "fmt" + + "github.com/spf13/cobra" +) + +var base64EncodeCmd = &cobra.Command{ + Use: "base64-encode [text]", + Short: "Base64 encode", + Args: cobra.ExactArgs(1), + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + s := base64.StdEncoding.EncodeToString([]byte(args[0])) + fmt.Println(s) + }, +} + +var base64DecodeCmd = &cobra.Command{ + Use: "base64-decode [text]", + Short: "Base64 decode", + Args: cobra.ExactArgs(1), + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + b, err := base64.StdEncoding.DecodeString(args[0]) + if err != nil { + fmt.Println("Invalid text!") + return + } + fmt.Println(string(b)) + }, +} diff --git a/cmd/root.go b/cmd/root.go index 4238af2..28fa074 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,8 @@ func commands() []*cobra.Command { md5Cmd, urlEncodeCmd, urlDecodeCmd, + base64EncodeCmd, + base64DecodeCmd, randStringCmd, countCmd, CamelCaseCmd,