diff --git a/docs/generator.go b/docs/generator.go index 9923845d20..9311b0c7ac 100644 --- a/docs/generator.go +++ b/docs/generator.go @@ -128,6 +128,18 @@ func (g *Generator) Generate(dir string, format Format) error { } } +func (g *Generator) GenerateJSON(dir string, format Format) error { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + return err + } + switch format { + case FormatJSON: + return g.renderTablesAsJSON(dir) + default: + return fmt.Errorf("unsupported format: %v", format) + } +} + // setDestinationManagedCqColumns overwrites or adds the CQ columns that are managed by the destination plugins (_cq_sync_time, _cq_source_name). // func setDestinationManagedCqColumns(tables []*schema.Table) { // for _, table := range tables { diff --git a/serve/deadcode_test.go b/serve/deadcode_test.go new file mode 100644 index 0000000000..a190a39f52 --- /dev/null +++ b/serve/deadcode_test.go @@ -0,0 +1,23 @@ +package serve + +import ( + "os/exec" + "path/filepath" + "strings" + "testing" +) + +func TestPluginBinaryKeepsMethodDeadCodeElimination(t *testing.T) { + if testing.Short() { + t.Skip("builds a plugin binary") + } + out, err := exec.Command("go", "build", "-tags=grpcnotrace", "-ldflags=-dumpdep", "-o", filepath.Join(t.TempDir(), "plugin"), "./testdata/deadcode").CombinedOutput() + if err != nil { + t.Fatalf("go build: %v\n%s", err, out[max(0, len(out)-4096):]) + } + for line := range strings.Lines(string(out)) { + if strings.Contains(line, "") { + t.Fatalf("a reflect method call is reachable, so the linker keeps every exported method of every type: %s", line) + } + } +} diff --git a/serve/docs.go b/serve/docs.go index e403a3445f..db5bb56a93 100644 --- a/serve/docs.go +++ b/serve/docs.go @@ -48,7 +48,7 @@ func (s *PluginServe) newCmdPluginDoc() *cobra.Command { return errors.New("only json format is supported. If need to generate markdown, use the `cloudquery tables` command") } - return g.Generate(args[0], docs.FormatJSON) + return g.GenerateJSON(args[0], docs.FormatJSON) }, } cmd.Flags().Var(format, "format", fmt.Sprintf("output format. one of: %s", strings.Join(format.Allowed, ","))) diff --git a/serve/testdata/deadcode/main.go b/serve/testdata/deadcode/main.go new file mode 100644 index 0000000000..9f12d678d1 --- /dev/null +++ b/serve/testdata/deadcode/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "context" + "log" + + "github.com/cloudquery/plugin-sdk/v4/internal/memdb" + "github.com/cloudquery/plugin-sdk/v4/plugin" + "github.com/cloudquery/plugin-sdk/v4/serve" +) + +func main() { + p := plugin.NewPlugin("deadcode", "v1.0.0", memdb.NewMemDBClient) + if err := serve.Plugin(p).Serve(context.Background()); err != nil { + log.Fatal(err) + } +}