feat(cmd/pastes): add support for paste ID as URL

This commit is contained in:
jolts 2025-02-08 11:01:51 +02:00
parent dc4cd2b177
commit 19db447cbc
Signed by untrusted user who does not match committer: xlarp
GPG key ID: 173C05E221B5DB30
2 changed files with 19 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
@ -64,6 +65,19 @@ func GetPaste(c *cli.Context) error {
pasteID := c.Args().First() pasteID := c.Args().First()
rawOutput := c.Bool("raw") rawOutput := c.Bool("raw")
urlFlag := c.Bool("url")
if urlFlag {
parsedURL, err := url.Parse(pasteID)
if err != nil {
return fmt.Errorf("invalid URL: %v", err)
}
parts := strings.Split(strings.Trim(parsedURL.Path, "/"), "/")
if len(parts) == 0 || parts[0] == "" {
return fmt.Errorf("failed to extract paste id from URL")
}
pasteID = parts[0]
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/pastes/%s", API_BASE_URL, pasteID), nil) req, err := http.NewRequest("GET", fmt.Sprintf("%s/pastes/%s", API_BASE_URL, pasteID), nil)
if err != nil { if err != nil {

View file

@ -39,6 +39,11 @@ func main() {
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "Display raw content only", Usage: "Display raw content only",
}, },
&cli.BoolFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "Is the paste_id a URL",
},
}, },
}, },
}, },