mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-02 13:22:40 +00:00
Restructure code for more testaability
Move a number of funcs/files in the top-level `main` package into a new `util` package, so test logic can directly invoke functions like RunMark(), etc. The main.go has been trimmed down to minimal sizing, with former supporting funcs moved into `util` package, so they can be run by unit tests. Signed-off-by: Rich Scott <richscott@sent.com>
This commit is contained in:
87
util/auth.go
Normal file
87
util/auth.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/reconquest/karma-go"
|
||||
)
|
||||
|
||||
type Credentials struct {
|
||||
Username string
|
||||
Password string
|
||||
BaseURL string
|
||||
PageID string
|
||||
}
|
||||
|
||||
func GetCredentials(
|
||||
username string,
|
||||
password string,
|
||||
targetURL string,
|
||||
baseURL string,
|
||||
compileOnly bool,
|
||||
|
||||
) (*Credentials, error) {
|
||||
var err error
|
||||
|
||||
if password == "" {
|
||||
if !compileOnly {
|
||||
return nil, errors.New(
|
||||
"confluence password should be specified using -p " +
|
||||
"flag or be stored in configuration file",
|
||||
)
|
||||
}
|
||||
password = "none"
|
||||
}
|
||||
|
||||
if password == "-" {
|
||||
stdin, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
"unable to read password from stdin",
|
||||
)
|
||||
}
|
||||
|
||||
password = string(stdin)
|
||||
}
|
||||
|
||||
if compileOnly && targetURL == "" {
|
||||
targetURL = "http://localhost"
|
||||
}
|
||||
|
||||
url, err := url.Parse(targetURL)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
"unable to parse %q as url", targetURL,
|
||||
)
|
||||
}
|
||||
|
||||
if url.Host == "" {
|
||||
if baseURL == "" {
|
||||
return nil, errors.New(
|
||||
"confluence base URL should be specified using -l " +
|
||||
"flag or be stored in configuration file",
|
||||
)
|
||||
}
|
||||
} else {
|
||||
baseURL = url.Scheme + "://" + url.Host
|
||||
}
|
||||
|
||||
baseURL = strings.TrimRight(baseURL, `/`)
|
||||
|
||||
pageID := url.Query().Get("pageId")
|
||||
|
||||
creds := &Credentials{
|
||||
Username: username,
|
||||
Password: password,
|
||||
BaseURL: baseURL,
|
||||
PageID: pageID,
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
}
|
||||
Reference in New Issue
Block a user