Skip to content

Commit

Permalink
Good bye urfave/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed May 13, 2021
1 parent a6e2cce commit a886de3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 53 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand All @@ -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=
75 changes: 36 additions & 39 deletions ogp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a886de3

Please sign in to comment.