Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transition code to allow migration to genericcli table printer #317

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cmd/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"

"github.com/fatih/color"
"github.com/fi-ts/cloudctl/cmd/output"
"github.com/fi-ts/cloudctl/cmd/tableprinters"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/spf13/viper"
)
Expand All @@ -19,7 +19,18 @@ func newPrinterFromCLI(out io.Writer) printers.Printer {
case "json":
printer = printers.NewJSONPrinter().WithOut(out)
case "table", "wide", "markdown":
printer = output.New()
tp := tableprinters.New()

tablePrinter := printers.NewTablePrinter(&printers.TablePrinterConfig{
ToHeaderAndRows: tp.ToHeaderAndRows,
Wide: format == "wide",
Markdown: format == "markdown",
NoHeaders: viper.GetBool("no-headers"),
}).WithOut(out)

tp.SetPrinter(tablePrinter)

printer = tablePrinter
case "template":
printer = printers.NewTemplatePrinter(viper.GetString("template")).WithOut(out)
default:
Expand Down
37 changes: 37 additions & 0 deletions cmd/tableprinters/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tableprinters

import (
"io"

"github.com/fi-ts/cloudctl/cmd/output"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
)

type TablePrinter struct {
t *printers.TablePrinter
// TODO: we want to slowly migrate to the genericcli table printer
// after everything was shifted to this package we can remove the "oldPrinter"
oldPrinter printers.Printer
}

func New() *TablePrinter {
return &TablePrinter{
oldPrinter: output.New(),
}
}

func (t *TablePrinter) SetPrinter(printer *printers.TablePrinter) {
t.t = printer
}

func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]string, error) {
// TODO: migrate old output package code to here
// switch d := data.(type) {
// default:
// return nil, nil, t.oldPrinter.Print(data)
// }
//
// fallback to old printer for as long as the migration takes:
t.t.WithOut(io.Discard)
return nil, nil, t.oldPrinter.Print(data)
}
Loading