Skip to content

Commit f6471e3

Browse files
authored
fix: Skip tables.json when packaging destinations (#1238)
This adds a new `type` (`source` or `destination`) argument to the `package` command, and switches the ordering of arguments bit (since we're making a breaking change to it anyway). The `type` is written to package.json as well, and if it is `destination`, we don't create a `tables.json`.
1 parent f6c1cab commit f6471e3

File tree

4 files changed

+131
-15
lines changed

4 files changed

+131
-15
lines changed

examples/simple_plugin/plugin/client.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77

8+
"github.com/apache/arrow/go/v14/arrow"
89
"github.com/cloudquery/plugin-sdk/examples/simple_plugin/client"
910
"github.com/cloudquery/plugin-sdk/examples/simple_plugin/services"
1011
"github.com/cloudquery/plugin-sdk/v4/message"
@@ -20,8 +21,6 @@ type Client struct {
2021
config client.Spec
2122
tables schema.Tables
2223
scheduler *scheduler.Scheduler
23-
24-
plugin.UnimplementedDestination
2524
}
2625

2726
func (c *Client) Logger() *zerolog.Logger {
@@ -55,6 +54,16 @@ func (*Client) Close(_ context.Context) error {
5554
return nil
5655
}
5756

57+
func (*Client) Write(context.Context, <-chan message.WriteMessage) error {
58+
// Not implemented, just used for testing destination packaging
59+
return nil
60+
}
61+
62+
func (*Client) Read(context.Context, *schema.Table, chan<- arrow.Record) error {
63+
// Not implemented, just used for testing destination packaging
64+
return nil
65+
}
66+
5867
func Configure(_ context.Context, logger zerolog.Logger, spec []byte, opts plugin.NewClientOptions) (plugin.Client, error) {
5968
if opts.NoConnection {
6069
return &Client{
@@ -93,4 +102,4 @@ func getTables() schema.Tables {
93102
schema.AddCqIDs(t)
94103
}
95104
return tables
96-
}
105+
}

plugin/plugin_package.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package plugin
22

3+
import "errors"
4+
35
const (
46
GoOSLinux = "linux"
57
GoOSWindows = "windows"
@@ -9,6 +11,22 @@ const (
911
GoArchArm64 = "arm64"
1012
)
1113

14+
type Type string
15+
16+
const (
17+
TypeSource Type = "source"
18+
TypeDestination Type = "destination"
19+
)
20+
21+
func (t Type) Validate() error {
22+
switch t {
23+
case TypeSource, TypeDestination:
24+
return nil
25+
default:
26+
return errors.New("invalid type: must be one of source, destination")
27+
}
28+
}
29+
1230
type PackageType string
1331

1432
const (

serve/package.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This creates a directory with the plugin binaries, package.json and documentatio
3232
type PackageJSON struct {
3333
SchemaVersion int `json:"schema_version"`
3434
Name string `json:"name"`
35+
Type plugin.Type `json:"type"`
3536
Message string `json:"message"`
3637
Version string `json:"version"`
3738
Protocols []int `json:"protocols"`
@@ -199,11 +200,12 @@ func (*PluginServe) getModuleName(pluginDirectory string) (string, error) {
199200
return strings.TrimSpace(importPath), nil
200201
}
201202

202-
func (s *PluginServe) writePackageJSON(dir, pluginVersion, message string, targets []TargetBuild) error {
203+
func (s *PluginServe) writePackageJSON(dir string, pluginType plugin.Type, pluginVersion, message string, targets []TargetBuild) error {
203204
packageJSON := PackageJSON{
204205
SchemaVersion: 1,
205206
Name: s.plugin.Name(),
206207
Message: message,
208+
Type: pluginType,
207209
Version: pluginVersion,
208210
Protocols: s.versions,
209211
SupportedTargets: targets,
@@ -266,13 +268,17 @@ func copyFile(src, dst string) error {
266268

267269
func (s *PluginServe) newCmdPluginPackage() *cobra.Command {
268270
cmd := &cobra.Command{
269-
Use: "package -m <message> <plugin_directory> <version>",
271+
Use: "package -m <message> <source|destination> <version> <plugin_directory>",
270272
Short: pluginPackageShort,
271273
Long: pluginPackageLong,
272-
Args: cobra.ExactArgs(2),
274+
Args: cobra.ExactArgs(3),
273275
RunE: func(cmd *cobra.Command, args []string) error {
274-
pluginDirectory := args[0]
276+
pluginType := plugin.Type(args[0])
277+
if err := pluginType.Validate(); err != nil {
278+
return err
279+
}
275280
pluginVersion := args[1]
281+
pluginDirectory := args[2]
276282
distPath := path.Join(pluginDirectory, "dist")
277283
if cmd.Flag("dist-dir").Changed {
278284
distPath = cmd.Flag("dist-dir").Value.String()
@@ -304,8 +310,10 @@ func (s *PluginServe) newCmdPluginPackage() *cobra.Command {
304310
}); err != nil {
305311
return err
306312
}
307-
if err := s.writeTablesJSON(cmd.Context(), distPath); err != nil {
308-
return err
313+
if pluginType == plugin.TypeSource {
314+
if err := s.writeTablesJSON(cmd.Context(), distPath); err != nil {
315+
return err
316+
}
309317
}
310318
targets := []TargetBuild{}
311319
for _, target := range s.plugin.Targets() {
@@ -316,7 +324,7 @@ func (s *PluginServe) newCmdPluginPackage() *cobra.Command {
316324
}
317325
targets = append(targets, *targetBuild)
318326
}
319-
if err := s.writePackageJSON(distPath, pluginVersion, message, targets); err != nil {
327+
if err := s.writePackageJSON(distPath, pluginType, pluginVersion, message, targets); err != nil {
320328
return fmt.Errorf("failed to write manifest: %w", err)
321329
}
322330
if err := s.copyDocs(distPath, docsPath); err != nil {

serve/package_test.go

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/google/go-cmp/cmp"
1616
)
1717

18-
func TestPluginPackage(t *testing.T) {
18+
func TestPluginPackage_Source(t *testing.T) {
1919
_, filename, _, ok := runtime.Caller(0)
2020
if !ok {
2121
t.Fatal("failed to get current file path")
@@ -30,7 +30,6 @@ func TestPluginPackage(t *testing.T) {
3030
plugin.WithBuildTargets([]plugin.BuildTarget{
3131
{OS: plugin.GoOSLinux, Arch: plugin.GoArchAmd64},
3232
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64},
33-
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64},
3433
}),
3534
)
3635
msg := `Test message
@@ -54,7 +53,7 @@ with multiple lines and **markdown**`
5453
srv := Plugin(p)
5554
cmd := srv.newCmdPluginRoot()
5655
distDir := t.TempDir()
57-
cmd.SetArgs([]string{"package", "--dist-dir", distDir, "-m", tc.message, simplePluginPath, packageVersion})
56+
cmd.SetArgs([]string{"package", "--dist-dir", distDir, "-m", tc.message, "source", packageVersion, simplePluginPath})
5857
err := cmd.Execute()
5958
if tc.wantErr && err == nil {
6059
t.Fatalf("expected error, got nil")
@@ -68,7 +67,6 @@ with multiple lines and **markdown**`
6867
expect := []string{
6968
"docs",
7069
"package.json",
71-
"plugin-testPlugin-v1.2.3-darwin-amd64.zip",
7270
"plugin-testPlugin-v1.2.3-linux-amd64.zip",
7371
"plugin-testPlugin-v1.2.3-windows-amd64.zip",
7472
"tables.json",
@@ -80,13 +78,13 @@ with multiple lines and **markdown**`
8078
expectPackage := PackageJSON{
8179
SchemaVersion: 1,
8280
Name: "testPlugin",
81+
Type: "source",
8382
Message: msg,
8483
Version: "v1.2.3",
8584
Protocols: []int{3},
8685
SupportedTargets: []TargetBuild{
8786
{OS: plugin.GoOSLinux, Arch: plugin.GoArchAmd64, Path: "plugin-testPlugin-v1.2.3-linux-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-linux-amd64.zip"))},
8887
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64, Path: "plugin-testPlugin-v1.2.3-windows-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-windows-amd64.zip"))},
89-
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64, Path: "plugin-testPlugin-v1.2.3-darwin-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-darwin-amd64.zip"))},
9088
},
9189
PackageType: plugin.PackageTypeNative,
9290
}
@@ -102,6 +100,89 @@ with multiple lines and **markdown**`
102100
}
103101
}
104102

103+
func TestPluginPackage_Destination(t *testing.T) {
104+
_, filename, _, ok := runtime.Caller(0)
105+
if !ok {
106+
t.Fatal("failed to get current file path")
107+
}
108+
dir := filepath.Dir(filepath.Dir(filename))
109+
simplePluginPath := filepath.Join(dir, "examples/simple_plugin")
110+
packageVersion := "v1.2.3"
111+
p := plugin.NewPlugin(
112+
"testPlugin",
113+
"development",
114+
memdb.NewMemDBClient,
115+
plugin.WithBuildTargets([]plugin.BuildTarget{
116+
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64},
117+
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64},
118+
}),
119+
)
120+
msg := `Test message
121+
with multiple lines and **markdown**`
122+
testCases := []struct {
123+
name string
124+
message string
125+
wantErr bool
126+
}{
127+
{
128+
name: "inline message",
129+
message: msg,
130+
},
131+
{
132+
name: "message from file",
133+
message: "@testdata/message.txt",
134+
},
135+
}
136+
for _, tc := range testCases {
137+
t.Run(tc.name, func(t *testing.T) {
138+
srv := Plugin(p)
139+
cmd := srv.newCmdPluginRoot()
140+
distDir := t.TempDir()
141+
cmd.SetArgs([]string{"package", "--dist-dir", distDir, "-m", tc.message, "destination", packageVersion, simplePluginPath})
142+
err := cmd.Execute()
143+
if tc.wantErr && err == nil {
144+
t.Fatalf("expected error, got nil")
145+
} else if err != nil {
146+
t.Fatalf("unexpected error: %v", err)
147+
}
148+
files, err := os.ReadDir(distDir)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
expect := []string{
153+
"docs",
154+
"package.json",
155+
"plugin-testPlugin-v1.2.3-darwin-amd64.zip",
156+
"plugin-testPlugin-v1.2.3-windows-amd64.zip",
157+
}
158+
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
159+
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
160+
}
161+
162+
expectPackage := PackageJSON{
163+
SchemaVersion: 1,
164+
Name: "testPlugin",
165+
Type: "destination",
166+
Message: msg,
167+
Version: "v1.2.3",
168+
Protocols: []int{3},
169+
SupportedTargets: []TargetBuild{
170+
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64, Path: "plugin-testPlugin-v1.2.3-windows-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-windows-amd64.zip"))},
171+
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64, Path: "plugin-testPlugin-v1.2.3-darwin-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-darwin-amd64.zip"))},
172+
},
173+
PackageType: plugin.PackageTypeNative,
174+
}
175+
checkPackageJSONContents(t, filepath.Join(distDir, "package.json"), expectPackage)
176+
177+
expectDocs := []string{
178+
"configuration.md",
179+
"overview.md",
180+
}
181+
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
182+
})
183+
}
184+
}
185+
105186
func sha256sum(filename string) string {
106187
f, err := os.Open(filename)
107188
if err != nil {

0 commit comments

Comments
 (0)