-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionstring.go
74 lines (61 loc) · 1.58 KB
/
connectionstring.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
/*
* Copyright (c) 2022, Gideon Williams gideon@gideonw.com
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package proto
import (
"fmt"
"strings"
)
var Protocol = "fossil"
type ConnectionString struct {
Local bool
Address string
Database string
}
// ParseConnectionString takes a connection string and parses it into the parts
// the application needs to make a connection. This function will always parse,
// even horribly malformed connection strings. It will only panic if a protocol
// is specified and it is != 'fossil'.
// Format:
//
// [fossil://]<host:port|local>[/<db_name>]
func ParseConnectionString(connStr string) ConnectionString {
ret := ConnectionString{
Local: true,
Address: "local",
Database: "default",
}
// if there is no connStr, use local and default
if len(connStr) == 0 {
return ret
}
protoSep := strings.Index(connStr, "://")
if protoSep != -1 {
if connStr[:protoSep] != Protocol {
panic(fmt.Sprintf("Unsupported protocol '%s'. ", connStr[:protoSep]))
}
}
// Remove the optional protocol prefix
connStr = strings.TrimPrefix(connStr, Protocol+"://")
// strip ending slash before assigning values
connStr = strings.TrimSuffix(connStr, "/")
// if there is no connStr, after removing all parts, use local and default
if len(connStr) == 0 {
return ret
}
// then search for path separator
delim := strings.Index(connStr, "/")
if delim == -1 {
ret.Address = connStr
ret.Database = "default"
} else {
ret.Address = connStr[:delim]
ret.Database = connStr[delim+1:]
}
if ret.Address != "local" {
ret.Local = false
}
return ret
}