/
config.go
60 lines (50 loc) · 1.36 KB
/
config.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
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package client
import (
"fmt"
"net/url"
"regexp"
"strings"
)
var idRE = regexp.MustCompile(`^[a-z0-9]{26}$`)
type Config struct {
// SiteURL is the URL of the Mattermost installation to connect to.
SiteURL string
// AuthToken is a valid user session authentication token.
AuthToken string
// ChannelID is the id of the channel to start or join a call in.
ChannelID string
// JobID is an id used to identify bot initiated sessions (e.g.
// recording/transcription)
JobID string
wsURL string
}
func (c *Config) Parse() error {
if c.SiteURL == "" {
return fmt.Errorf("invalid SiteURL value: should not be empty")
}
c.SiteURL = strings.TrimRight(strings.TrimSpace(c.SiteURL), "/")
u, err := url.Parse(c.SiteURL)
if err != nil {
return fmt.Errorf("failed to parse SiteURL: %w", err)
}
if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf("invalid SiteURL scheme %q", u.Scheme)
}
if u.Scheme == "http" {
u.Scheme = "ws"
u.Path += mmWebSocketAPIPath
} else {
u.Scheme = "wss"
u.Path += mmWebSocketAPIPath
}
c.wsURL = u.String()
if c.AuthToken == "" {
return fmt.Errorf("invalid AuthToken value: should not be empty")
}
if !idRE.MatchString(c.ChannelID) {
return fmt.Errorf("invalid ChannelID value")
}
return nil
}