-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmlfmt_main.go
91 lines (72 loc) · 2.11 KB
/
xmlfmt_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
90
91
// xmlfmt - XML Formatter
// The xmlfmt will format the XML string without rewriting the document
package main
////////////////////////////////////////////////////////////////////////////
// Program: xmlfmt
// Purpose: XML Formatter
// Authors: Antonio Sun (c) 2016-2022, All rights reserved
////////////////////////////////////////////////////////////////////////////
//go:generate sh xmlfmt_cliGen.sh
import (
"fmt"
"io/ioutil"
"os"
"github.com/go-easygen/go-flags"
"github.com/go-xmlfmt/xmlfmt"
)
//////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
////////////////////////////////////////////////////////////////////////////
// Global variables definitions
var (
progname = "xmlfmt"
version = "1.1.2"
date = "2022-08-12"
// opts store all the configurable options
opts optsT
)
var parser = flags.NewParser(&opts, flags.Default)
////////////////////////////////////////////////////////////////////////////
// Function definitions
// Function main
func main() {
opts.Version = showVersion
opts.Verbflg = func() {
opts.Verbose++
}
if _, err := parser.Parse(); err != nil {
fmt.Println()
parser.WriteHelp(os.Stdout)
os.Exit(1)
}
fmt.Println()
DoXmlfmt()
}
func showVersion() {
fmt.Fprintf(os.Stderr, "xmlfmt - XML Formatter, version %s\n", version)
fmt.Fprintf(os.Stderr, "Built on %s\n", date)
fmt.Fprintf(os.Stderr, "Copyright (C) 2016-2022, Antonio Sun\n\n")
fmt.Fprintf(os.Stderr, "The xmlfmt will format the XML string without rewriting the document\n")
os.Exit(0)
}
// DoXmlfmt implements the business logic of command `xmlfmt`
func DoXmlfmt() error {
var data []byte
var err error
if opts.Filei == "-" {
data, err = ioutil.ReadAll(os.Stdin)
} else {
data, err = ioutil.ReadFile(opts.Filei)
}
abortOn("Input", err)
fmt.Println(xmlfmt.FormatXML(string(data),
opts.Prefix, opts.Indent, opts.Nested))
return nil
}
// abortOn will quit on anticipated errors gracefully without stack trace
func abortOn(errCase string, e error) {
if e != nil {
fmt.Printf("[%s] %s error: %v\n", progname, errCase, e)
os.Exit(1)
}
}