-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
148 lines (132 loc) · 4.03 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const ipfsApi = require('ipfs-http-client')
const fs = require('fs')
const configFile = './config.json'
const argv = process.argv.slice(2)
let cfg = {
ipfs: "http://localhost:5001/api/v0",
pins: [],
}
// Load config file
try {
cfg = {...cfg, ...(JSON.parse(fs.readFileSync(configFile, 'utf8')))}
} catch (e) {
console.log("[INFO] no config.json file found. using defaults")
}
// Connect to ipfs node
const ipfs = new ipfsApi(cfg.ipfs)
const printVersion = () => {
console.log("ipns-pin version 1.1.0")
}
// Helper function to update pins
const updatePin = (pin) => new Promise(async (resolve, reject) => {
// Resolve ipns to cid
let last = null
for await (const name of ipfs.name.resolve(`/ipns/${pin.ipns}`)) {
last = name
}
const cid = last.substr(6).trim('/')
// Check if pin has changed or was added
if(!pin.current || pin.current != cid) {
// Pin new cid
ipfs.pin.add(cid, {
recursive: true,
}).then(() => {
const newPin = {...pin, current: cid}
// Unpin old cid if necessary
if (pin.current) {
ipfs.pin.rm(pin.current).catch(e => {
console.log('[WARNING] failed to remove old pin')
}).finally(() => {
resolve(newPin)
})
} else {
resolve(newPin)
}
}).catch(e => {
console.log("[ERROR] failed to update pin: ", e)
resolve(pin)
})
} else {
//TODO: verify pin! Currently not implemented in js-ipfs
resolve(pin)
}
})
const addPin = (ipns) => {
const add = cfg.pins.find(p => p.ipns == ipns)
if (add)
console.log("Error: pin already exists")
else {
updatePin({ipns: ipns}).then(newPin => {
writeConfig({...cfg, pins: [...cfg.pins, newPin]})
}).catch(e => {
console.log("Error: failed to pin")
})
}
}
const rmPin = async (ipns) => {
const rm = cfg.pins.find(p => p.ipns == ipns)
if (rm) {
if (rm.current)
await ipfs.pin.rm(rm.current)
writeConfig({...cfg, pins: cfg.pins.filter(p => p.ipns != ipns)})
} else {
console.log("Error: pin not found")
}
}
const listPins = () => {
for (const pin of cfg.pins) {
console.log(`${pin.ipns} ${pin.current}`)
}
}
const writeConfig = (cfg) => {
console.log('Writing config...')
fs.writeFileSync(configFile, JSON.stringify(cfg, null, '\t'))
console.log('Done!')
}
const printUpdated = (old, current) => {
for (const pin of old) {
const newPin = current.find(a => a.ipns == pin.ipns)
if (newPin.current != pin.current) {
console.log(`[${pin.ipns}] ${pin.current} -> ${newPin.current}`)
}
}
}
const printHelp = () => {
console.log("Usage:")
console.log("ipns-pin version - Prints current version")
console.log("ipns-pin update - Updates pins")
console.log("ipns-pin ls - lists all pins")
console.log("ipns-pin add {nodeid/dnslink} - add new ipns pin")
console.log("ipns-pin rm {nodeid/dnslink} - remove a ipns pin")
console.log("ipns-pin ipfs {api address} - set which ipfs node to use")
}
// If pins exist run them all through the previous helper
if (argv.length == 1) {
if (argv[0] == "update") {
if (cfg.pins) {
console.log('Updating pins...')
Promise.all(cfg.pins.map(updatePin)).then(r => {
console.log('Pins updated!')
printUpdated(cfg.pins, r)
// Merge updated pins with existing config
const newCfg = {...cfg, pins: r}
writeConfig(newCfg)
})
} else {
console.log('error: no pins found in config.json')
}
}
else if (argv[0] == "ls") {
listPins()
} else if (argv[0] == "version")
printVersion()
else printHelp()
} else if (argv.length == 2) {
if (argv[0] == "add") {
addPin(argv[1])
} else if (argv[0] == "rm") {
rmPin(argv[1])
} else if (argv[0] == 'ipfs') {
writeConfig({...cfg, ipfs: argv[1]})
} else printHelp()
} else printHelp()