mark/auth.go

88 lines
1.4 KiB
Go
Raw Normal View History

2019-04-08 22:20:31 +03:00
package main
import (
"errors"
2023-01-03 18:54:04 +01:00
"io"
2019-04-08 22:20:31 +03:00
"net/url"
"os"
2019-04-08 22:20:31 +03:00
"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,
2019-04-08 22:20:31 +03:00
) (*Credentials, error) {
var err error
2019-04-08 22:20:31 +03:00
if password == "" {
if !compileOnly {
return nil, errors.New(
"confluence password should be specified using -p " +
"flag or be stored in configuration file",
)
2019-04-08 22:20:31 +03:00
}
password = "none"
2019-04-08 22:20:31 +03:00
}
if password == "-" {
2023-01-03 18:54:04 +01:00
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"
}
2019-04-08 22:20:31 +03:00
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",
)
2019-04-08 22:20:31 +03:00
}
} else {
baseURL = url.Scheme + "://" + url.Host
2019-04-08 22:20:31 +03:00
}
baseURL = strings.TrimRight(baseURL, `/`)
pageID := url.Query().Get("pageId")
creds := &Credentials{
Username: username,
Password: password,
BaseURL: baseURL,
PageID: pageID,
}
return creds, nil
}