-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
83 lines (73 loc) · 1.84 KB
/
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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/asty-org/asty/asty"
)
const UsageString = `Usage: asty <command> [flags]
commands:
go2json - convert go source to json
json2go - convert json to go source
help - print this message
flags:
`
func printError(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
args := os.Args
var input, output string
var indent int
var comments, positions, references, imports bool
fs := flag.NewFlagSet("asty", flag.ExitOnError)
fs.StringVar(&input, "input", "", "input file name (default: stdin)")
fs.StringVar(&output, "output", "", "output file name (default: stdout)")
fs.IntVar(&indent, "indent", 0, "indentation level (default: 0)")
fs.BoolVar(&comments, "comments", false, "include comments (default: false)")
fs.BoolVar(&positions, "positions", false, "include positions (default: false)")
fs.BoolVar(&references, "references", false,
"include references to reuse nodes from multiple places (default: false)")
fs.BoolVar(&imports, "imports", false,
"include imports list into output (default: false)")
fs.Usage = func() {
fmt.Fprint(fs.Output(), UsageString)
fs.PrintDefaults()
}
if len(args) < 2 {
fs.Usage()
return
}
err := fs.Parse(args[2:])
if err != nil {
printError(err)
}
options := asty.Options{
WithImports: imports,
WithComments: comments,
WithPositions: positions,
WithReferences: references,
}
switch args[1] {
case "go2json":
indentStr := strings.Repeat(" ", indent)
err := asty.SourceToJSON(input, output, indentStr, options)
if err != nil {
printError(err)
}
case "json2go":
err := asty.JSONToSource(input, output, options)
if err != nil {
printError(err)
}
case "help":
fs.Usage()
return
default:
fmt.Printf("unknown command: %s\n", args[1])
fs.Usage()
os.Exit(1)
}
}