Skip to content

Commit

Permalink
feat(file) write state to stdout when output file flag is -
Browse files Browse the repository at this point in the history
Fix #7 
From #10
  • Loading branch information
matthewbednarski authored and Travis Raines committed Apr 21, 2021
1 parent b59c777 commit a4ac4f2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ configure Kong.`,
func init() {
rootCmd.AddCommand(dumpCmd)
dumpCmd.Flags().StringVarP(&dumpCmdKongStateFile, "output-file", "o",
"kong.yaml", "write Kong configuration to FILE")
"kong.yaml", "write Kong configuration to FILE. "+
"Use '-' to write to stdout.")
dumpCmd.Flags().BoolVar(&dumpConfig.SkipConsumers, "skip-consumers",
false, "skip exporting consumers and any plugins associated "+
"with consumers")
Expand Down
7 changes: 6 additions & 1 deletion file/writer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"fmt"
"io/ioutil"
"sort"
"strings"
Expand Down Expand Up @@ -165,7 +166,11 @@ func KongStateToFile(kongState *state.KongState, filename string) error {
})

c, err := yaml.Marshal(file)
err = ioutil.WriteFile(filename, c, 0600)
if filename == "-" {
_, err = fmt.Print(string(c))
} else {
err = ioutil.WriteFile(filename, c, 0600)
}
if err != nil {
return err
}
Expand Down
112 changes: 112 additions & 0 deletions file/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package file

import (
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
"testing"

"github.com/stretchr/testify/assert"

"github.com/hbagdi/deck/state"
"github.com/hbagdi/go-kong/kong"
)

func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
log.SetOutput(os.Stderr)
}()
os.Stdout = writer
os.Stderr = writer

log.SetOutput(writer)
out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, reader)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}
func TestWriteKongStateToStdoutEmptyState(t *testing.T) {
var ks, _ = state.NewKongState()
var filename = "-"
assert := assert.New(t)
assert.Equal("-", filename)
assert.NotEmpty(t, ks)
output := captureOutput(func() {
KongStateToFile(ks, filename)
})
assert.Equal("{}\n", output)

}
func TestWriteKongStateToStdoutStateWithOneService(t *testing.T) {
var ks, _ = state.NewKongState()
var filename = "-"
assert := assert.New(t)
var service state.Service
service.ID = kong.String("first")
service.Host = kong.String("example.com")
service.Name = kong.String("my-service")
ks.Services.Add(service)
output := captureOutput(func() {
KongStateToFile(ks, filename)
})
fmt.Print(service.Host)
expected := fmt.Sprintf("services:\n- host: %s\n name: %s\n", *service.Host, *service.Name)
assert.Equal(expected, output)

}
func TestWriteKongStateToStdoutStateWithOneServiceOneRoute(t *testing.T) {
var ks, _ = state.NewKongState()
var filename = "-"
assert := assert.New(t)
var service state.Service
service.ID = kong.String("first")
service.Host = kong.String("example.com")
service.Name = kong.String("my-service")
ks.Services.Add(service)

var route state.Route
route.Name = kong.String("my-route")
route.ID = kong.String("first")
route.Hosts = kong.StringSlice("example.com", "demo.example.com")
route.Service = &kong.Service{
ID: kong.String(*service.ID),
Name: kong.String(*service.Name),
}

ks.Routes.Add(route)

output := captureOutput(func() {
KongStateToFile(ks, filename)
})
fmt.Print(service.Host)
expected := fmt.Sprintf(`services:
- host: %s
name: %s
routes:
- hosts:
- %s
- %s
name: %s
`, *service.Host, *service.Name, *route.Hosts[0], *route.Hosts[1], *route.Name)
assert.Equal(expected, output)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ require (
github.com/yudai/gojsondiff v1.0.0
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 // indirect
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3Ifn
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI=
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 h1:1Fzlr8kkDLQwqMP8GxrhptBLqZG/EDpiATneiZHY998=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down

0 comments on commit a4ac4f2

Please sign in to comment.