/
table.go
65 lines (58 loc) · 2.16 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package writer
import (
"context"
"fmt"
"io"
"github.com/atosatto/ansible-requirements-lint/pkg/errors"
"github.com/atosatto/ansible-requirements-lint/pkg/linter"
"github.com/olekukonko/tablewriter"
)
// TableWriter formats the Linters output in an ASCII table format
type TableWriter struct {
Verbose bool
}
// WriteUpdates write Linters results in ASCII table format to the given io.Writer
func (t TableWriter) WriteUpdates(ctx context.Context, w io.Writer, input <-chan linter.Result) error {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Name", "Current Version", "Latest Version", "Status"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
defer table.Render()
for {
select {
case <-ctx.Done():
return nil
case res, more := <-input:
// return if there are no more input results
if !more {
return nil
}
// get the role name and the Update metadata
var roleName = roleName(res.Role)
var meta = metadataToUpdate(res)
// print the text formatted message
if res.Level != linter.LevelInfo || t.Verbose {
switch {
case errors.IsRoleVersionNotFoundError(res.Err) && res.Role.Version != "":
// we cannot determine whether there is any update cause we haven't found
// the version among the one available for the role
table.Append([]string{roleName, res.Role.Version, meta.ToVersion, "Update"})
case errors.IsRoleVersionNotFoundError(res.Err):
// the user has not defined any version for the role
table.Append([]string{roleName, "-", meta.ToVersion, "Update"})
case errors.IsRoleNotFoundError(res.Err):
table.Append([]string{roleName, "-", "-", "Role Not Found"})
case res.Err != nil:
// there have been an error fetching for the version
table.Append([]string{roleName, "-", "-", fmt.Sprintf("Error: %v", res.Err)})
case meta.IsUpdate:
// there is an update for the role
table.Append([]string{roleName, res.Role.Version, meta.ToVersion, "Update"})
default:
// the role is at the latest version
table.Append([]string{roleName, res.Role.Version, meta.ToVersion, "Ok"})
}
}
}
}
}