Skip to content

Commit 9bb0d66

Browse files
author
Chris Cooper
committed
adds support for taking a file from stdin
1 parent 1858fb8 commit 9bb0d66

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ Printing output to command line:
6363
"ParameterValue": "Type: String"
6464
}
6565
]
66+
67+
Accept input over stdin:
68+
69+
$ cat test.json | cf_parameter_generator
70+
[
71+
{
72+
"ParameterKey": "AccessControl",
73+
"ParameterValue": "Type: String"
74+
},
75+
{
76+
"ParameterKey": "ApplicationName",
77+
"ParameterValue": "Type: String"
78+
},
79+
{
80+
"ParameterKey": "AssetID",
81+
"ParameterValue": "Type: String"
82+
},
83+
{
84+
"ParameterKey": "Environment",
85+
"ParameterValue": "Type: String"
86+
},
87+
{
88+
"ParameterKey": "LifecycleConfigurationStatus",
89+
"ParameterValue": "Type: String"
90+
},
91+
{
92+
"ParameterKey": "NoncurrentVersionExpirationInDays",
93+
"ParameterValue": "Type: Number"
94+
},
95+
{
96+
"ParameterKey": "SubnetIDs",
97+
"ParameterValue": "Type: List<AWS::EC2::Subnet::Id>"
98+
},
99+
{
100+
"ParameterKey": "VersioningConfigurationStatus",
101+
"ParameterValue": "Type: String"
102+
}
103+
]
66104

67105
Saving output to a new file (will update an existing file or overwrite it if it is blank (0 bytes))
68106

lib/cfpgen.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"bytes"
1616

17+
"io"
18+
1719
"gopkg.in/yaml.v2"
1820
)
1921

@@ -36,17 +38,26 @@ type Config struct {
3638
}
3739

3840
// Generate generates a cloud formation parameters file template and writes either to a file or stdout
39-
func Generate(c *Config) error {
41+
func Generate(c *Config, reader io.Reader) error {
4042
flag.Parse()
41-
42-
if c.InFile == "" {
43+
var data []byte
44+
var err error
45+
if reader != nil {
46+
data, err = ioutil.ReadAll(reader)
47+
if err != nil {
48+
return err
49+
}
50+
}
51+
if c.InFile == "" && reader == nil {
4352
return ErrMissingInFile
4453
}
4554

4655
m := make(map[string]interface{})
47-
data, err := ioutil.ReadFile(c.InFile)
48-
if err != nil {
49-
return err
56+
if data == nil {
57+
data, err = ioutil.ReadFile(c.InFile)
58+
if err != nil {
59+
return err
60+
}
5061
}
5162
err = c.unmarshal(data, &m)
5263
if err != nil {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func config() *cfpgen.Config {
3939
}
4040

4141
func main() {
42-
err := cfpgen.Generate(config())
42+
err := cfpgen.Generate(config(), os.Stdin)
4343
if err != nil {
4444
fmt.Fprintln(os.Stderr, err)
4545
flag.Usage()

0 commit comments

Comments
 (0)