This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.go
64 lines (57 loc) · 1.44 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/ahelmy/xdev/internal"
)
// jsonCmd represents the json command
var jsonCmd = &cobra.Command{
Use: "json",
Short: "JSON indentation and minification",
Long: `JSON indentation and minification. For example:
json '{"name":"ahmed","age":30}'
Output: {
"name": "ahmed",
"age": 30
}
json -m {
"name": "ahmed",
"age": 30
}
Output: {"name":"ahmed","age":30}`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a JSON string.")
return
}
if cmd.Flag("minify").Value.String() == "true" {
minify, err := internal.MinifyJSON(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Println(minify)
} else {
indent, err := internal.IndentJSON(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Println(indent)
}
},
}
func init() {
rootCmd.AddCommand(jsonCmd)
jsonCmd.Flags().BoolP("minify", "m", false, "Minify the JSON string")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// jsonCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// jsonCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}