Skip to content

Commit

Permalink
add plugin print
Browse files Browse the repository at this point in the history
  • Loading branch information
laskoviymishka committed Jul 4, 2024
1 parent 115476a commit e6c8d4f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"cmd/trcli/config/config.go":"transfer_manager/go/cmd/trcli/config/config.go",
"cmd/trcli/config/model.go":"transfer_manager/go/cmd/trcli/config/model.go",
"cmd/trcli/main.go":"transfer_manager/go/cmd/trcli/main.go",
"cmd/trcli/pluginprinter/table.go":"transfer_manager/go/cmd/trcli/pluginprinter/table.go",
"cmd/trcli/replicate/replicate.go":"transfer_manager/go/cmd/trcli/replicate/replicate.go",
"cmd/trcli/replicate/tests/ch_init.sql":"transfer_manager/go/cmd/trcli/replicate/tests/ch_init.sql",
"cmd/trcli/replicate/tests/dump/pg_init.sql":"transfer_manager/go/cmd/trcli/replicate/tests/dump/pg_init.sql",
Expand Down
19 changes: 19 additions & 0 deletions cmd/trcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (

"github.com/doublecloud/tross/cmd/trcli/activate"
"github.com/doublecloud/tross/cmd/trcli/check"
"github.com/doublecloud/tross/cmd/trcli/pluginprinter"
"github.com/doublecloud/tross/cmd/trcli/replicate"
"github.com/doublecloud/tross/cmd/trcli/upload"
"github.com/doublecloud/tross/cmd/trcli/validate"
"github.com/doublecloud/tross/internal/core/log/zap"
"github.com/doublecloud/tross/internal/core/xerrors"
"github.com/doublecloud/tross/internal/logger"
"github.com/doublecloud/tross/pkg/abstract/model"
"github.com/doublecloud/tross/pkg/cobraaux"
_ "github.com/doublecloud/tross/pkg/dataplane"
"github.com/doublecloud/tross/pkg/parsers"
transformers_registry "github.com/doublecloud/tross/pkg/transformer"
"github.com/spf13/cobra"
zp "go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -51,6 +55,7 @@ func main() {
return nil
},
}
cobraaux.RegisterCommand(rootCommand, listRegisteredPlugins())
cobraaux.RegisterCommand(rootCommand, activate.ActivateCommand())
cobraaux.RegisterCommand(rootCommand, check.CheckCommand())
cobraaux.RegisterCommand(rootCommand, replicate.ReplicateCommand())
Expand All @@ -71,3 +76,17 @@ func newLoggerConfig() zp.Config {
cfg.ErrorOutputPaths = []string{"stderr"}
return cfg
}

func listRegisteredPlugins() *cobra.Command {
cmd := &cobra.Command{
Use: "print-plugins",
Short: "List registered plugins",
Run: func(cmd *cobra.Command, args []string) {
err := pluginprinter.Print(model.KnownSources(), model.KnownDestinations(), parsers.KnownParsers(), transformers_registry.KnownTransformerNames(), "api")
if err != nil {
logger.Log.Infof("Failed to print known plugins: %s", err)
}
},
}
return cmd
}
89 changes: 89 additions & 0 deletions cmd/trcli/pluginprinter/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package pluginprinter

import (
"io"
"os"

"github.com/doublecloud/tross/internal/core/xerrors"
"github.com/olekukonko/tablewriter"
)

func boolToString(value bool) string {
if value {
return "+"
}
return ""
}

func Print(sources, destinations, parsers, transformers []string, title string) error {
if _, err := io.WriteString(os.Stdout, title+"\n"); err != nil {
return xerrors.Errorf("unable to write string title: %w", err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(true)
table.SetHeader([]string{"Provider", "Source", "Target"})

mapProviders := make(map[string][]bool)
for _, source := range sources {
if _, ok := mapProviders[source]; !ok {
mapProviders[source] = make([]bool, 2)
}
mapProviders[source][0] = true
}
for _, destination := range destinations {
if _, ok := mapProviders[destination]; !ok {
mapProviders[destination] = make([]bool, 2)
}
mapProviders[destination][1] = true
}

for provider, row := range mapProviders {
oneRow := make([]string, len(row)+1)
for i, file := range row {
oneRow[i+1] = boolToString(file)
}
oneRow[0] = provider
table.Append(oneRow)
}

table.Render()

if _, err := io.WriteString(os.Stdout, "\n"); err != nil {
return xerrors.Errorf("unable to write string eol: %w", err)
}

tableParser := tablewriter.NewWriter(os.Stdout)
tableParser.SetRowLine(true)
tableParser.SetHeader([]string{"Parser", "Imported"})

for _, parser := range parsers {
row := make([]string, 2)
row[0] = parser
row[1] = boolToString(true)
tableParser.Append(row)
}

tableParser.Render()

if _, err := io.WriteString(os.Stdout, "\n"); err != nil {
return xerrors.Errorf("unable to write string eol: %w", err)
}

tableTransformer := tablewriter.NewWriter(os.Stdout)
tableTransformer.SetRowLine(true)
tableTransformer.SetHeader([]string{"Transformer", "Imported"})

for _, transformer := range transformers {
row := make([]string, 2)
row[0] = transformer
row[1] = boolToString(true)
tableTransformer.Append(row)
}
tableTransformer.Render()

if _, err := io.WriteString(os.Stdout, "\n"); err != nil {
return xerrors.Errorf("unable to write string eol: %w", err)
}

return nil
}

0 comments on commit e6c8d4f

Please sign in to comment.