Skip to content

Commit c2bac9c

Browse files
authored
feat: Support plugin kind in CLI publish command (#13997)
#### Summary Replaces #13996. Supports plugin kind in the CLI when publishing plugins <!--
1 parent bda21fc commit c2bac9c

5 files changed

Lines changed: 58 additions & 60 deletions

File tree

cli/cmd/publish.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818

1919
cloudquery_api "github.com/cloudquery/cloudquery-api-go"
2020
"github.com/cloudquery/cloudquery/cli/internal/auth"
21-
"github.com/cloudquery/plugin-sdk/v4/plugin"
2221
"github.com/spf13/cobra"
2322
)
2423

@@ -69,13 +68,16 @@ type PackageJSONVersion struct {
6968
SchemaVersion int `json:"schema_version"`
7069
}
7170

71+
type Kind = cloudquery_api.PluginKind
72+
7273
type PackageJSONV1 struct {
73-
Name string `json:"name"`
74-
Message string `json:"message"`
75-
Version string `json:"version"`
76-
Protocols []int `json:"protocols"`
77-
SupportedTargets []TargetBuild `json:"supported_targets"`
78-
PackageType plugin.PackageType `json:"package_type"`
74+
Name string `json:"name"`
75+
Message string `json:"message"`
76+
Version string `json:"version"`
77+
Kind cloudquery_api.PluginKind `json:"kind"`
78+
Protocols []int `json:"protocols"`
79+
SupportedTargets []TargetBuild `json:"supported_targets"`
80+
PackageType string `json:"package_type"`
7981
}
8082

8183
type TargetBuild struct {
@@ -121,18 +123,20 @@ func runPublish(ctx context.Context, cmd *cobra.Command, args []string) error {
121123
return fmt.Errorf("failed to create new draft version: %w", err)
122124
}
123125

124-
// upload table schemas
125-
fmt.Println("Uploading table schemas...")
126-
tablesJSONPath := filepath.Join(distDir, "tables.json")
127-
err = uploadTableSchemas(ctx, c, teamName, pluginName, pkgJSON.Version, tablesJSONPath)
128-
if err != nil {
129-
return fmt.Errorf("failed to upload table schemas: %w", err)
126+
if pkgJSON.Kind == cloudquery_api.Source {
127+
// upload table schemas
128+
fmt.Println("Uploading table schemas...")
129+
tablesJSONPath := filepath.Join(distDir, "tables.json")
130+
err = uploadTableSchemas(ctx, c, teamName, pluginName, tablesJSONPath, pkgJSON)
131+
if err != nil {
132+
return fmt.Errorf("failed to upload table schemas: %w", err)
133+
}
130134
}
131135

132136
// upload docs
133137
fmt.Println("Uploading docs...")
134138
docsDir := filepath.Join(distDir, "docs")
135-
err = uploadDocs(ctx, c, teamName, pluginName, pkgJSON.Version, docsDir)
139+
err = uploadDocs(ctx, c, teamName, pluginName, docsDir, pkgJSON)
136140
if err != nil {
137141
return fmt.Errorf("failed to upload docs: %w", err)
138142
}
@@ -141,7 +145,7 @@ func runPublish(ctx context.Context, cmd *cobra.Command, args []string) error {
141145
fmt.Println("Uploading binaries...")
142146
for _, t := range pkgJSON.SupportedTargets {
143147
fmt.Printf("- Uploading %s_%s...\n", t.OS, t.Arch)
144-
err = uploadBinary(ctx, c, teamName, pluginName, pkgJSON.Version, t.OS, t.Arch, path.Join(distDir, t.Path))
148+
err = uploadBinary(ctx, c, teamName, pluginName, t.OS, t.Arch, path.Join(distDir, t.Path), pkgJSON)
145149
if err != nil {
146150
return fmt.Errorf("failed to upload binary: %w", err)
147151
}
@@ -156,7 +160,7 @@ func runPublish(ctx context.Context, cmd *cobra.Command, args []string) error {
156160
if finalize {
157161
fmt.Println("Finalizing plugin version...")
158162
draft := false
159-
resp, err := c.UpdatePluginVersionWithResponse(ctx, teamName, pluginName, pkgJSON.Version, cloudquery_api.UpdatePluginVersionJSONRequestBody{
163+
resp, err := c.UpdatePluginVersionWithResponse(ctx, teamName, pkgJSON.Kind, pluginName, pkgJSON.Version, cloudquery_api.UpdatePluginVersionJSONRequestBody{
160164
Draft: &draft,
161165
})
162166
if err != nil {
@@ -191,7 +195,7 @@ func createNewDraftVersion(ctx context.Context, c *cloudquery_api.ClientWithResp
191195
SupportedTargets: targets,
192196
Checksums: checksums,
193197
}
194-
resp, err := c.CreatePluginVersionWithResponse(ctx, teamName, pluginName, pkgJSON.Version, body)
198+
resp, err := c.CreatePluginVersionWithResponse(ctx, teamName, pkgJSON.Kind, pluginName, pkgJSON.Version, body)
195199
if err != nil {
196200
return fmt.Errorf("failed to create plugin version: %w", err)
197201
}
@@ -205,20 +209,20 @@ func createNewDraftVersion(ctx context.Context, c *cloudquery_api.ClientWithResp
205209
return nil
206210
}
207211

208-
func uploadTableSchemas(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, version, tablesJSONPath string) error {
212+
func uploadTableSchemas(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, tablesJSONPath string, pkgJSON PackageJSONV1) error {
209213
b, err := os.ReadFile(tablesJSONPath)
210214
if err != nil {
211215
return fmt.Errorf("failed to read tables.json: %w", err)
212216
}
213-
tables := make([]cloudquery_api.PluginTable, 0)
217+
tables := make([]cloudquery_api.PluginTableCreate, 0)
214218
err = json.Unmarshal(b, &tables)
215219
if err != nil {
216220
return fmt.Errorf("failed to parse tables.json: %w", err)
217221
}
218222
body := cloudquery_api.CreatePluginVersionTablesJSONRequestBody{
219223
Tables: tables,
220224
}
221-
resp, err := c.CreatePluginVersionTablesWithResponse(ctx, teamName, pluginName, version, body)
225+
resp, err := c.CreatePluginVersionTablesWithResponse(ctx, teamName, pkgJSON.Kind, pluginName, pkgJSON.Version, body)
222226
if err != nil {
223227
return fmt.Errorf("failed to upload table schemas: %w", err)
224228
}
@@ -247,12 +251,12 @@ func errorFromHTTPResponse(httpResp *http.Response, resp any) error {
247251
return fmt.Errorf("error code: %v", httpResp.StatusCode)
248252
}
249253

250-
func uploadDocs(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, version, docsDir string) error {
254+
func uploadDocs(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, docsDir string, pkgJSON PackageJSONV1) error {
251255
dirEntries, err := os.ReadDir(docsDir)
252256
if err != nil {
253257
return fmt.Errorf("failed to read docs directory: %w", err)
254258
}
255-
pages := make([]cloudquery_api.PluginDocsPage, 0, len(dirEntries))
259+
pages := make([]cloudquery_api.PluginDocsPageCreate, 0, len(dirEntries))
256260
for _, dirEntry := range dirEntries {
257261
if dirEntry.IsDir() {
258262
continue
@@ -274,7 +278,7 @@ func uploadDocs(ctx context.Context, c *cloudquery_api.ClientWithResponses, team
274278
return fmt.Errorf("failed to parse ordinal_position in %s: %w", dirEntry.Name(), err)
275279
}
276280
}
277-
pages = append(pages, cloudquery_api.PluginDocsPage{
281+
pages = append(pages, cloudquery_api.PluginDocsPageCreate{
278282
Content: contentStr,
279283
Name: strings.TrimSuffix(dirEntry.Name(), ".md"),
280284
OrdinalPosition: &ordinal,
@@ -284,7 +288,7 @@ func uploadDocs(ctx context.Context, c *cloudquery_api.ClientWithResponses, team
284288
body := cloudquery_api.CreatePluginVersionDocsJSONRequestBody{
285289
Pages: pages,
286290
}
287-
resp, err := c.CreatePluginVersionDocsWithResponse(ctx, teamName, pluginName, version, body)
291+
resp, err := c.CreatePluginVersionDocsWithResponse(ctx, teamName, pkgJSON.Kind, pluginName, pkgJSON.Version, body)
288292
if err != nil {
289293
return fmt.Errorf("failed to upload docs: %w", err)
290294
}
@@ -294,9 +298,9 @@ func uploadDocs(ctx context.Context, c *cloudquery_api.ClientWithResponses, team
294298
return nil
295299
}
296300

297-
func uploadBinary(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, version, goos, goarch, localPath string) error {
301+
func uploadBinary(ctx context.Context, c *cloudquery_api.ClientWithResponses, teamName, pluginName, goos, goarch, localPath string, pkgJSON PackageJSONV1) error {
298302
target := goos + "_" + goarch
299-
resp, err := c.UploadPluginAssetWithResponse(ctx, teamName, pluginName, version, target)
303+
resp, err := c.UploadPluginAssetWithResponse(ctx, teamName, pkgJSON.Kind, pluginName, pkgJSON.Version, target)
300304
if err != nil {
301305
return fmt.Errorf("failed to upload binary: %w", err)
302306
}
@@ -314,11 +318,7 @@ func uploadBinary(ctx context.Context, c *cloudquery_api.ClientWithResponses, te
314318
return fmt.Errorf("upload response is nil, failed to upload binary")
315319
}
316320
uploadURL := resp.JSON201.Url
317-
if uploadURL == nil {
318-
return fmt.Errorf("upload URL is nil, failed to upload binary")
319-
}
320-
321-
err = uploadFile(*uploadURL, localPath)
321+
err = uploadFile(uploadURL, localPath)
322322
if err != nil {
323323
return fmt.Errorf("failed to upload file: %w", err)
324324
}

cli/cmd/publish_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ func TestPublish(t *testing.T) {
1717
t.Setenv("CQ_API_KEY", "testkey")
1818

1919
wantCalls := map[string]int{
20-
"PUT /plugins/cloudquery/test/versions/v1.2.3": 1,
21-
"PUT /plugins/cloudquery/test/versions/v1.2.3/tables": 1,
22-
"PUT /plugins/cloudquery/test/versions/v1.2.3/docs": 1,
23-
"POST /plugins/cloudquery/test/versions/v1.2.3/assets/linux_amd64": 1,
24-
"POST /plugins/cloudquery/test/versions/v1.2.3/assets/darwin_amd64": 1,
20+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3": 1,
21+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3/tables": 1,
22+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3/docs": 1,
23+
"POST /plugins/cloudquery/source/test/versions/v1.2.3/assets/linux_amd64": 1,
24+
"POST /plugins/cloudquery/source/test/versions/v1.2.3/assets/darwin_amd64": 1,
2525
"PUT /upload-linux": 1,
2626
"PUT /upload-darwin": 1,
2727
}
@@ -30,26 +30,26 @@ func TestPublish(t *testing.T) {
3030
w.Header().Set("Content-Type", "application/json")
3131
gotCalls[r.Method+" "+r.URL.Path]++
3232
switch r.URL.Path {
33-
case "/plugins/cloudquery/test/versions/v1.2.3":
33+
case "/plugins/cloudquery/source/test/versions/v1.2.3":
3434
checkAuthHeader(t, r)
3535
w.WriteHeader(http.StatusCreated)
3636
w.Write([]byte(`{"name": "v1.2.3"}`))
3737
checkCreateVersionRequest(t, r)
38-
case "/plugins/cloudquery/test/versions/v1.2.3/tables":
38+
case "/plugins/cloudquery/source/test/versions/v1.2.3/tables":
3939
checkAuthHeader(t, r)
4040
w.WriteHeader(http.StatusCreated)
4141
w.Write([]byte(`{}`))
4242
checkCreateTablesRequest(t, r)
43-
case "/plugins/cloudquery/test/versions/v1.2.3/docs":
43+
case "/plugins/cloudquery/source/test/versions/v1.2.3/docs":
4444
checkAuthHeader(t, r)
4545
w.WriteHeader(http.StatusCreated)
4646
w.Write([]byte(`{}`))
4747
checkCreateDocsRequest(t, r)
48-
case "/plugins/cloudquery/test/versions/v1.2.3/assets/linux_amd64":
48+
case "/plugins/cloudquery/source/test/versions/v1.2.3/assets/linux_amd64":
4949
checkAuthHeader(t, r)
5050
w.WriteHeader(http.StatusCreated)
5151
w.Write([]byte(fmt.Sprintf(`{"url": "%s"}`, "http://"+r.Host+"/upload-linux")))
52-
case "/plugins/cloudquery/test/versions/v1.2.3/assets/darwin_amd64":
52+
case "/plugins/cloudquery/source/test/versions/v1.2.3/assets/darwin_amd64":
5353
checkAuthHeader(t, r)
5454
w.WriteHeader(http.StatusCreated)
5555
w.Write([]byte(fmt.Sprintf(`{"url": "%s"}`, "http://"+r.Host+"/upload-darwin")))
@@ -79,22 +79,22 @@ func TestPublishFinalize(t *testing.T) {
7979
t.Setenv("CQ_API_KEY", "testkey")
8080

8181
wantCalls := map[string]int{
82-
"PUT /plugins/cloudquery/test/versions/v1.2.3": 1,
83-
"PUT /plugins/cloudquery/test/versions/v1.2.3/tables": 1,
84-
"PUT /plugins/cloudquery/test/versions/v1.2.3/docs": 1,
85-
"POST /plugins/cloudquery/test/versions/v1.2.3/assets/linux_amd64": 1,
86-
"POST /plugins/cloudquery/test/versions/v1.2.3/assets/darwin_amd64": 1,
87-
"PUT /upload-linux": 1,
88-
"PUT /upload-darwin": 1,
89-
"PATCH /plugins/cloudquery/test/versions/v1.2.3": 1,
82+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3": 1,
83+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3/tables": 1,
84+
"PUT /plugins/cloudquery/source/test/versions/v1.2.3/docs": 1,
85+
"POST /plugins/cloudquery/source/test/versions/v1.2.3/assets/linux_amd64": 1,
86+
"POST /plugins/cloudquery/source/test/versions/v1.2.3/assets/darwin_amd64": 1,
87+
"PUT /upload-linux": 1,
88+
"PUT /upload-darwin": 1,
89+
"PATCH /plugins/cloudquery/source/test/versions/v1.2.3": 1,
9090
}
9191
gotCalls := map[string]int{}
9292
gotUploads := 0
9393
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9494
w.Header().Set("Content-Type", "application/json")
9595
gotCalls[r.Method+" "+r.URL.Path]++
9696
switch r.URL.Path {
97-
case "/plugins/cloudquery/test/versions/v1.2.3":
97+
case "/plugins/cloudquery/source/test/versions/v1.2.3":
9898
checkAuthHeader(t, r)
9999
if r.Method == "PATCH" {
100100
checkUpdateVersionRequest(t, r)
@@ -108,20 +108,20 @@ func TestPublishFinalize(t *testing.T) {
108108
w.Write([]byte(`{"name": "v1.2.3"}`))
109109
checkCreateVersionRequest(t, r)
110110
}
111-
case "/plugins/cloudquery/test/versions/v1.2.3/tables":
111+
case "/plugins/cloudquery/source/test/versions/v1.2.3/tables":
112112
checkAuthHeader(t, r)
113113
w.WriteHeader(http.StatusCreated)
114114
w.Write([]byte(`{}`))
115115
checkCreateTablesRequest(t, r)
116-
case "/plugins/cloudquery/test/versions/v1.2.3/docs":
116+
case "/plugins/cloudquery/source/test/versions/v1.2.3/docs":
117117
checkAuthHeader(t, r)
118118
w.WriteHeader(http.StatusCreated)
119119
w.Write([]byte(`{}`))
120-
case "/plugins/cloudquery/test/versions/v1.2.3/assets/linux_amd64":
120+
case "/plugins/cloudquery/source/test/versions/v1.2.3/assets/linux_amd64":
121121
checkAuthHeader(t, r)
122122
w.WriteHeader(http.StatusCreated)
123123
w.Write([]byte(fmt.Sprintf(`{"url": "%s"}`, "http://"+r.Host+"/upload-linux")))
124-
case "/plugins/cloudquery/test/versions/v1.2.3/assets/darwin_amd64":
124+
case "/plugins/cloudquery/source/test/versions/v1.2.3/assets/darwin_amd64":
125125
checkAuthHeader(t, r)
126126
w.WriteHeader(http.StatusCreated)
127127
w.Write([]byte(fmt.Sprintf(`{"url": "%s"}`, "http://"+r.Host+"/upload-darwin")))

cli/cmd/testdata/dist-v1/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"name": "test",
44
"message": "## This is a subtitle\n\nThis is a paragraph.\n",
55
"version": "v1.2.3",
6+
"kind": "source",
67
"protocols": [
78
3
89
],

cli/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/apache/arrow/go/v14 v14.0.0-20230905030402-a526ba697d4e
88
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
99
github.com/cenkalti/backoff/v4 v4.2.1
10-
github.com/cloudquery/cloudquery-api-go v1.0.2
10+
github.com/cloudquery/cloudquery-api-go v1.0.3
1111
github.com/cloudquery/plugin-pb-go v1.10.0
1212
github.com/cloudquery/plugin-sdk/v4 v4.10.2
1313
github.com/getsentry/sentry-go v0.20.0
@@ -95,7 +95,6 @@ require (
9595
github.com/pmezard/go-difflib v1.0.0 // indirect
9696
github.com/rivo/uniseg v0.4.4 // indirect
9797
github.com/russross/blackfriday/v2 v2.1.0 // indirect
98-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect
9998
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
10099
github.com/sirupsen/logrus v1.9.3 // indirect
101100
github.com/spf13/pflag v1.0.5 // indirect

cli/go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo
4444
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
4545
github.com/cloudquery/arrow/go/v14 v14.0.0-20230916001126-ffb7089245ac h1:Krr/L41tEYCxdUZfck3QI0mPSOuiMxUfZGWGlQymZ+U=
4646
github.com/cloudquery/arrow/go/v14 v14.0.0-20230916001126-ffb7089245ac/go.mod h1:/SqmdO2dsWqFHqQQeupnsr0ollL8C91n3x0I72rArY8=
47-
github.com/cloudquery/cloudquery-api-go v1.0.2 h1:ZleWU9DZRxpD9df2sV25QyEDPXT0EADHpwkQgs1l+rA=
48-
github.com/cloudquery/cloudquery-api-go v1.0.2/go.mod h1:oyNUZZ6CKjPapxMbmE/qR2vdo9/G+tuCdF7uXT1LYuM=
47+
github.com/cloudquery/cloudquery-api-go v1.0.3 h1:Jy6SBUjGUumbvmmwZ/8ggbSA2iw2W1dzOMjwe9CHEvE=
48+
github.com/cloudquery/cloudquery-api-go v1.0.3/go.mod h1:oyNUZZ6CKjPapxMbmE/qR2vdo9/G+tuCdF7uXT1LYuM=
4949
github.com/cloudquery/plugin-pb-go v1.10.0 h1:76DSubESX8HWFJZaB90J0mPDnmUoXEzyXutwL0w0TfI=
5050
github.com/cloudquery/plugin-pb-go v1.10.0/go.mod h1:K0L9ugyPVKBgmxhWwr7wNEE/khfJr1lTmhl6HfkhXYA=
5151
github.com/cloudquery/plugin-sdk/v4 v4.10.2 h1:CHu0Rri+CcGLlyQ5roSrLWon+pl1SkucOvjaUfm6Pek=
@@ -227,8 +227,6 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
227227
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
228228
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
229229
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
230-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
231-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
232230
github.com/schollz/closestmatch v2.1.0+incompatible h1:Uel2GXEpJqOWBrlyI+oY9LTiyyjYS17cCYRqP13/SHk=
233231
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
234232
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=

0 commit comments

Comments
 (0)