mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
a bit of refactoring
This commit is contained in:
parent
7fe4542c0a
commit
e27dd44bfa
106
auth.go
Normal file
106
auth.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/reconquest/karma-go"
|
||||||
|
"github.com/zazab/zhash"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Credentials struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
BaseURL string
|
||||||
|
PageID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCredentials(
|
||||||
|
args map[string]interface{},
|
||||||
|
config zhash.Hash,
|
||||||
|
) (*Credentials, error) {
|
||||||
|
var (
|
||||||
|
username, _ = args["-u"].(string)
|
||||||
|
password, _ = args["-p"].(string)
|
||||||
|
targetURL, _ = args["-l"].(string)
|
||||||
|
)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if username == "" {
|
||||||
|
username, err = config.GetString("username")
|
||||||
|
if err != nil {
|
||||||
|
if zhash.IsNotFound(err) {
|
||||||
|
return nil, errors.New(
|
||||||
|
"Confluence username should be specified using -u " +
|
||||||
|
"flag or be stored in configuration file",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"can't read username configuration variable: %s", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if password == "" {
|
||||||
|
password, err = config.GetString("password")
|
||||||
|
if err != nil {
|
||||||
|
if zhash.IsNotFound(err) {
|
||||||
|
return nil, errors.New(
|
||||||
|
"Confluence password should be specified using -p " +
|
||||||
|
"flag or be stored in configuration file",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"can't read password configuration variable: %s", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 == "" {
|
||||||
|
var ok bool
|
||||||
|
baseURL, ok = args["--base-url"].(string)
|
||||||
|
if !ok {
|
||||||
|
baseURL, err = config.GetString("base_url")
|
||||||
|
if err != nil {
|
||||||
|
if zhash.IsNotFound(err) {
|
||||||
|
return nil, errors.New(
|
||||||
|
"Confluence base URL should be specified using -l " +
|
||||||
|
"flag or be stored in configuration file",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"can't read base_url configuration variable: %s", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
baseURL = strings.TrimRight(baseURL, `/`)
|
||||||
|
|
||||||
|
pageID := url.Query().Get("pageId")
|
||||||
|
|
||||||
|
creds := &Credentials{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
|
BaseURL: baseURL,
|
||||||
|
PageID: pageID,
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds, nil
|
||||||
|
}
|
82
main.go
82
main.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -134,9 +133,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
username, _ = args["-u"].(string)
|
|
||||||
password, _ = args["-p"].(string)
|
|
||||||
targetURL, _ = args["-l"].(string)
|
|
||||||
targetFile, _ = args["-f"].(string)
|
targetFile, _ = args["-f"].(string)
|
||||||
dryRun = args["--dry-run"].(bool)
|
dryRun = args["--dry-run"].(bool)
|
||||||
editLock = args["-k"].(bool)
|
editLock = args["-k"].(bool)
|
||||||
@ -167,81 +163,23 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if username == "" {
|
creds, err := GetCredentials(args, config)
|
||||||
username, err = config.GetString("username")
|
|
||||||
if err != nil {
|
|
||||||
if zhash.IsNotFound(err) {
|
|
||||||
logger.Fatal(
|
|
||||||
"Confluence username should be specified using -u " +
|
|
||||||
"flag or be stored in configuration file",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Fatalf(
|
|
||||||
"can't read username configuration variable: %s", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if password == "" {
|
|
||||||
password, err = config.GetString("password")
|
|
||||||
if err != nil {
|
|
||||||
if zhash.IsNotFound(err) {
|
|
||||||
logger.Fatal(
|
|
||||||
"Confluence password should be specified using -p " +
|
|
||||||
"flag or be stored in configuration file",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Fatalf(
|
|
||||||
"can't read password configuration variable: %s", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
url, err := url.Parse(targetURL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
baseURL := url.Scheme + "://" + url.Host
|
api := NewAPI(creds.BaseURL, creds.Username, creds.Password)
|
||||||
|
|
||||||
if url.Host == "" {
|
if creds.PageID != "" && meta != nil {
|
||||||
var ok bool
|
|
||||||
baseURL, ok = args["--base-url"].(string)
|
|
||||||
if !ok {
|
|
||||||
baseURL, err = config.GetString("base_url")
|
|
||||||
if err != nil {
|
|
||||||
if zhash.IsNotFound(err) {
|
|
||||||
logger.Fatal(
|
|
||||||
"Confluence base URL should be specified using -l " +
|
|
||||||
"flag or be stored in configuration file",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Fatalf(
|
|
||||||
"can't read base_url configuration variable: %s", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
baseURL = strings.TrimRight(baseURL, `/`)
|
|
||||||
|
|
||||||
api := NewAPI(baseURL, username, password)
|
|
||||||
|
|
||||||
pageID := url.Query().Get("pageId")
|
|
||||||
|
|
||||||
if pageID != "" && meta != nil {
|
|
||||||
logger.Warningf(
|
logger.Warningf(
|
||||||
`specified file contains metadata, ` +
|
`specified file contains metadata, ` +
|
||||||
`but it will be ignore due specified command line URL`,
|
`but it will be ignored due specified command line URL`,
|
||||||
)
|
)
|
||||||
|
|
||||||
meta = nil
|
meta = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if pageID == "" && meta == nil {
|
if creds.PageID == "" && meta == nil {
|
||||||
logger.Fatalf(
|
logger.Fatalf(
|
||||||
`specified file doesn't contain metadata ` +
|
`specified file doesn't contain metadata ` +
|
||||||
`and URL is not specified via command line ` +
|
`and URL is not specified via command line ` +
|
||||||
@ -259,11 +197,11 @@ func main() {
|
|||||||
|
|
||||||
target = page
|
target = page
|
||||||
} else {
|
} else {
|
||||||
if pageID == "" {
|
if creds.PageID == "" {
|
||||||
logger.Fatalf("URL should provide 'pageId' GET-parameter")
|
logger.Fatalf("URL should provide 'pageId' GET-parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
page, err := api.getPageByID(pageID)
|
page, err := api.getPageByID(creds.PageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -283,11 +221,11 @@ func main() {
|
|||||||
logger.Infof(
|
logger.Infof(
|
||||||
`edit locked on page '%s' by user '%s' to prevent manual edits`,
|
`edit locked on page '%s' by user '%s' to prevent manual edits`,
|
||||||
target.Title,
|
target.Title,
|
||||||
username,
|
creds.Username,
|
||||||
)
|
)
|
||||||
|
|
||||||
err := api.setPagePermissions(target, RestrictionEdit, []Restriction{
|
err := api.setPagePermissions(target, RestrictionEdit, []Restriction{
|
||||||
{User: username},
|
{User: creds.Username},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
@ -296,7 +234,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"page successfully updated: %s\n",
|
"page successfully updated: %s\n",
|
||||||
baseURL+target.Links.Full,
|
creds.BaseURL+target.Links.Full,
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user