From a886de3276b5b21e0583f1f122ff332c4ebe9c6f Mon Sep 17 00:00:00 2001 From: Hiromu OCHIAI Date: Thu, 13 May 2021 13:00:36 +0900 Subject: [PATCH] Good bye urfave/cli --- go.mod | 3 ++- go.sum | 13 ---------- ogp/main.go | 75 +++++++++++++++++++++++++---------------------------- 3 files changed, 38 insertions(+), 53 deletions(-) diff --git a/go.mod b/go.mod index 0ab949c..a943235 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/otiai10/opengraph/v2 require ( github.com/otiai10/marmoset v0.4.0 github.com/otiai10/mint v1.3.2 - github.com/urfave/cli v1.22.1 golang.org/x/net v0.0.0-20200923182212-328152dc79b1 ) + +go 1.13 diff --git a/go.sum b/go.sum index cd479a2..8da13f8 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,3 @@ -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/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ryit9KGfp3sUsW4Lqg32iRMJyzs= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI= @@ -11,14 +8,6 @@ github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E= github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -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 v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= -github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -28,5 +17,3 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/ogp/main.go b/ogp/main.go index 1ff97da..53fae28 100644 --- a/ogp/main.go +++ b/ogp/main.go @@ -2,55 +2,52 @@ package main import ( "encoding/json" + "flag" "fmt" "net/url" "os" "github.com/otiai10/opengraph/v2" - "github.com/urfave/cli" ) func main() { - app := cli.NewApp() - app.Version = "2.0.0" - app.Usage = "Fetch URL and extract OpenGraph meta informations." - app.UsageText = "ogp [-A] {URL}" - app.Flags = []cli.Flag{ - cli.BoolFlag{ - Name: "absolute,A", - Usage: "populate relative URLs to absolute URLs", - }, - } - app.Action = func(ctx *cli.Context) error { - rawurl := ctx.Args().First() - if rawurl == "" { - return fmt.Errorf("URL must be specified") - } - u, err := url.Parse(rawurl) - if err != nil { - return err - } - if u.Scheme == "" { - u.Scheme = "https" - } - og := opengraph.New(u.String()) - if err := og.Fetch(); err != nil { - return err - } - if ctx.Bool("absolute") { - if err := og.ToAbs(); err != nil { - return err - } - } - b, err := json.MarshalIndent(og, "", "\t") - if err != nil { - return err - } - fmt.Printf("%+v\n", string(b)) - return nil + flagset := flag.CommandLine + flagset.Usage = func() { + fmt.Println("Fetch URL and extract OpenGraph meta informations.") } - if err := app.Run(os.Args); err != nil { + abs := flagset.Bool("A", false, "populate relative URLs to absolute URLs") + flagset.Parse(os.Args[1:]) + if err := run(flagset.Args(), *abs); err != nil { fmt.Println("Error:", err.Error()) os.Exit(1) } } + +func run(args []string, absolute bool) error { + if len(args) == 0 { + return fmt.Errorf("URL must be specified") + } + rawurl := args[0] + u, err := url.Parse(rawurl) + if err != nil { + return err + } + if u.Scheme == "" { + u.Scheme = "https" + } + og := opengraph.New(u.String()) + if err := og.Fetch(); err != nil { + return err + } + if absolute { + if err := og.ToAbs(); err != nil { + return err + } + } + b, err := json.MarshalIndent(og, "", "\t") + if err != nil { + return err + } + fmt.Printf("%+v\n", string(b)) + return nil +}