Skip to content

Commit

Permalink
Merge pull request #251 from joaoqalves/add-export-output-to-file
Browse files Browse the repository at this point in the history
Add export output to file
  • Loading branch information
barnybug authored Feb 16, 2018
2 parents c3615c8 + d2d8da2 commit c27d487
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ coverage.txt
coverage/

/dist/

# GoLand
.idea
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ func UnexpandSelfAliases(records []dns.RR, zone *route53.HostedZone, full bool)
}
}

func exportBind(name string, full bool) {
func exportBind(name string, full bool, writer io.Writer) {
zone := lookupZone(name)
ExportBindToWriter(r53, zone, full, os.Stdout)
ExportBindToWriter(r53, zone, full, writer)
}

type exportSorter struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ Feature: export
When I run "cli53 rrcreate $domain 'alias 86400 AWS ALIAS A www $self false'"
And I run "cli53 export --full $domain"
Then the output contains "alias.$domain. 86400 AWS ALIAS A www.$domain. $self false"

Scenario: I can export a domain to a file
Given I have a domain "$domain"
When I run "cli53 rrcreate $domain 'www A 127.0.0.1'"
And I run "cli53 export --output /tmp/testcli53 $domain"
Then the output file "/tmp/testcli53" contains "$domain"
14 changes: 14 additions & 0 deletions internal/features/step_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ func init() {
}
})

Then(`^the output file "(.+?)" contains "(.+?)"$`, func(outputFile string, s string) {
outputFile = unquote(outputFile)
s = unquote(domain(s))
output, err := ioutil.ReadFile(outputFile)

if err != nil {
T.Errorf("Could not read %s", outputFile)
}

if !strings.Contains(string(output), s) {
T.Errorf("Output did not contain \"%s\"\nactual: %s", s, runOutput)
}
})

Then(`^the output matches "(.+?)"$`, func(s string) {
re, err := regexp.Compile(s)
fatalIfErr(err)
Expand Down
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/urfave/cli"
"os"
)

var r53 *route53.Route53
Expand Down Expand Up @@ -256,6 +257,10 @@ func Main(args []string) int {
Name: "full, f",
Usage: "export prefixes as full names",
},
cli.StringFlag{
Name: "output",
Usage: "Write to an output file instead of STDOUT",
},
),
Action: func(c *cli.Context) (err error) {
r53, err = getService(c)
Expand All @@ -266,7 +271,17 @@ func Main(args []string) int {
cli.ShowCommandHelp(c, "export")
return cli.NewExitError("Expected exactly 1 parameter", 1)
}
exportBind(c.Args().First(), c.Bool("full"))

outputFileName := c.String("output")
writer := os.Stdout
if len(outputFileName) > 0 {
writer, err = os.Create(outputFileName)
defer writer.Close()
if err != nil {
return err
}
}
exportBind(c.Args().First(), c.Bool("full"), writer)
return nil
},
},
Expand Down

0 comments on commit c27d487

Please sign in to comment.