diff --git a/go-paste.go b/go-paste.go index af328c4..7055aae 100644 --- a/go-paste.go +++ b/go-paste.go @@ -2,11 +2,14 @@ package main import ( "errors" - "github.com/bearbin/go-paste/fpaste" - "github.com/bearbin/go-paste/pastebin" - "github.com/codegangsta/cli" + "fmt" "io/ioutil" "os" + + "github.com/urfave/cli/v2" + + "github.com/bearbin/go-paste/fpaste" + "github.com/bearbin/go-paste/pastebin" ) var errUnknownService = errors.New("unknown paste service") @@ -15,54 +18,57 @@ func main() { app := cli.NewApp() app.Usage = "get and put pastes from pastebin and other paste sites." app.Flags = []cli.Flag{ - cli.StringFlag{Name: "service, s", Value: "pastebin", Usage: "the pastebin service to use"}, + &cli.StringFlag{Name: "service, s", Value: "pastebin", Usage: "the pastebin service to use"}, } - app.Commands = []cli.Command{ + app.Commands = []*cli.Command{ { - Name: "put", - ShortName: "p", - Usage: "put a paste", + Name: "put", + Usage: "put a paste", + Aliases: []string{"p"}, Flags: []cli.Flag{ - cli.BoolFlag{Name: "id", Usage: "return the paste id not the url"}, - cli.StringFlag{Name: "title, t", Value: "", Usage: "the title for the paste"}, + &cli.BoolFlag{Name: "id", Usage: "return the paste id not the url"}, + &cli.StringFlag{Name: "title, t", Value: "", Usage: "the title for the paste"}, }, - Action: func(c *cli.Context) { - srv, err := convertService(c.GlobalString("service")) + Action: func(c *cli.Context) error { + srv, err := convertService(c.String("service")) if err != nil { - println("ERROR:", err.Error()) - os.Exit(1) + return err } + var text []byte if c.Args().First() == "-" || c.Args().First() == "" { text, err = ioutil.ReadAll(os.Stdin) } else { text, err = ioutil.ReadFile(c.Args().First()) } + if err != nil { - println("ERROR:", err.Error()) - os.Exit(1) + return err } + code, err := srv.Put(string(text), c.String("title")) if err != nil { - println("ERROR:", err.Error()) - os.Exit(1) + return err } - if c.Bool("id") { - println(code) - } else { - println(srv.WrapID(code)) + + if !c.Bool("id") { + code = srv.WrapID(code) } + + _, _ = fmt.Fprintln(app.Writer, code) + + return nil }, }, { - Name: "get", - ShortName: "g", - Usage: "get a paste from its url", + Name: "get", + Usage: "get a paste from its url", + Aliases: []string{"g"}, Flags: []cli.Flag{ - cli.BoolFlag{Name: "id", Usage: "get a paste from its ID instead of its URL"}, + &cli.BoolFlag{Name: "id", Usage: "get a paste from its ID instead of its URL"}, }, - Action: func(c *cli.Context) { - srv, err := convertService(c.GlobalString("service")) + Action: func(c *cli.Context) error { + srv, err := convertService(c.String("service")) if err != nil { println("ERROR:", err.Error()) os.Exit(1) @@ -75,14 +81,21 @@ func main() { } text, err := srv.Get(id) if err != nil { - println("ERROR:", err.Error()) - os.Exit(1) + return err } - println(text) + + _, _ = fmt.Fprintln(app.Writer, text) + + return nil }, }, } - app.Run(os.Args) + + if err := app.Run(os.Args); err != nil { + _, _ = fmt.Fprintf(app.ErrWriter, "%v\n", err) + + os.Exit(1) + } } func convertService(srv string) (service, error) { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..435cb9b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/bearbin/go-paste + +go 1.15 + +require github.com/urfave/cli/v2 v2.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f8207d7 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=