@@ -4,10 +4,11 @@ import (
44 "fmt"
55 "os"
66 "os/signal"
7+ "regexp"
78 "strings"
89 "syscall"
910
10- path "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path"
11+ ipath "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path"
1112 fallback "gx/ipfs/QmaWDhoQaV6cDyy6NSKFgPaUAGRtb4SMiLpaDYEsxP7X8P/fallback-ipfs-shell"
1213 cli "gx/ipfs/Qmc1AtgBdoUHP8oYSqU81NRYdzohmF45t5XNwVMvhCxsBA/cli"
1314)
@@ -36,21 +37,19 @@ func main() {
3637 }
3738
3839 outfile := c .String ("output" )
39- arg := c .Args ().First ()
40+ inPath , err := parsePath (c .Args ().First ())
41+ if err != nil {
42+ fmt .Fprintf (os .Stderr , "Argument parse failure: %s\n " , err )
43+ os .Exit (1 )
44+ }
4045
4146 // Use the final segment of the object's path if no path was given.
4247 if outfile == "" {
43- ipfsPath , err := path .ParsePath (arg )
44- if err != nil {
45- fmt .Fprintf (os .Stderr , "ParsePath failure: %s\n " , err )
46- os .Exit (1 )
47- }
48- segments := ipfsPath .Segments ()
48+ segments := inPath .Segments ()
4949 outfile = segments [len (segments )- 1 ]
5050 }
5151
5252 var shell fallback.Shell
53- var err error
5453
5554 if c .String ("node" ) == "fallback" {
5655 shell , err = fallback .NewShell ()
@@ -76,7 +75,7 @@ func main() {
7675 return nil
7776 }
7877
79- if err := shell .Get (arg , outfile ); err != nil {
78+ if err := shell .Get (inPath . String () , outfile ); err != nil {
8079 os .Remove (outfile )
8180 fmt .Fprintf (os .Stderr , "ipget failed: %s\n " , err )
8281 os .Exit (2 )
@@ -132,3 +131,17 @@ func movePostfixOptions(args []string) []string {
132131 // append extracted arguments to the real args
133132 return append (args , the_args ... )
134133}
134+
135+ func parsePath (path string ) (ipath.Path , error ) {
136+ ipfsPath , err := ipath .ParsePath (path )
137+ if err == nil { // valid canonical path
138+ return ipfsPath , nil
139+ }
140+ // allow HTTP gateway URLs as input as well
141+ matches := regexp .MustCompile (`^https?://.*(/(?:ipfs|ipns|ipld)/.+)$` ).FindStringSubmatch (path )
142+ if len (matches ) < 1 {
143+ return "" , fmt .Errorf ("%q does not appear to be a valid IPFS path or HTTP gateway URL" , path )
144+ }
145+
146+ return ipath .ParsePath (matches [1 ])
147+ }
0 commit comments