-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
52 lines (48 loc) · 1.07 KB
/
url.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
package aliyunsls
import (
"fmt"
"net/url"
"strings"
)
type URL struct {
Endpoint string
AccessKeyID string
AccessKeySecret string
Project string
LogStore string
}
func ParseURL(_url string) (*URL, error) {
u, err := url.Parse(_url)
if err != nil {
return nil, fmt.Errorf("invalid url of aliyunsls: %w", err)
}
if u.Scheme != "aliyun+sls" {
return nil, fmt.Errorf("unknonw scheme(%s) for aliyunsls", u.Scheme)
}
tmpArr := strings.SplitN(u.Path, "/", 3)
if len(tmpArr) != 3 {
return nil, fmt.Errorf("invalid path(%s), it must be /{project}/{logstore}", u.Path)
}
tmpArr = tmpArr[1:]
password, _ := u.User.Password()
rs := &URL{
Endpoint: u.Host,
AccessKeyID: u.User.Username(),
AccessKeySecret: password,
Project: tmpArr[0],
LogStore: tmpArr[1],
}
if err := rs.check(); err != nil {
return nil, err
}
return rs, nil
}
func (u *URL) check() error {
if u.Project == "" {
return fmt.Errorf("missing project setup")
}
if u.LogStore == "" {
return fmt.Errorf("missing logstore setup")
}
return nil
}