191 lines
5.2 KiB
Go
191 lines
5.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/alexflint/go-arg"
|
||
|
)
|
||
|
|
||
|
type NodeData struct {
|
||
|
id string
|
||
|
title string
|
||
|
}
|
||
|
|
||
|
type Node struct {
|
||
|
children []*Node
|
||
|
data NodeData
|
||
|
}
|
||
|
|
||
|
func (n Node) printChildren(level int) {
|
||
|
log.Printf("%v%v", strings.Repeat("\t", level), n.data.title)
|
||
|
// leaf note
|
||
|
if len(n.children) == 0 {
|
||
|
return
|
||
|
}
|
||
|
for _, child := range n.children {
|
||
|
child.printChildren(level + 1)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (n *Node) getChildWithID(id string) (node *Node, hasChild bool) {
|
||
|
for _, c := range n.children {
|
||
|
if c.data.id == id {
|
||
|
return c, true
|
||
|
}
|
||
|
}
|
||
|
empty := Node{}
|
||
|
return &empty, false
|
||
|
}
|
||
|
|
||
|
func (n *Node) addChild(child *Node) {
|
||
|
n.children = append(n.children, child)
|
||
|
}
|
||
|
|
||
|
type spaceResult struct {
|
||
|
Results []struct {
|
||
|
ID string `json:"id"`
|
||
|
Type string `json:"type"`
|
||
|
Status string `json:"status"`
|
||
|
Title string `json:"title"`
|
||
|
Ancestors []struct {
|
||
|
ID string `json:"id"`
|
||
|
Type string `json:"type"`
|
||
|
Status string `json:"status"`
|
||
|
Title string `json:"title"`
|
||
|
MacroRenderedOutput struct {
|
||
|
} `json:"macroRenderedOutput"`
|
||
|
Extensions struct {
|
||
|
Position int `json:"position"`
|
||
|
} `json:"extensions"`
|
||
|
Expandable struct {
|
||
|
Container string `json:"container"`
|
||
|
Metadata string `json:"metadata"`
|
||
|
Restrictions string `json:"restrictions"`
|
||
|
History string `json:"history"`
|
||
|
Body string `json:"body"`
|
||
|
Version string `json:"version"`
|
||
|
Descendants string `json:"descendants"`
|
||
|
Space string `json:"space"`
|
||
|
ChildTypes string `json:"childTypes"`
|
||
|
Operations string `json:"operations"`
|
||
|
SchedulePublishDate string `json:"schedulePublishDate"`
|
||
|
Children string `json:"children"`
|
||
|
Ancestors string `json:"ancestors"`
|
||
|
} `json:"_expandable"`
|
||
|
Links struct {
|
||
|
Self string `json:"self"`
|
||
|
Tinyui string `json:"tinyui"`
|
||
|
Editui string `json:"editui"`
|
||
|
Webui string `json:"webui"`
|
||
|
} `json:"_links"`
|
||
|
} `json:"ancestors"`
|
||
|
MacroRenderedOutput struct {
|
||
|
} `json:"macroRenderedOutput"`
|
||
|
Extensions struct {
|
||
|
Position int `json:"position"`
|
||
|
} `json:"extensions"`
|
||
|
Expandable struct {
|
||
|
ChildTypes string `json:"childTypes"`
|
||
|
Container string `json:"container"`
|
||
|
Metadata string `json:"metadata"`
|
||
|
Operations string `json:"operations"`
|
||
|
SchedulePublishDate string `json:"schedulePublishDate"`
|
||
|
Children string `json:"children"`
|
||
|
Restrictions string `json:"restrictions"`
|
||
|
History string `json:"history"`
|
||
|
Body string `json:"body"`
|
||
|
Version string `json:"version"`
|
||
|
Descendants string `json:"descendants"`
|
||
|
Space string `json:"space"`
|
||
|
} `json:"_expandable"`
|
||
|
Links struct {
|
||
|
Self string `json:"self"`
|
||
|
Tinyui string `json:"tinyui"`
|
||
|
Editui string `json:"editui"`
|
||
|
Webui string `json:"webui"`
|
||
|
} `json:"_links"`
|
||
|
} `json:"results"`
|
||
|
Start int `json:"start"`
|
||
|
Limit int `json:"limit"`
|
||
|
Size int `json:"size"`
|
||
|
Links struct {
|
||
|
Base string `json:"base"`
|
||
|
Context string `json:"context"`
|
||
|
Next string `json:"next"`
|
||
|
Self string `json:"self"`
|
||
|
} `json:"_links"`
|
||
|
}
|
||
|
|
||
|
func getSpaceChildren(url, user, token string) spaceResult {
|
||
|
b64Auth := base64.StdEncoding.EncodeToString([]byte(user + ":" + token))
|
||
|
client := &http.Client{}
|
||
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
req.Header.Add("Authorization", "Basic "+b64Auth)
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
log.Fatalf("Error fetching url: %v, status code: %v", url, resp.StatusCode)
|
||
|
}
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
var result spaceResult
|
||
|
json.Unmarshal(body, &result)
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func createPageTree(s *spaceResult) {
|
||
|
var parent *Node
|
||
|
root := Node{data: NodeData{title: "root"}}
|
||
|
for _, page := range s.Results {
|
||
|
parent = &root
|
||
|
// ignore the root node
|
||
|
if len(page.Ancestors) == 0 {
|
||
|
continue
|
||
|
}
|
||
|
// create branches from ancestors
|
||
|
for i, ancestor := range page.Ancestors {
|
||
|
// skip root node
|
||
|
if i == 0 {
|
||
|
continue
|
||
|
}
|
||
|
node, hasChild := parent.getChildWithID(ancestor.ID)
|
||
|
if hasChild {
|
||
|
parent = node
|
||
|
continue
|
||
|
}
|
||
|
n := Node{data: NodeData{id: ancestor.ID, title: ancestor.Title}}
|
||
|
parent.addChild(&n)
|
||
|
parent = &n
|
||
|
}
|
||
|
// create leaf node
|
||
|
n := Node{data: NodeData{id: page.ID, title: page.Title}}
|
||
|
parent.addChild(&n)
|
||
|
}
|
||
|
root.printChildren(0)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var args struct {
|
||
|
Baseurl string `arg:"required" help:"Base URL of the Confluence instance (required)"`
|
||
|
Spacekey string `arg:"required" help:"Spacekey to export (required)"`
|
||
|
User string `arg:"required" help:"User used for authentication (required)"`
|
||
|
Token string `arg:"required" help:"Token used for authentication (required)"`
|
||
|
}
|
||
|
arg.MustParse(&args)
|
||
|
url := args.Baseurl + "/wiki/rest/api/space/" + args.Spacekey + "/content/page?expand=ancestors&limit=9999"
|
||
|
result := getSpaceChildren(url, args.User, args.Token)
|
||
|
createPageTree(&result)
|
||
|
}
|