-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonfiddle_main.go
89 lines (71 loc) · 2.21 KB
/
jsonfiddle_main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// jsonfiddle - JSON Fiddling
// Tool to fiddle with json strings
package main
////////////////////////////////////////////////////////////////////////////
// Program: jsonfiddle
// Purpose: JSON Fiddling
// Authors: Tong Sun (c) 2017-2023, All rights reserved
////////////////////////////////////////////////////////////////////////////
//go:generate sh jsonfiddle_cliGen.sh
//go:generate emd gen -in README.e.md -out README.md
import (
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"github.com/go-easygen/go-flags"
"github.com/go-easygen/go-flags/clis"
)
//////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
////////////////////////////////////////////////////////////////////////////
// Global variables definitions
var (
progname = "jsonfiddle"
version = "0.5.0"
date = "2023-01-22"
// opts store all the configurable options
opts optsT
)
var gfParser = flags.NewParser(&opts, flags.Default)
////////////////////////////////////////////////////////////////////////////
// Function definitions
//==========================================================================
// Function main
func main() {
opts.Version = showVersion
opts.Verbflg = func() {
opts.Verbose++
}
if _, err := gfParser.Parse(); err != nil {
fmt.Println()
gfParser.WriteHelp(os.Stdout)
os.Exit(1)
}
fmt.Println()
//DoJsonfiddle()
}
//==========================================================================
// support functions
func showVersion() {
fmt.Fprintf(os.Stderr, "jsonfiddle - JSON Fiddling, version %s\n", version)
fmt.Fprintf(os.Stderr, "Built on %s\n", date)
fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
fmt.Fprintf(os.Stderr, "Tool to fiddle with json strings\n")
os.Exit(0)
}
// readJson reads the given json file as []byte.
func readJson(r io.Reader) []byte {
data, err := ioutil.ReadAll(r)
clis.AbortOn("Reading json input", err)
if opts.Protect {
data = regexp.MustCompile(`({{)([^ }]+)(}})`).
ReplaceAll(data, []byte(`<<${2}>>`))
// "age":<<C_age>> => "age":"<<C_age>>"
data = regexp.MustCompile(`(:)(<<[^>]+>>)([]},])`).
ReplaceAll(data, []byte(`${1}"${2}"${3}`))
}
clis.Verbose(2, "%s", string(data))
return data
}