-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
100 lines (83 loc) · 1.67 KB
/
auth.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
package main
import (
"errors"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/reconquest/karma-go"
)
type Credentials struct {
Username string
Password string
BaseURL string
PageID string
}
func GetCredentials(
flags Flags,
config *Config,
) (*Credentials, error) {
var err error
var (
username = flags.Username
password = flags.Password
targetURL = flags.TargetURL
)
if username == "" {
username = config.Username
if username == "" {
return nil, errors.New(
"Confluence username should be specified using -u " +
"flag or be stored in configuration file",
)
}
}
if password == "" {
password = config.Password
if password == "" {
return nil, errors.New(
"Confluence password should be specified using -p " +
"flag or be stored in configuration file",
)
}
}
if password == "-" {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, karma.Format(
err,
"unable to read password from stdin",
)
}
password = string(stdin)
}
url, err := url.Parse(targetURL)
if err != nil {
return nil, karma.Format(
err,
"unable to parse %q as url", targetURL,
)
}
baseURL := url.Scheme + "://" + url.Host
if url.Host == "" {
baseURL = flags.BaseURL
if baseURL == "" {
baseURL = config.BaseURL
}
if baseURL == "" {
return nil, errors.New(
"Confluence base URL should be specified using -l " +
"flag or be stored in configuration file",
)
}
}
baseURL = strings.TrimRight(baseURL, `/`)
pageID := url.Query().Get("pageId")
creds := &Credentials{
Username: username,
Password: password,
BaseURL: baseURL,
PageID: pageID,
}
return creds, nil
}