-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd.go
73 lines (63 loc) · 1.38 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
//Package cognibot Copyright 2016 Cognifly and Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cognibot
import (
"net/url"
"github.com/cognifly/cognilog"
)
// Cmder interface defines the methods required by the Fetch to request
// a resource.
type Cmder interface {
URL() *url.URL
Method() string
}
// Cmd defines a basic Command implementation.
type Cmd struct {
U *url.URL
M string
}
// URL returns the resource targeted by this command.
func (c *Cmd) URL() *url.URL {
return c.U
}
// Method returns the HTTP verb to use to process this command (i.e. "GET", "HEAD", etc.).
func (c *Cmd) Method() string {
return c.M
}
// NewCmd returns an initialized Cmd
func NewCmd(s string) *Cmd {
url, err := url.Parse(s)
if err != nil {
cognilog.Log("red", err)
}
return &Cmd{
U: url,
M: "GET",
}
}
// parseCmd returns cmd by resolving a url
func parseCmd(s string, lnk *url.URL) (*Cmd, error) {
href, err := url.Parse(s)
if err != nil {
cognilog.Log("red", err)
return nil, err
}
url := lnk.ResolveReference(href)
return &Cmd{
U: url,
M: "GET",
}, nil
}
// BotCmd returns an initialized robot Cmd
func BotCmd(s string) *Cmd {
lnk, err := url.Parse(s)
if err != nil {
cognilog.Log("red", err)
}
rob := lnk.ResolveReference(robotPath)
return &Cmd{
U: rob,
M: "GET",
}
}