File: main.go

package info (click to toggle)
golang-gopkg-xmlpath.v2 0.0~git20150820.0.860cbec-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 116 kB
  • ctags: 92
  • sloc: makefile: 3
file content (88 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download | duplicates (3)
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
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"flag"
	"fmt"
	"gopkg.in/xmlpath.v2"
	"io"
	"net/http"
	"os"
	"regexp"
	"strings"
)

var all = flag.Bool("all", false, "print all occurrences rather than the first one")
var trim = flag.Bool("trim", false, "trim spaces around results")
var line = flag.Bool("line", false, "reformat each match as a single line")
var quiet = flag.Bool("q", false, "run quietly with no stdout output")

func main() {
	flag.Parse()
	if len(flag.Args()) != 2 {
		fmt.Fprintf(os.Stderr, "usage: webpath <xpath> <url>\n")
		os.Exit(1)
	}

	if err := run(); err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}
}

var whitespace = regexp.MustCompile("[ \t\n]+")

func run() error {
	args := flag.Args()

	path, err := xmlpath.Compile(args[0])
	if err != nil {
		return err
	}

	loc := args[1]

	var body io.Reader
	if strings.HasPrefix(loc, "https:") || strings.HasPrefix(loc, "http:") {
		resp, err := http.Get(args[1])
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		body = resp.Body
	} else {
		file, err := os.Open(loc)
		if err != nil {
			return err
		}
		defer file.Close()
		body = file
	}

	n, err := xmlpath.ParseHTML(body)
	if err != nil {
		return err
	}

	iter := path.Iter(n)
	ok := false
	for iter.Next() {
		ok = true
		if *quiet {
			break
		}
		s := iter.Node().String()
		if *line {
			s = strings.TrimSpace(whitespace.ReplaceAllString(s, " "))
		} else if *trim {
			s = strings.TrimSpace(s)
		}
		fmt.Println(s)
		if !*all {
			break
		}
	}
	if !ok {
		os.Exit(1)
	}
	return nil
}