-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patherg.go
116 lines (101 loc) · 2.28 KB
/
erg.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package erg
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"github.com/fvbommel/sortorder"
"github.com/square/grange"
)
// Erg type
// Sort boolean - turn it off/on for sorting on expand
// default is true
type Erg struct {
host string
port int
ssl bool
Sort bool
client *http.Client
}
// New(address string) returns a new erg
// takes two arguments
// host - hostname default - localhost
// port - port default - 8080
// ssl - use https or not default - false
func New(host string, port int) *Erg {
return &Erg{
host: host,
port: port,
ssl: false,
Sort: true,
client: &http.Client{},
}
}
func NewWithSsl(host string, port int) *Erg {
e := New(host, port)
e.ssl = true
return e
}
func NewWithClient(client *http.Client, host string, port int, ssl bool) *Erg {
return &Erg{
host: host,
port: port,
ssl: ssl,
Sort: true,
client: client,
}
}
// Expand takes a range expression as argument
// and returns an slice of strings as result
// err is set to nil on success
func (e *Erg) Expand(query string) (result []string, err error) {
protocol := "http"
if e.ssl {
protocol = "https"
}
resp, err := e.client.Get(fmt.Sprintf("%s://%s:%d/range/list?%s",
protocol,
e.host,
e.port,
url.QueryEscape(query),
))
if err != nil {
return nil, err
}
// "When err is nil, resp always contains a non-nil resp.Body. Caller should
// close resp.Body when done reading from it." https://golang.org/pkg/net/http/#Client.Get
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil, readErr
}
return nil, errors.New(string(body))
}
scanner := bufio.NewScanner(resp.Body)
grangeResult := grange.NewResult()
for scanner.Scan() {
grangeResult.Add(scanner.Text())
}
if grangeResult.Cardinality() > 0 {
for node := range grangeResult.Iter() {
result = append(result, node.(string))
}
if e.Sort {
sort.Sort(sortorder.Natural(result))
}
}
return result, nil
}
// Compress takes a slice of strings as argument
// and returns a compressed form.
func (*Erg) Compress(nodes []string) (result string) {
grangeResult := grange.NewResult()
for _, node := range nodes {
grangeResult.Add(node)
}
return grange.Compress(&grangeResult)
}