Skip to content

Commit

Permalink
feat: output bytes intead of writing to io.Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufhm committed Oct 21, 2024
1 parent 60c024a commit 33b4d29
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
27 changes: 14 additions & 13 deletions pkg/lagoon/problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package lagoon

import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -121,23 +121,24 @@ func (p *Lagoon) MustHaveEnvVars() {
}
}

func (p *Lagoon) Output(w io.Writer) error {
func (p *Lagoon) Output() ([]byte, error) {
if !p.PushProblemsToInsightsRemote {
return nil
return nil, nil
}

buf := bufio.NewWriter(w)
buf := bytes.Buffer{}
bufW := bufio.NewWriter(&buf)
problems := []Problem{}

if p.ResultList.TotalBreaches == 0 {
InitClient(p.ApiBaseUrl, p.ApiToken)
err := p.DeleteProblems()
if err != nil {
return err
return nil, err
}
fmt.Fprint(buf, "no breach to push to Lagoon; only deleted previous problems")
buf.Flush()
return nil
fmt.Fprint(bufW, "no breach to push to Lagoon; only deleted previous problems")
bufW.Flush()
return buf.Bytes(), nil
}

for _, r := range p.ResultList.Results {
Expand Down Expand Up @@ -172,14 +173,14 @@ func (p *Lagoon) Output(w io.Writer) error {
if err == nil { // we have a token, and so we can proceed via the internal service call
err = ProblemsToInsightsRemote(problems, p.InsightsRemoteEndpoint, bearerToken)
if err != nil {
return err
return nil, err
}
} else {
return err
return nil, err
}
fmt.Fprintln(buf, "successfully pushed problems to Lagoon Remote")
buf.Flush()
return nil
fmt.Fprintln(bufW, "successfully pushed problems to Lagoon Remote")
bufW.Flush()
return buf.Bytes(), nil
}

func (p *Lagoon) DeleteProblems() error {
Expand Down
11 changes: 7 additions & 4 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"io"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/salsadigitalauorg/shipshape/pkg/result"
)

type Outputter interface {
AddFlags(*cobra.Command)
Output(io.Writer) error
Output() ([]byte, error)
}

var Registry = map[string]func(*result.ResultList) Outputter{}
Expand Down Expand Up @@ -50,7 +48,12 @@ func ParseConfig(raw map[string]interface{}, rl *result.ResultList) {

func OutputAll(w io.Writer) error {
for _, p := range Outputters {
if err := p.Output(w); err != nil {
buf, err := p.Output()
if err != nil {
return err
}

if _, err := w.Write(buf); err != nil {
return err
}
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/output/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package output

import (
"bufio"
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
Expand Down Expand Up @@ -68,22 +69,23 @@ func (p *Stdout) EnvironmentOverrides() {
}
}

func (p *Stdout) Output(w io.Writer) error {
func (p *Stdout) Output() ([]byte, error) {
var buf bytes.Buffer
switch p.Format {
case "pretty":
p.Pretty(w)
p.Pretty(&buf)
case "table":
p.Table(w)
p.Table(&buf)
case "json":
data, err := json.Marshal(p.ResultList)
if err != nil {
return fmt.Errorf("unable to convert result to json: %+v", err)
return nil, fmt.Errorf("unable to convert result to json: %+v", err)
}
fmt.Fprintln(w, string(data))
fmt.Fprintln(&buf, string(data))
case "junit":
p.JUnit(w)
p.JUnit(&buf)
}
return nil
return buf.Bytes(), nil
}

// TableDisplay generates the tabular output for the ResultList.
Expand Down
40 changes: 14 additions & 26 deletions pkg/output/stdout_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package output_test

import (
"bufio"
"bytes"
"testing"
"text/tabwriter"

"github.com/stretchr/testify/assert"

Expand All @@ -17,17 +15,16 @@ func TestTableDisplay(t *testing.T) {
assert := assert.New(t)

var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
s := &Stdout{Format: "table", ResultList: &result.ResultList{}}
s.Table(w)
s.Table(&buf)
assert.Equal(
"No result available; ensure your shipshape.yml is configured correctly.\n",
buf.String())

buf = bytes.Buffer{}
s = &Stdout{Format: "table", ResultList: &result.ResultList{
Results: []result.Result{{Name: "a", Status: result.Pass}}}}
s.Table(w)
s.Table(&buf)
assert.Equal("NAME STATUS PASSES FAILS\n"+
"a Pass \n", buf.String())

Expand All @@ -39,7 +36,7 @@ func TestTableDisplay(t *testing.T) {
{Name: "c", Status: result.Pass},
},
}}
s.Table(w)
s.Table(&buf)
assert.Equal("NAME STATUS PASSES FAILS\n"+
"a Pass \n"+
"b Pass \n"+
Expand Down Expand Up @@ -78,7 +75,7 @@ func TestTableDisplay(t *testing.T) {
},
},
}}
s.Table(w)
s.Table(&buf)
assert.Equal("NAME STATUS PASSES FAILS\n"+
"a Pass Pass a \n"+
" Pass ab \n"+
Expand All @@ -100,8 +97,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("No result available; ensure your shipshape.yml is configured correctly.\n", buf.String())
})

Expand All @@ -112,9 +108,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
buf = bytes.Buffer{}
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("Ship is in top shape; no breach detected!\n", buf.String())
})

Expand All @@ -130,8 +124,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("# Breaches were detected\n\n ### b\n -- Fail b\n\n", buf.String())
})

Expand All @@ -142,8 +135,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("Ship is in top shape; no breach detected!\n", buf.String())
})

Expand All @@ -166,8 +158,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("Breaches were detected but were all fixed successfully!\n\n"+
" ### a\n -- fixed 1\n\n", buf.String())
})
Expand Down Expand Up @@ -199,8 +190,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("Breaches were detected but not all of them could be "+
"fixed as they are either not supported yet or there were errors "+
"when trying to remediate.\n\n"+
Expand All @@ -227,8 +217,7 @@ func TestPrettyDisplay(t *testing.T) {

s := &Stdout{Format: "pretty", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.Pretty(w)
s.Pretty(&buf)
assert.Equal("Breaches were detected but none of them could be "+
"fixed as there were errors when trying to remediate.\n\n"+
"# Non-remediated breaches\n\n"+
Expand All @@ -242,8 +231,7 @@ func TestJUnit(t *testing.T) {
rl := result.NewResultList(false)
s := &Stdout{Format: "junit", ResultList: &rl}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
s.JUnit(w)
s.JUnit(&buf)
assert.Equal(`<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" errors="0"></testsuites>
`, buf.String())
Expand All @@ -252,7 +240,7 @@ func TestJUnit(t *testing.T) {
rl.Results = append(rl.Results, result.Result{
Name: "a", Status: result.Pass})
buf = bytes.Buffer{}
s.JUnit(w)
s.JUnit(&buf)
assert.Equal(`<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" errors="0">
<testsuite name="test-check" tests="0" errors="0">
Expand All @@ -270,7 +258,7 @@ func TestJUnit(t *testing.T) {
},
})
buf = bytes.Buffer{}
s.JUnit(w)
s.JUnit(&buf)
assert.Equal(`<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" errors="0">
<testsuite name="test-check" tests="0" errors="0">
Expand Down

0 comments on commit 33b4d29

Please sign in to comment.