-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for cmd to convert key-value to JSON (#6)
* feat: Added text to Json * Optimize the error handling * fix the README and file name
- Loading branch information
1 parent
aa2c13c
commit b4760fc
Showing
8 changed files
with
109 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,12 @@ bin | |
|
||
.idea/ | ||
.vscode/ | ||
*.iml | ||
*.iml | ||
|
||
# Build files | ||
|
||
candy | ||
|
||
# Env files | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters