MCPcopy Create free account
hub / github.com/cloudquery/cloudquery / runAddonDownload

Function runAddonDownload

cli/cmd/addon_download.go:61–124  ·  view source on GitHub ↗
(ctx context.Context, cmd *cobra.Command, args []string)

Source from the content-addressed store, hash-verified

59}
60
61func runAddonDownload(ctx context.Context, cmd *cobra.Command, args []string) error {
62 tc := cqapiauth.NewTokenClient()
63 token, err := tc.GetToken()
64 if err != nil {
65 return fmt.Errorf("failed to get auth token: %w", err)
66 }
67
68 addonParts := strings.Split(args[0], "/")
69 if len(addonParts) != 3 {
70 return fmt.Errorf("invalid addon ref: %s", args[0])
71 }
72 addonVer := strings.Split(addonParts[2], "@")
73 if len(addonVer) != 2 {
74 return fmt.Errorf("invalid addon ref %q: no version specified", args[0])
75 }
76 if !strings.HasPrefix(addonVer[1], "v") {
77 return fmt.Errorf("invalid addon ref %q: version must start with 'v'", args[0])
78 }
79
80 c, err := api.NewClient(token.Value)
81 if err != nil {
82 return err
83 }
84
85 targetDir, err := cmd.Flags().GetString("target")
86 if err != nil {
87 return err
88 }
89
90 currentTeam, err := auth.GetTeamForToken(ctx, token)
91 if err != nil {
92 return err
93 }
94
95 addonTeam := addonParts[0]
96 addonType := addonParts[1]
97 addonName := addonVer[0]
98 addonVersion := addonVer[1]
99 location, checksum, err := publish.GetAddonMetadata(ctx, c, currentTeam, addonTeam, addonType, addonName, addonVersion)
100 if err != nil {
101 return err
102 }
103
104 req, err := http.NewRequestWithContext(ctx, http.MethodGet, location, nil)
105 if err != nil {
106 return fmt.Errorf("failed to create request: %w", err)
107 }
108 // TODO: Remove this when https://github.com/timakin/bodyclose/issues/39 is fixed (false positive when close is in another package)
109 // nolint: bodyclose
110 res, err := http.DefaultClient.Do(req)
111 if err != nil {
112 return fmt.Errorf("failed to make download request: %w", err)
113 }
114 if res.StatusCode > 399 {
115 return fmt.Errorf("addon download failed: %d %s", res.StatusCode, location)
116 }
117
118 zipPath := "-"

Callers 1

newCmdAddonDownloadFunction · 0.85

Calls 1

ErrorfMethod · 0.80

Tested by

no test coverage detected