| 57 | } |
| 58 | |
| 59 | func runPluginDocsDownload(ctx context.Context, cmd *cobra.Command, args []string) error { |
| 60 | tc := auth.NewTokenClient() |
| 61 | token, err := tc.GetToken() |
| 62 | if err != nil { |
| 63 | return fmt.Errorf("failed to get auth token: %w", err) |
| 64 | } |
| 65 | |
| 66 | pluginRef, err := hub.ParseHubPluginRef(args[0]) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | docsDir := cmd.Flag("docs-dir").Value.String() |
| 72 | st, err := os.Stat(docsDir) |
| 73 | if err != nil { |
| 74 | if !os.IsNotExist(err) { |
| 75 | return err |
| 76 | } |
| 77 | if err := os.MkdirAll(docsDir, 0755); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | } else if !st.IsDir() { |
| 81 | return fmt.Errorf("%s is not a directory", docsDir) |
| 82 | } |
| 83 | |
| 84 | fmt.Printf("Downloading documentation for plugin %v...\n", pluginRef) |
| 85 | |
| 86 | c, err := api.NewClient(token.Value) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | pg, ppg := cloudquery_api.Page(1), cloudquery_api.PerPage(1000) |
| 92 | resp, err := c.ListPluginVersionDocsWithResponse(ctx, pluginRef.TeamName, cloudquery_api.PluginKind(pluginRef.Kind), pluginRef.Name, pluginRef.Version, &cloudquery_api.ListPluginVersionDocsParams{ |
| 93 | Page: &pg, |
| 94 | PerPage: &ppg, |
| 95 | }) |
| 96 | if err != nil { |
| 97 | return fmt.Errorf("failed to read docs: %w", err) |
| 98 | } |
| 99 | if resp.StatusCode() != http.StatusOK { |
| 100 | return hub.ErrorFromHTTPResponse(resp.HTTPResponse, resp) |
| 101 | } |
| 102 | if resp.JSON200 == nil { |
| 103 | return errors.New("failed to read docs: nil response") |
| 104 | } |
| 105 | for _, item := range resp.JSON200.Items { |
| 106 | safeName := strings.ReplaceAll(item.Name, string(filepath.Separator), "_") + ".md" |
| 107 | fmt.Print(" ", safeName, " ") |
| 108 | fn := filepath.Join(docsDir, safeName) |
| 109 | fp, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | nb, err := fp.WriteString(item.Content) |
| 114 | if err != nil { |
| 115 | _ = os.Remove(fn) |
| 116 | return err |