Skip to content

Commit

Permalink
Add friendly output handler
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Nov 22, 2020
1 parent 2c0ea56 commit 08136af
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/kouchctl/output/friendly/friendly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package friendly

import (
"io"

"github.com/go-kivik/xkivik/v4/cmd/kouchctl/output"
"github.com/go-kivik/xkivik/v4/cmd/kouchctl/output/json"
)

type format struct{}

var _ output.Format = &format{}

// New returns a new friendly formatter.
func New() output.Format {
return &format{}
}

func (f *format) Output(w io.Writer, r io.Reader) error {
if f, ok := r.(output.FriendlyOutput); ok {
return f.Execute(w)
}
return json.New().Output(w, r)
}
56 changes: 56 additions & 0 deletions cmd/kouchctl/output/friendly/friendly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package friendly

import (
"bytes"
"io"
"strings"
"testing"

"gitlab.com/flimzy/testy"
)

type friendly struct {
io.Reader
}

func (friendly) Execute(w io.Writer) error {
_, err := w.Write([]byte("Friendly!"))
return err
}

func TestOutput(t *testing.T) {
type tt struct {
r io.Reader
err string
}

tests := testy.NewTable()
tests.Add("non-friendly output", tt{
r: strings.NewReader(`{"foo":"bar"}`),
})
tests.Add("friendly output", tt{
r: friendly{},
})

tests.Run(t, func(t *testing.T, tt tt) {
buf := &bytes.Buffer{}
f := format{}
err := f.Output(buf, tt.r)
testy.Error(t, tt.err, err)
if d := testy.DiffText(testy.Snapshot(t), buf); d != nil {
t.Error(d)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Friendly!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
22 changes: 22 additions & 0 deletions cmd/kouchctl/output/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package output

import (
"io"
)

// FriendlyOutput produces friendly output.
type FriendlyOutput interface {
Execute(io.Writer) error
}

0 comments on commit 08136af

Please sign in to comment.