~dricottone/parcels

ref: 7f176b28a6081856111480a58b9b1b6714ed33a0 parcels/main.go -rw-r--r-- 1.0 KiB
7f176b28Dominic Ricottone README update 2 years ago
                                                                                
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
package main

import (
	"fmt"
	"os"
	"io"
	"flag"

	"git.dominic-ricottone.com/~dricottone/parcels/common"
)

func find_in_stream(reader io.Reader, target int) {
	url, err := common.PullFromReader(reader, target, 0)
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}

	fmt.Println(url)
}

func parse_stream(reader io.Reader) {
	content, urls, err := common.ParseFromReader(reader, 0)
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("%s", content)
	fmt.Printf("%s", urls)
}

func parse_file(filename string) {
	// Check file
	file, err := os.Open(filename)
	if err != nil {
		fmt.Printf("cannot read file '%s'\n", filename)
		os.Exit(1)
	}
	defer file.Close()

	parse_stream(file)
}

func main() {
	// Check STDIN
	_, err := os.Stdin.Stat()
	if err != nil {
		fmt.Println("cannot read input")
		os.Exit(1)
	}

	// Look for arguments
	var index = flag.Int("n", -1, "print Nth URL")
	flag.Parse()

	// Parse
	if *index < 0 {
		parse_stream(os.Stdin)
	} else {
		find_in_stream(os.Stdin, *index)
	}
}