-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.js
executable file
·64 lines (55 loc) · 1.9 KB
/
search.js
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
#!/usr/bin/env node
import fetch from 'node-fetch'
import dotenv from 'dotenv'
import { Buffer } from 'node:buffer'
import { Command } from 'commander'
dotenv.config()
console.debug = process.env.DEBUG
? console.table
: () => {
}
const options = new Command()
.name('search.js')
.description('CLI to search Confluence Pages')
.version('0.0.1')
.requiredOption('-q, --query <query>', 'CQL query to search for, eg: text~gitlab')
.requiredOption('-u, --user <user>', 'user eg: [email protected]')
.requiredOption('-t, --token <token>', 'your_user_api_token with scope read:content-details:confluence,write:content:confluence')
.requiredOption('-d, --domain <domainurl>', 'eg: https://<domain_name>.atlassian.net')
.parse()
.opts()
console.debug(options)
let user; let token; let query; let domain = ''
query = options.query
user = options.user ? user = options.user : user = process.env.CONFLUENCE_USER
token = options.token ? token = options.token : token = process.env.CONFLUENCE_TOKEN
domain = options.domain ? domain = options.domain : domain = process.env.CONFLUENCE_DOMAIN
const searchQuery = domain + '/wiki/rest/api/content/search?cql=' + query
const header = {
'Content-Type': 'application/json',
Authorization: 'Basic ' + Buffer.from(user + ':' + token).toString('base64')
}
console.table({ searchQuery })
console.debug(header)
fetch(searchQuery, {
method: 'GET',
headers: header
}).then(res => res.json())
.then(json => {
if (json.results != null) {
const hm = []
json.results.forEach(result => {
hm.push({
id: result.id,
type: result.type,
url: domain + '/wiki' + result._links.tinyui,
title: result.title
})
})
json.size ? console.table(hm) : console.table({ 'No results matching query': query })
} else {
console.error(json.message)
}
}).catch(err => {
console.error(err)
})