cli/pkg/cmd/attestation/io/handler.go
Meredith Lancaster 63640b16a7
Update gh attestation verify output (#8991)
* start updating default verify cmd output

Signed-off-by: Meredith Lancaster <malancas@github.com>

* start adding support for printing a table of attestation details

Signed-off-by: Meredith Lancaster <malancas@github.com>

* extract attestation details from verification result

Signed-off-by: Meredith Lancaster <malancas@github.com>

* condense logging

Signed-off-by: Meredith Lancaster <malancas@github.com>

* update logging from feedback

Signed-off-by: Meredith Lancaster <malancas@github.com>

* update error logging

Signed-off-by: Meredith Lancaster <malancas@github.com>

* cleanup more error logging

Signed-off-by: Meredith Lancaster <malancas@github.com>

* include test data for printing to table in the mock sigstore verifier response

Signed-off-by: Meredith Lancaster <malancas@github.com>

* fix linter err

Signed-off-by: Meredith Lancaster <malancas@github.com>

* Update pkg/cmd/attestation/verification/mock_verifier.go

Co-authored-by: Phill MV <phillmv@github.com>

---------

Signed-off-by: Meredith Lancaster <malancas@github.com>
Co-authored-by: Phill MV <phillmv@github.com>
2024-04-24 14:03:35 -06:00

78 lines
1.7 KiB
Go

package io
import (
"fmt"
"github.com/cli/cli/v2/internal/tableprinter"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/utils"
)
type Handler struct {
ColorScheme *iostreams.ColorScheme
IO *iostreams.IOStreams
debugEnabled bool
}
func NewHandler(io *iostreams.IOStreams) *Handler {
enabled, _ := utils.IsDebugEnabled()
return &Handler{
ColorScheme: io.ColorScheme(),
IO: io,
debugEnabled: enabled,
}
}
func NewTestHandler() *Handler {
testIO, _, _, _ := iostreams.Test()
return NewHandler(testIO)
}
// Printf writes the formatted arguments to the stderr writer.
func (h *Handler) Printf(f string, v ...interface{}) (int, error) {
if !h.IO.IsStdoutTTY() {
return 0, nil
}
return fmt.Fprintf(h.IO.ErrOut, f, v...)
}
// Println writes the arguments to the stderr writer with a newline at the end.
func (h *Handler) Println(v ...interface{}) (int, error) {
if !h.IO.IsStdoutTTY() {
return 0, nil
}
return fmt.Fprintln(h.IO.ErrOut, v...)
}
func (h *Handler) VerbosePrint(msg string) (int, error) {
if !h.debugEnabled || !h.IO.IsStdoutTTY() {
return 0, nil
}
return fmt.Fprintln(h.IO.ErrOut, msg)
}
func (h *Handler) VerbosePrintf(f string, v ...interface{}) (int, error) {
if !h.debugEnabled || !h.IO.IsStdoutTTY() {
return 0, nil
}
return fmt.Fprintf(h.IO.ErrOut, f, v...)
}
func (h *Handler) PrintTable(headers []string, rows [][]string) error {
t := tableprinter.New(h.IO, tableprinter.WithHeader(headers...))
for _, row := range rows {
for _, field := range row {
t.AddField(field, tableprinter.WithTruncate(nil))
}
t.EndRow()
}
if err := t.Render(); err != nil {
return fmt.Errorf("failed to print output: %v", err)
}
return nil
}