-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
146 lines (134 loc) · 2.8 KB
/
io.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/nekrassov01/mintab"
)
type format int
const (
formatJSON format = iota
formatTextTable
formatMarkdownTable
formatBacklogTable
)
var formats = []string{
"json",
"table",
"markdown",
"backlog",
}
func (f format) String() string {
if f >= 0 && int(f) < len(formats) {
return formats[f]
}
return ""
}
func fromList(fp string) ([]string, error) {
if fp == "" {
return nil, errors.New("no file provided")
}
f, err := os.Open(filepath.Clean(fp))
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var lines []string
for scanner.Scan() {
line, err := checkLine(scanner.Text())
if err != nil {
return nil, err
}
if line != "" {
lines = append(lines, line)
}
}
if len(lines) == 0 {
return nil, fmt.Errorf("no line provided: %s", fp)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func checkLine(line string) (string, error) {
line = strings.TrimSpace(line)
if strings.Contains(line, ",") {
return "", fmt.Errorf("invalid line detected: comma not allowed")
}
line = strings.Trim(line, `'"`)
return line, nil
}
func out(infos []*certInfo, w io.Writer, format string, omit bool) error {
switch format {
case formatJSON.String():
return toJSON(infos, w)
case formatTextTable.String(), formatMarkdownTable.String(), formatBacklogTable.String():
return toTable(infos, w, format, omit)
default:
return fmt.Errorf("invalid format: allowed values: %s", pipeJoin(formats))
}
}
func toJSON(infos []*certInfo, w io.Writer) error {
b := json.NewEncoder(w)
b.SetIndent("", " ")
return b.Encode(infos)
}
func toTable(infos []*certInfo, w io.Writer, format string, omit bool) error {
opts := make([]mintab.Option, 0, 2)
switch format {
case formatTextTable.String():
case formatMarkdownTable.String():
opts = append(opts, mintab.WithFormat(mintab.MarkdownFormat))
case formatBacklogTable.String():
opts = append(opts, mintab.WithFormat(mintab.BacklogFormat))
}
if omit {
opts = append(opts, mintab.WithIgnoreFields([]int{8, 9}))
}
table := mintab.New(w, opts...)
if err := table.Load(toInput(infos)); err != nil {
return err
}
table.Render()
return nil
}
func toInput(infos []*certInfo) mintab.Input {
header := []string{
"DomainName",
"AccessPort",
"IPAddresses",
"Issuer",
"CommonName",
"SANs",
"NotBefore",
"NotAfter",
"CurrentTime",
"DaysLeft",
}
data := make([][]any, len(infos))
for i, info := range infos {
data[i] = []any{
info.DomainName,
info.AccessPort,
info.IPAddresses,
info.Issuer,
info.CommonName,
info.SANs,
info.NotBefore,
info.NotAfter,
info.CurrentTime,
info.DaysLeft,
}
}
return mintab.Input{
Header: header,
Data: data,
}
}