|
| 1 | +// Copyright (c) 2026 Probo Inc <hello@getprobo.com>. |
| 2 | +// |
| 3 | +// Permission to use, copy, modify, and/or distribute this software for any |
| 4 | +// purpose with or without fee is hereby granted, provided that the above |
| 5 | +// copyright notice and this permission notice appear in all copies. |
| 6 | +// |
| 7 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 8 | +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 9 | +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 10 | +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 11 | +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 12 | +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 13 | +// PERFORMANCE OF THIS SOFTWARE. |
| 14 | + |
| 15 | +package updatecontent |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "go.probo.inc/probo/pkg/cli/api" |
| 24 | + "go.probo.inc/probo/pkg/cmd/cmdutil" |
| 25 | + "go.probo.inc/probo/pkg/prosemirror" |
| 26 | +) |
| 27 | + |
| 28 | +const updateContentMutation = ` |
| 29 | +mutation($input: UpdateDocumentVersionContentInput!) { |
| 30 | + updateDocumentVersionContent(input: $input) { |
| 31 | + content |
| 32 | + } |
| 33 | +} |
| 34 | +` |
| 35 | + |
| 36 | +type updateContentResponse struct { |
| 37 | + UpdateDocumentVersionContent struct { |
| 38 | + Content string `json:"content"` |
| 39 | + } `json:"updateDocumentVersionContent"` |
| 40 | +} |
| 41 | + |
| 42 | +func NewCmdUpdateContent(f *cmdutil.Factory) *cobra.Command { |
| 43 | + var ( |
| 44 | + flagID string |
| 45 | + flagContent string |
| 46 | + flagFromMarkdown string |
| 47 | + ) |
| 48 | + |
| 49 | + cmd := &cobra.Command{ |
| 50 | + Use: "update-content", |
| 51 | + Short: "Update document version content", |
| 52 | + Example: ` # Update with ProseMirror JSON |
| 53 | + prb document-version update-content --id <version-id> --content '{"type":"doc",...}' |
| 54 | +
|
| 55 | + # Update from markdown |
| 56 | + prb document-version update-content --id <version-id> --from-markdown "# Hello" |
| 57 | +
|
| 58 | + # Update from stdin |
| 59 | + cat content.json | prb document-version update-content --id <version-id>`, |
| 60 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 61 | + cfg, err := f.Config() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + host, hc, err := cfg.DefaultHost() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + client := api.NewClient( |
| 72 | + host, |
| 73 | + hc.Token, |
| 74 | + "/api/console/v1/graphql", |
| 75 | + cfg.HTTPTimeoutDuration(), |
| 76 | + ) |
| 77 | + |
| 78 | + var content string |
| 79 | + switch { |
| 80 | + case flagFromMarkdown != "": |
| 81 | + doc, err := prosemirror.ParseMarkdown(flagFromMarkdown) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + out, err := json.Marshal(doc) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("cannot marshal prosemirror document: %w", err) |
| 88 | + } |
| 89 | + content = string(out) |
| 90 | + case flagContent != "": |
| 91 | + content = flagContent |
| 92 | + default: |
| 93 | + data, err := io.ReadAll(f.IOStreams.In) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("cannot read from stdin: %w", err) |
| 96 | + } |
| 97 | + content = string(data) |
| 98 | + } |
| 99 | + |
| 100 | + input := map[string]any{ |
| 101 | + "id": flagID, |
| 102 | + "content": content, |
| 103 | + } |
| 104 | + |
| 105 | + data, err := client.Do( |
| 106 | + updateContentMutation, |
| 107 | + map[string]any{"input": input}, |
| 108 | + ) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + var resp updateContentResponse |
| 114 | + if err := json.Unmarshal(data, &resp); err != nil { |
| 115 | + return fmt.Errorf("cannot parse response: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + _, _ = fmt.Fprintf( |
| 119 | + f.IOStreams.Out, |
| 120 | + "Updated document version content %s\n", |
| 121 | + flagID, |
| 122 | + ) |
| 123 | + |
| 124 | + return nil |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + cmd.Flags().StringVar(&flagID, "id", "", "Document version ID (required)") |
| 129 | + cmd.Flags().StringVar(&flagContent, "content", "", "ProseMirror JSON content") |
| 130 | + cmd.Flags().StringVar( |
| 131 | + &flagFromMarkdown, |
| 132 | + "from-markdown", |
| 133 | + "", |
| 134 | + "Markdown content to convert and upload", |
| 135 | + ) |
| 136 | + |
| 137 | + _ = cmd.MarkFlagRequired("id") |
| 138 | + |
| 139 | + return cmd |
| 140 | +} |
0 commit comments