Skip to content

Commit

Permalink
feat: Add support for cmd to convert key-value to JSON (#6)
Browse files Browse the repository at this point in the history
* feat: Added text to Json

* Optimize the error handling

* fix the README and file name
  • Loading branch information
Pradumnasaraf authored Apr 2, 2023
1 parent aa2c13c commit b4760fc
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 25 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ bin

.idea/
.vscode/
*.iml
*.iml

# Build files

candy

# Env files

.env
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Usage:
Available Commands:
JTY Converts a JSON into YAML.
YTJ Converts a YAML into JSON.
KVTJ Convert Key-Value (text) to JSON.
docker Docker related commands. Like generating a Dockerfile for a language.
k8s Kubernetes related commands. Like generating manifest files for kubernetes objects.
```
Expand Down
8 changes: 2 additions & 6 deletions cmd/JsonToYaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ var jsonToYaml = &cobra.Command{
vp := viper.New()
vp.SetConfigFile(inputJsonFile)
err := vp.ReadInConfig()
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

// Write the YAML file
if outputYamlFile == "" {
Expand All @@ -33,9 +31,7 @@ var jsonToYaml = &cobra.Command{
vp.SetConfigFile(outputYamlFile)

err = vp.WriteConfig()
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

if outputYamlFile == "" {
log.Print("Operation completed successfully. Check the output.yaml file.")
Expand Down
14 changes: 8 additions & 6 deletions cmd/docker/docker-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ var dockerfileCmd = &cobra.Command{

func createDockerfile(lang string) {
file, err := os.Create("Dockerfile")
if err != nil {
log.Fatal(err)
}
checkNilErr(err)
defer file.Close()

_, err = file.WriteString(lang)
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

log.Print("Dockerfile created successfully.")
}
Expand All @@ -57,3 +53,9 @@ func init() {
dockerfileCmd.Flags().StringVarP(&language, "lang", "l", "", "Programming language to generate Dockerfile for.")
dockerfileCmd.MarkFlagRequired("lang")
}

func checkNilErr(err error) {
if err != nil {
log.Fatal(err)
}
}
70 changes: 70 additions & 0 deletions cmd/keyValueToJson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"encoding/json"
"log"
"os"
"strings"

"github.com/spf13/cobra"
)

var (
inputTextFile string
outputJsonFile1 string
)

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

// Read the input file
content, err := os.ReadFile(inputTextFile)
checkNilErr(err)

// Check if the input file is empty
if len(content) == 0 {
checkNilErr(err)
}

// Convert the input file to JSON
entries := strings.Split(string(content), "\n")
m := make(map[string]string)
for _, e := range entries {
parts := strings.Split(e, "=")
m[parts[0]] = parts[1]
}
jsonString, _ := json.MarshalIndent(m, "", " ")

// Print the output to the console
if printOutput, _ := cmd.Flags().GetBool("print"); printOutput {
log.Println(string(jsonString))
return
}

if outputJsonFile1 == "" {
outputJsonFile1 = "output.json"
}

// Write the output file
file, err := os.Create(outputJsonFile1)
checkNilErr(err)

defer file.Close()

_, err = file.WriteString(string(jsonString))
checkNilErr(err)
},
}

func init() {

// Flags for the TTJ command
keyValueToJson.Flags().StringVarP(&inputTextFile, "file", "f", "", "Input the text file name. Eg: keys.txt or .env")
keyValueToJson.MarkFlagRequired("file")

keyValueToJson.Flags().StringVarP(&outputJsonFile1, "output", "o", "", "Output JSON file name (default is output.json)")
keyValueToJson.Flags().BoolP("print", "p", false, "Print the output to the console")
}
15 changes: 9 additions & 6 deletions cmd/kubernetes/kubernetes-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ var kubernetesManifestCmd = &cobra.Command{

func createManifestFile(filename string, obj string) {
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

defer file.Close()

_, err = file.WriteString(obj)
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

log.Print(filename + " created successfully.")
}
Expand All @@ -62,3 +59,9 @@ func init() {
kubernetesManifestCmd.Flags().StringVarP(&k8Obj, "obj", "o", "", "Kubernetes object to generate manifest for.")
kubernetesManifestCmd.MarkFlagRequired("obj")
}

func checkNilErr(err error) {
if err != nil {
log.Fatal(err)
}
}
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"log"
"os"

"github.com/Pradumnasaraf/candy/cmd/docker"
Expand Down Expand Up @@ -30,6 +31,13 @@ func init() {
// Subcommands for the root command
rootCmd.AddCommand(jsonToYaml)
rootCmd.AddCommand(yamlToJsonCmd)
rootCmd.AddCommand(keyValueToJson)
rootCmd.AddCommand(docker.DockerCmd)
rootCmd.AddCommand(kubernetes.KubernetesCmd)
}

func checkNilErr(err error) {
if err != nil {
log.Fatal(err)
}
}
8 changes: 2 additions & 6 deletions cmd/yamlToJson.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ var yamlToJsonCmd = &cobra.Command{
vp := viper.New()
vp.SetConfigFile(inputYamlFile)
err := vp.ReadInConfig()
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

// Write the JSON file
if outputJsonFile == "" {
outputJsonFile = "output.json"
}
vp.SetConfigFile(outputJsonFile)
err = vp.WriteConfig()
if err != nil {
log.Fatal(err)
}
checkNilErr(err)

if outputJsonFile == "" {
log.Print("Operation completed successfully. Check the output.json file.")
Expand Down

0 comments on commit b4760fc

Please sign in to comment.