@@ -7,64 +7,73 @@ import (
7
7
8
8
"github.com/awslabs/aws-cloudformation-template-formatter/format"
9
9
"github.com/awslabs/aws-cloudformation-template-formatter/parse"
10
+
11
+ "github.com/andrew-d/go-termutil"
12
+ "github.com/spf13/pflag"
10
13
)
11
14
12
- var usage = `Usage: cfn-format [-j] <filename>
15
+ var usage = `Usage: cfn-format [OPTION...] [FILENAME]
13
16
14
17
AWS CloudFormation Format is a tool that reads a CloudFormation template
15
18
and outputs the same template, formatted according to the same standards
16
19
used in AWS documentation.
17
20
21
+ If FILENAME is not supplied, cfn-format will read from STDIN.
22
+
18
23
Options:
19
- -j Output the template as JSON (default format: YAML).
20
- -w Write the output back to the file rather than to stdout.
21
- --help Show this message and exit.
22
- `
24
+ --help Show this message and exit.`
25
+
26
+ var jsonFlag bool
27
+ var writeFlag bool
28
+
29
+ func init () {
30
+ pflag .BoolVarP (& jsonFlag , "json" , "j" , false , "Output the template as JSON (default format: YAML)." )
31
+ pflag .BoolVarP (& writeFlag , "write" , "w" , false , "Write the output back to the file rather than to stdout." )
32
+
33
+ pflag .Usage = func () {
34
+ fmt .Fprintln (os .Stderr , usage )
35
+ pflag .PrintDefaults ()
36
+ os .Exit (1 )
37
+ }
38
+ }
23
39
24
40
func die (message string ) {
25
41
fmt .Fprintln (os .Stderr , message )
26
42
os .Exit (1 )
27
43
}
28
44
29
- func help () {
30
- die (usage )
31
- }
32
-
33
45
func main () {
34
- style := "yaml"
35
- write := false
46
+ var fileName string
47
+ var source map [string ]interface {}
48
+ var err error
36
49
37
- // Parse options
38
- if len (os .Args ) < 2 {
39
- help ()
40
- }
50
+ pflag .Parse ()
51
+ args := pflag .Args ()
41
52
42
- for _ , arg := range os .Args [1 : len (os .Args )- 1 ] {
43
- switch arg {
44
- case "-j" :
45
- style = "json"
46
- case "-w" :
47
- write = true
48
- case "-h" , "--help" :
49
- help ()
53
+ if len (args ) == 1 {
54
+ // Reading from a file
55
+ fileName = args [0 ]
56
+ source , err = parse .ReadFile (fileName )
57
+ if err != nil {
58
+ die (err .Error ())
59
+ }
60
+ } else if ! termutil .Isatty (os .Stdin .Fd ()) {
61
+ if writeFlag {
62
+ // Can't use write without a filename!
63
+ die ("Can't write back to a file when reading from stdin" )
50
64
}
51
- }
52
-
53
- // Get the filename
54
- fileName := os .Args [len (os .Args )- 1 ]
55
- if fileName == "--help" {
56
- help ()
57
- }
58
65
59
- // Read the source template
60
- source , err := parse .ReadFile (fileName )
61
- if err != nil {
62
- die (err .Error ())
66
+ source , err = parse .Read (os .Stdin )
67
+ if err != nil {
68
+ die (err .Error ())
69
+ }
70
+ } else {
71
+ pflag .Usage ()
63
72
}
64
73
65
74
// Format the output
66
75
var output string
67
- if style == "json" {
76
+ if jsonFlag {
68
77
output = format .Json (source )
69
78
} else {
70
79
output = format .Yaml (source )
@@ -76,7 +85,7 @@ func main() {
76
85
die (err .Error ())
77
86
}
78
87
79
- if write {
88
+ if writeFlag {
80
89
ioutil .WriteFile (fileName , []byte (output ), 0644 )
81
90
} else {
82
91
fmt .Println (output )
0 commit comments