|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/AxeForging/pipekit/services" |
| 10 | + |
| 11 | + "github.com/urfave/cli" |
| 12 | +) |
| 13 | + |
| 14 | +// CommentCommand returns the markdown comment command group. |
| 15 | +func CommentCommand() cli.Command { |
| 16 | + return cli.Command{ |
| 17 | + Name: "comment", |
| 18 | + Usage: "render, inspect, and amend anchored markdown comments", |
| 19 | + Subcommands: []cli.Command{ |
| 20 | + { |
| 21 | + Name: "anchor", |
| 22 | + Usage: "print a hidden pipekit anchor marker", |
| 23 | + Action: func(c *cli.Context) error { |
| 24 | + name, err := firstArgOrErr(c, "anchor name") |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + marker, err := services.AnchorMarker(name) |
| 29 | + if err != nil { |
| 30 | + return cli.NewExitError(err.Error(), 1) |
| 31 | + } |
| 32 | + fmt.Println(marker) |
| 33 | + return nil |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + Name: "fence", |
| 38 | + Usage: "render stdin or a file as a fenced markdown code block", |
| 39 | + Flags: []cli.Flag{ |
| 40 | + cli.StringFlag{Name: "language, l", Usage: "code fence language tag"}, |
| 41 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 42 | + }, |
| 43 | + Action: func(c *cli.Context) error { |
| 44 | + body, err := readInputFileOrStdin(c) |
| 45 | + if err != nil { |
| 46 | + return cli.NewExitError(err.Error(), 1) |
| 47 | + } |
| 48 | + return writeCommentOutput(c, services.RenderCodeFence(c.String("language"), string(body))) |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + Name: "render", |
| 53 | + Usage: "render a markdown comment body with a hidden anchor", |
| 54 | + Flags: []cli.Flag{ |
| 55 | + cli.StringFlag{Name: "anchor, a", Usage: "hidden anchor name", Required: true}, |
| 56 | + cli.StringFlag{Name: "body-file", Usage: "read visible markdown body from file"}, |
| 57 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 58 | + }, |
| 59 | + Action: func(c *cli.Context) error { |
| 60 | + body, err := readCommentBody(c) |
| 61 | + if err != nil { |
| 62 | + return cli.NewExitError(err.Error(), 1) |
| 63 | + } |
| 64 | + out, err := services.RenderAnchoredComment(c.String("anchor"), body) |
| 65 | + if err != nil { |
| 66 | + return cli.NewExitError(err.Error(), 1) |
| 67 | + } |
| 68 | + return writeCommentOutput(c, out) |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + Name: "payload", |
| 73 | + Usage: "render stdin or a file as a GitHub comment API payload", |
| 74 | + Flags: []cli.Flag{ |
| 75 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 76 | + }, |
| 77 | + Action: func(c *cli.Context) error { |
| 78 | + body, err := readInputFileOrStdin(c) |
| 79 | + if err != nil { |
| 80 | + return cli.NewExitError(err.Error(), 1) |
| 81 | + } |
| 82 | + out, err := services.GitHubCommentPayload(string(body)) |
| 83 | + if err != nil { |
| 84 | + return cli.NewExitError(err.Error(), 1) |
| 85 | + } |
| 86 | + return writeCommentOutput(c, out+"\n") |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + Name: "amend", |
| 91 | + Usage: "replace the visible body after a hidden anchor", |
| 92 | + Flags: []cli.Flag{ |
| 93 | + cli.StringFlag{Name: "anchor, a", Usage: "hidden anchor name", Required: true}, |
| 94 | + cli.StringFlag{Name: "body-file", Usage: "read replacement markdown body from file", Required: true}, |
| 95 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 96 | + }, |
| 97 | + Action: func(c *cli.Context) error { |
| 98 | + existing, err := readInputFileOrStdin(c) |
| 99 | + if err != nil { |
| 100 | + return cli.NewExitError(err.Error(), 1) |
| 101 | + } |
| 102 | + body, err := os.ReadFile(c.String("body-file")) |
| 103 | + if err != nil { |
| 104 | + return cli.NewExitError(fmt.Sprintf("reading body file: %v", err), 1) |
| 105 | + } |
| 106 | + out, err := services.AmendAnchoredComment(string(existing), c.String("anchor"), string(body)) |
| 107 | + if err != nil { |
| 108 | + return cli.NewExitError(err.Error(), 1) |
| 109 | + } |
| 110 | + return writeCommentOutput(c, out) |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + Name: "inspect", |
| 115 | + Usage: "inspect anchors and fenced blocks in markdown or GitHub comments JSON", |
| 116 | + Action: func(c *cli.Context) error { |
| 117 | + r, err := readerFromArgOrStdin(c) |
| 118 | + if err != nil { |
| 119 | + return cli.NewExitError(err.Error(), 1) |
| 120 | + } |
| 121 | + defer r.Close() |
| 122 | + comments, err := services.InspectComments(r) |
| 123 | + if err != nil { |
| 124 | + return cli.NewExitError(err.Error(), 1) |
| 125 | + } |
| 126 | + return encodeCommentJSON(comments) |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + Name: "select", |
| 131 | + Usage: "select the first GitHub comment JSON item containing an anchor", |
| 132 | + Flags: []cli.Flag{ |
| 133 | + cli.StringFlag{Name: "anchor, a", Usage: "hidden anchor name", Required: true}, |
| 134 | + cli.StringFlag{Name: "format, f", Value: "json", Usage: "output format: json, id, body, url"}, |
| 135 | + }, |
| 136 | + Action: func(c *cli.Context) error { |
| 137 | + r, err := readerFromArgOrStdin(c) |
| 138 | + if err != nil { |
| 139 | + return cli.NewExitError(err.Error(), 1) |
| 140 | + } |
| 141 | + defer r.Close() |
| 142 | + comments, err := services.InspectComments(r) |
| 143 | + if err != nil { |
| 144 | + return cli.NewExitError(err.Error(), 1) |
| 145 | + } |
| 146 | + comment, ok := services.SelectAnchoredComment(comments, c.String("anchor")) |
| 147 | + if !ok { |
| 148 | + return cli.NewExitError("no matching anchored comment found", 1) |
| 149 | + } |
| 150 | + switch c.String("format") { |
| 151 | + case "json", "": |
| 152 | + return encodeCommentJSON(comment) |
| 153 | + case "id": |
| 154 | + fmt.Println(comment.ID) |
| 155 | + case "body": |
| 156 | + fmt.Print(comment.Body) |
| 157 | + case "url": |
| 158 | + fmt.Println(comment.URL) |
| 159 | + default: |
| 160 | + return cli.NewExitError("unsupported format: use json, id, body, or url", 1) |
| 161 | + } |
| 162 | + return nil |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func readCommentBody(c *cli.Context) (string, error) { |
| 170 | + if path := c.String("body-file"); path != "" { |
| 171 | + data, err := os.ReadFile(path) |
| 172 | + if err != nil { |
| 173 | + return "", fmt.Errorf("reading body file: %w", err) |
| 174 | + } |
| 175 | + return string(data), nil |
| 176 | + } |
| 177 | + data, err := readBytesFromArgOrStdin(c) |
| 178 | + if err != nil { |
| 179 | + return "", err |
| 180 | + } |
| 181 | + return string(data), nil |
| 182 | +} |
| 183 | + |
| 184 | +func readInputFileOrStdin(c *cli.Context) ([]byte, error) { |
| 185 | + r, err := readerFromArgOrStdin(c) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + defer r.Close() |
| 190 | + return io.ReadAll(r) |
| 191 | +} |
| 192 | + |
| 193 | +func writeCommentOutput(c *cli.Context, content string) error { |
| 194 | + if path := c.String("output"); path != "" { |
| 195 | + return os.WriteFile(path, []byte(content), 0644) |
| 196 | + } |
| 197 | + fmt.Print(content) |
| 198 | + return nil |
| 199 | +} |
| 200 | + |
| 201 | +func encodeCommentJSON(v interface{}) error { |
| 202 | + data, err := json.Marshal(v) |
| 203 | + if err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + fmt.Println(string(data)) |
| 207 | + return nil |
| 208 | +} |
0 commit comments