Skip to content

Commit e42c380

Browse files
feat: Add encode-decode string to base64 support (#12)
1 parent cf14710 commit e42c380

7 files changed

+47
-9
lines changed

cmd/encode.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
isDecode bool
12+
)
13+
14+
var encodeCmd = &cobra.Command{
15+
Use: "encode [string to encode]",
16+
Short: "It encodes and decodes a string to base64 and vice versa.",
17+
Args: cobra.ExactArgs(1),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
20+
stringToEncode := args[0]
21+
22+
if isDecode {
23+
decoded, err := base64.StdEncoding.DecodeString(stringToEncode)
24+
checkNilErr(err)
25+
fmt.Println(string(decoded))
26+
return
27+
}
28+
29+
encoded := base64.StdEncoding.EncodeToString([]byte(stringToEncode))
30+
fmt.Println(encoded)
31+
},
32+
}
33+
34+
func init() {
35+
encodeCmd.Flags().BoolVarP(&isDecode, "decode", "d", false, "Decode the string")
36+
}

cmd/jsonToYaml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414

1515
// jsonToYaml is the command for converting JSON to YAML
1616
var jsonToYaml = &cobra.Command{
17-
Use: "JTY",
17+
Use: "JTY [flags]",
1818
Short: "Converts a JSON into YAML.",
1919
Run: func(cmd *cobra.Command, args []string) {
2020

cmd/keyValueToJson.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
// textToJsonCmd represents the aa command
1818
var keyValueToJson = &cobra.Command{
19-
Use: "KVTJ",
19+
Use: "KVTJ [flags]",
2020
Short: "Converts Key-Value (text) to JSON.",
2121
Run: func(cmd *cobra.Command, args []string) {
2222

cmd/kubernetes/kubernetes-manifest.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414

1515
// kubernetesManifestCmd is the command for generating a manifest file for a kubernetes object
1616
var kubernetesManifestCmd = &cobra.Command{
17-
Use: "manifest",
17+
Use: "manifest [flags]",
1818
Short: "Generates manifest file for different objects.",
1919
Run: func(cmd *cobra.Command, args []string) {
2020

@@ -44,7 +44,7 @@ var kubernetesManifestCmd = &cobra.Command{
4444
func createManifestFile(filename string, obj string) {
4545
file, err := os.Create(filename)
4646
checkNilErr(err)
47-
47+
4848
defer file.Close()
4949

5050
_, err = file.WriteString(obj)
@@ -64,4 +64,4 @@ func checkNilErr(err error) {
6464
if err != nil {
6565
log.Fatal(err)
6666
}
67-
}
67+
}

cmd/kubernetes/kubernetes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// KubernetesCmd is the command for Kubernetes related commands.
88
var KubernetesCmd = &cobra.Command{
9-
Use: "k8s",
9+
Use: "k8s [command]",
1010
Short: "Kubernetes related commands. Like generating manifest files for kubernetes objects.",
1111
Run: func(cmd *cobra.Command, args []string) {
1212
cmd.Help()

cmd/root.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// rootCmd is the root command for candy
1313
var rootCmd = &cobra.Command{
14-
Use: "candy",
14+
Use: "candy [command]",
1515
Short: "Do all your tedious tasks with a single command",
1616
Run: func(cmd *cobra.Command, args []string) {
1717
cmd.Help()
@@ -29,15 +29,17 @@ func Execute() {
2929
func init() {
3030

3131
// Subcommands for the root command
32+
rootCmd.AddCommand(encodeCmd)
3233
rootCmd.AddCommand(jsonToYaml)
3334
rootCmd.AddCommand(yamlToJsonCmd)
3435
rootCmd.AddCommand(keyValueToJson)
3536
rootCmd.AddCommand(docker.DockerCmd)
3637
rootCmd.AddCommand(kubernetes.KubernetesCmd)
38+
3739
}
3840

3941
func checkNilErr(err error) {
4042
if err != nil {
4143
log.Fatal(err)
4244
}
43-
}
45+
}

cmd/yamlToJson.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414

1515
// yamlToJsonCmd is the command for converting YAML to JSON
1616
var yamlToJsonCmd = &cobra.Command{
17-
Use: "YTJ",
17+
Use: "YTJ [flags]",
1818
Short: "Converts a YAML into JSON.",
1919
Run: func(cmd *cobra.Command, args []string) {
2020

0 commit comments

Comments
 (0)