feat(cmd/pastes): add support for paste ID as URL
This commit is contained in:
parent
dc4cd2b177
commit
19db447cbc
2 changed files with 19 additions and 0 deletions
|
@ -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 {
|
||||||
|
|
5
main.go
5
main.go
|
@ -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",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue