forked from otiai10/opengraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (43 loc) · 898 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/otiai10/opengraph"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Version = "1.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")
}
og, err := opengraph.Fetch(rawurl)
if err != nil {
return err
}
if ctx.Bool("absolute") {
og = og.ToAbsURL()
}
b, err := json.MarshalIndent(og, "", "\t")
if err != nil {
return err
}
fmt.Printf("%+v\n", string(b))
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}
}