-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathaddon_publish.go
More file actions
128 lines (106 loc) · 3.76 KB
/
addon_publish.go
File metadata and controls
128 lines (106 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
cloudquery_api "github.com/cloudquery/cloudquery-api-go"
"github.com/cloudquery/cloudquery-api-go/auth"
"github.com/cloudquery/cloudquery/cli/internal/api"
"github.com/cloudquery/cloudquery/cli/internal/hub"
"github.com/cloudquery/cloudquery/cli/internal/publish"
"github.com/spf13/cobra"
)
const (
addonPublishShort = "Publish to CloudQuery Hub."
addonPublishLong = `Publish to CloudQuery Hub.
This publishes an addon version to CloudQuery Hub from a manifest file.
`
addonPublishExample = `
# Publish an addon version from a manifest file
cloudquery addon publish /path/to/manifest.json v1.0.0`
)
func newCmdAddonPublish() *cobra.Command {
cmd := &cobra.Command{
Use: "publish manifest.json v1.0.0 [--finalize]",
Short: addonPublishShort,
Long: addonPublishLong,
Example: addonPublishExample,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
// Set up a channel to listen for OS signals for graceful shutdown.
ctx, cancel := context.WithCancel(cmd.Context())
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM)
go func() {
<-sigChan
cancel()
}()
return runAddonPublish(ctx, cmd, args)
},
}
cmd.Flags().BoolP("finalize", "f", false, `Finalize the addon version after publishing. If false, the addon version will be marked as draft.`)
return cmd
}
func runAddonPublish(ctx context.Context, cmd *cobra.Command, args []string) error {
tc := auth.NewTokenClient()
token, err := tc.GetToken()
if err != nil {
return fmt.Errorf("failed to get auth token: %w", err)
}
manifestPath, version := args[0], args[1]
manifestDir := filepath.Dir(manifestPath)
manifest, err := publish.ReadManifestJSON(manifestPath)
if err != nil {
return fmt.Errorf("failed to read manifest.json: %w", err)
}
if manifest.Type != "addon" {
return errors.New("manifest.json is not of addon type")
}
zipPath := filepath.Join(manifestDir, manifest.PathToZip)
if _, err := os.Stat(zipPath); err != nil {
return fmt.Errorf("could not read file %s: %w", zipPath, err)
}
name := fmt.Sprintf("%s/%s/%s@%s", manifest.TeamName, manifest.AddonType, manifest.AddonName, version)
fmt.Printf("Publishing addon %s to CloudQuery Hub...\n", name)
c, err := api.NewClient(token.Value)
if err != nil {
return err
}
// create new draft version
if err := publish.CreateNewAddonDraftVersion(ctx, c, manifest, version, manifestDir, zipPath); err != nil {
return fmt.Errorf("failed to create new draft version: %w", err)
}
// upload package
fmt.Println("Uploading addon...")
if err := publish.UploadAddon(ctx, c, manifest, version, zipPath); err != nil {
return fmt.Errorf("failed to upload addon: %w", err)
}
// optional: mark addon as draft=false
finalize, err := cmd.Flags().GetBool("finalize")
if err != nil {
return err
}
if finalize {
fmt.Println("Finalizing addon version...")
draft := false
resp, err := c.UpdateAddonVersionWithResponse(ctx, manifest.TeamName, cloudquery_api.AddonType(manifest.AddonType), manifest.AddonName, version, cloudquery_api.UpdateAddonVersionJSONRequestBody{
Draft: &draft,
})
if err != nil {
return fmt.Errorf("failed to finalize addon version: %w", err)
}
if resp.HTTPResponse.StatusCode > 299 {
return hub.ErrorFromHTTPResponse(resp.HTTPResponse, resp)
}
fmt.Println("Success!")
fmt.Printf("%s/%s/%s@%s is now available on the CloudQuery Hub.\n", manifest.TeamName, manifest.AddonType, manifest.AddonName, version)
return nil
}
fmt.Println("Success!")
fmt.Println("\nNote: this addon version is marked as draft=true. You can preview and finalize it on the CloudQuery Hub, or run `cloudquery addon publish` with the --finalize flag.")
return nil
}