-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcmd.go
77 lines (69 loc) · 1.75 KB
/
cmd.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"fmt"
"os"
"github.com/fern4lvarez/go-metainspector/metainspector"
)
// exit prints a message and exit the current program
func exit(msg string) {
fmt.Println(msg)
os.Exit(1)
}
// defaultURL returns "www.example.com"
// if there's no arguments
// or help is required
// and returns the first argument if exists
func defaultURL() string {
if len(os.Args) > 1 {
if a := os.Args[1]; a == "--h" {
return "www.example.com"
}
return os.Args[1]
}
return "www.example.com"
}
// sprintIfExists prints a pretty line if value exists
func sprintIfExists(n string, v string) {
if v != "" {
fmt.Printf("----> %s: %s\n", n, v)
}
}
// aprintIfExists prints pretty lines if the slice
// is not empty
func aprintIfExists(n string, v []string, a bool) {
if len(v) != 0 {
fmt.Printf("----> %s: ", n)
for i := range v {
if !a && i == 10 {
fmt.Printf("...")
break
}
fmt.Printf("%s ", v[i])
}
fmt.Printf("\n")
}
}
// prettyspector prints nicely all results
func prettyspector(mi *metainspector.MetaInspector, a bool) {
sprintIfExists("Title", mi.Title())
sprintIfExists("Author", mi.Author())
sprintIfExists("Description", mi.Description())
sprintIfExists("Generator", mi.Generator())
sprintIfExists("Charset", mi.Charset())
sprintIfExists("Language", mi.Language())
sprintIfExists("Feed URL", mi.Feed())
aprintIfExists("Keywords", mi.Keywords(), a)
aprintIfExists("Links", mi.Links(), a)
aprintIfExists("Images", mi.Images(), a)
}
func main() {
var url = flag.String("u", defaultURL(), "URL to metainspect.")
var all = flag.Bool("all", false, "Show full results.")
flag.Parse()
mi, err := metainspector.New(*url)
if err != nil {
exit("Something went wrong. Please, try again.")
}
prettyspector(mi, *all)
}