Skip to content

Commit 5d01e89

Browse files
committed
feat: allow custom ipfs binary
License: MIT Signed-off-by: Henrique Dias <[email protected]>
1 parent b9d6f18 commit 5d01e89

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

assets/locales/en.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"moveRepositoryLocation": "Move Repository Location",
4141
"runGarbageCollector": "Run Garbage Collector",
4242
"selectDirectory": "Select Directory",
43+
"setCustomIpfsBinary": "Set Custom IPFS Binary",
44+
"clearCustomIpfsBinary": "Clear Custom IPFS Binary",
4345
"polkitDialog": {
4446
"title": "Polkit not found",
4547
"message": "IPFS can't be added to /usr/local/bin/ without polkit agent."
@@ -171,5 +173,17 @@
171173
"couldNotSaveDialog": {
172174
"title": "Could not write to disk",
173175
"message": "There was an error writing to the disk. Please try again."
176+
},
177+
"setCustomIpfsBinaryConfirmation": {
178+
"title": "Custom IPFS binary",
179+
"message": "By setting a custom IPFS, IPFS Desktop will no longer use the bundled IPFS version and will use the binary you specify. Do you wish to proceed?"
180+
},
181+
"setCustomIpfsBinarySuccess": {
182+
"title": "Custom IPFS binary",
183+
"message": "IPFS Desktop will start using the binary located at { path }. To start using it, IPFS needs to be restarted first."
184+
},
185+
"clearCustomIpfsBinarySuccess": {
186+
"title": "Clear custom IPFS binary",
187+
"message": "The custom IPFS binary was cleared. To start using the bundled IPFS version, IPFS needs to be restarted first."
174188
}
175189
}

src/custom-ipfs-binary.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const i18n = require('i18next')
2+
const { app, dialog } = require('electron')
3+
const { showDialog } = require('./dialogs')
4+
const logger = require('./common/logger')
5+
const store = require('./common/store')
6+
const dock = require('./dock')
7+
8+
const SETTINGS_KEY = 'binaryPath'
9+
10+
async function setCustomBinary (ctx) {
11+
logger.info('[custom binary] request to change')
12+
let opt = showDialog({
13+
title: i18n.t('setCustomIpfsBinaryConfirmation.title'),
14+
message: i18n.t('setCustomIpfsBinaryConfirmation.message'),
15+
type: 'warning',
16+
buttons: [
17+
i18n.t('yes'),
18+
i18n.t('no')
19+
]
20+
})
21+
22+
if (opt !== 0) {
23+
logger.info('[custom binary] user canceled')
24+
return
25+
}
26+
27+
const { canceled, filePaths } = await dock.run(() => dialog.showOpenDialog({
28+
title: i18n.t('pickCustomIpfsBinary'),
29+
defaultPath: app.getPath('home'),
30+
properties: ['openFile']
31+
}))
32+
33+
if (canceled || filePaths.length === 0) {
34+
logger.info('[custom binary] user canceled')
35+
return
36+
}
37+
38+
store.set(SETTINGS_KEY, filePaths[0])
39+
40+
opt = showDialog({
41+
title: i18n.t('setCustomIpfsBinarySuccess.title'),
42+
message: i18n.t('setCustomIpfsBinarySuccess.message', { path: filePaths[0] }),
43+
buttons: [
44+
i18n.t('restart'),
45+
i18n.t('close')
46+
]
47+
})
48+
49+
logger.info(`[custom binary] updated to ${filePaths[0]}`)
50+
51+
if (opt === 0) {
52+
ctx.restartIpfs()
53+
}
54+
}
55+
56+
function clearCustomBinary (ctx) {
57+
store.delete('binaryPath')
58+
logger.info('[custom binary] cleared')
59+
60+
const opt = showDialog({
61+
title: i18n.t('clearCustomIpfsBinarySuccess.title'),
62+
message: i18n.t('clearCustomIpfsBinarySuccess.message'),
63+
buttons: [
64+
i18n.t('restart'),
65+
i18n.t('close')
66+
]
67+
})
68+
69+
if (opt === 0) {
70+
ctx.restartIpfs()
71+
}
72+
}
73+
74+
function hasCustomBinary () {
75+
return typeof store.get(SETTINGS_KEY) === 'string'
76+
}
77+
78+
function getCustomBinary () {
79+
if (hasCustomBinary()) {
80+
return store.get(SETTINGS_KEY)
81+
}
82+
}
83+
84+
module.exports = {
85+
setCustomBinary,
86+
clearCustomBinary,
87+
hasCustomBinary,
88+
getCustomBinary
89+
}

src/tray.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const store = require('./common/store')
1010
const { IS_MAC, IS_WIN, VERSION, GO_IPFS_VERSION } = require('./common/consts')
1111
const moveRepositoryLocation = require('./move-repository-location')
1212
const runGarbageCollector = require('./run-gc')
13+
const { setCustomBinary, clearCustomBinary, hasCustomBinary } = require('./custom-ipfs-binary')
1314

1415
// Notes on this: we are only supporting accelerators on macOS for now because
1516
// they natively work as soon as the menu opens. They don't work like that on Windows
@@ -105,6 +106,18 @@ function buildMenu (ctx) {
105106
label: i18n.t('runGarbageCollector'),
106107
click: () => { runGarbageCollector(ctx) },
107108
enabled: false
109+
},
110+
{
111+
id: 'setCustomBinary',
112+
label: i18n.t('setCustomIpfsBinary'),
113+
click: () => { setCustomBinary(ctx) },
114+
visible: false
115+
},
116+
{
117+
id: 'clearCustomBinary',
118+
label: i18n.t('clearCustomIpfsBinary'),
119+
click: () => { clearCustomBinary(ctx) },
120+
visible: false
108121
}
109122
]
110123
},
@@ -120,7 +133,7 @@ function buildMenu (ctx) {
120133
click: () => { shell.openExternal('https://github.com/ipfs-shipyard/ipfs-desktop/releases') }
121134
},
122135
{
123-
label: `go-ipfs ${GO_IPFS_VERSION}`,
136+
label: `go-ipfs ${hasCustomBinary() ? '(custom)' : GO_IPFS_VERSION}`,
124137
click: () => { shell.openExternal('https://github.com/ipfs/go-ipfs/releases') }
125138
},
126139
{ type: 'separator' },
@@ -217,6 +230,9 @@ module.exports = function (ctx) {
217230
menu.getMenuItemById('moveRepositoryLocation').enabled = !gcRunning && status !== STATUS.STOPPING_STARTED
218231
menu.getMenuItemById('runGarbageCollector').enabled = menu.getMenuItemById('ipfsIsRunning').visible && !gcRunning
219232

233+
menu.getMenuItemById('setCustomBinary').visible = !hasCustomBinary()
234+
menu.getMenuItemById('clearCustomBinary').visible = hasCustomBinary()
235+
220236
if (status === STATUS.STARTING_FINISHED) {
221237
tray.setImage(icon(on))
222238
} else {

0 commit comments

Comments
 (0)