configMenu.js
1 export class ConfigMenu { 2 constructor(uiService, menuLoop, configService, numberSelector) { 3 this.ui = uiService; 4 this.loop = menuLoop; 5 this.configService = configService; 6 this.numberSelector = numberSelector; 7 8 this.loop.initialize(this.showConfigMenu); 9 } 10 11 show = async () => { 12 this.config = this.configService.get(); 13 this.ui.showInfoMessage("Codex Configuration"); 14 await this.loop.showLoop(); 15 }; 16 17 showConfigMenu = async () => { 18 await this.ui.askMultipleChoice("Select to edit:", [ 19 { 20 label: `Storage quota = ${this.bytesAmountToString(this.config.storageQuota)}`, 21 action: this.editStorageQuota, 22 }, 23 { 24 label: `Discovery port = ${this.config.ports.discPort}`, 25 action: this.editDiscPort, 26 }, 27 { 28 label: `P2P listen port = ${this.config.ports.listenPort}`, 29 action: this.editListenPort, 30 }, 31 { 32 label: `API port = ${this.config.ports.apiPort}`, 33 action: this.editApiPort, 34 }, 35 { 36 label: "Save changes and exit", 37 action: this.saveChangesAndExit, 38 }, 39 { 40 label: "Discard changes and exit", 41 action: this.discardChangesAndExit, 42 }, 43 ]); 44 }; 45 46 // this and the byte-format handling in 47 // numberSelector should be extracted to 48 // their own util. 49 bytesAmountToString = (numBytes) => { 50 const units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; 51 52 var value = numBytes; 53 var index = 0; 54 while (value > 1024) { 55 index = index + 1; 56 value = value / 1024; 57 } 58 59 if (index == 0) return `${numBytes} Bytes`; 60 return `${numBytes} Bytes (${value} ${units[index]})`; 61 }; 62 63 editStorageQuota = async () => { 64 this.ui.showInfoMessage("You can use: 'GB' or 'gb', etc."); 65 const newQuota = await this.numberSelector.show( 66 this.config.storageQuota, 67 "Storage quota", 68 true, 69 ); 70 if (newQuota < 100 * 1024 * 1024) { 71 this.ui.showErrorMessage("Storage quote should be >= 100mb."); 72 } else { 73 this.config.storageQuota = newQuota; 74 } 75 }; 76 77 editDiscPort = async () => { 78 const newPort = await this.numberSelector.show( 79 this.config.ports.discPort, 80 "Discovery port", 81 false, 82 ); 83 if (this.isInPortRange(newPort)) { 84 this.config.ports.discPort = newPort; 85 } 86 }; 87 88 editListenPort = async () => { 89 const newPort = await this.numberSelector.show( 90 this.config.ports.listenPort, 91 "P2P listen port", 92 false, 93 ); 94 if (this.isInPortRange(newPort)) { 95 this.config.ports.listenPort = newPort; 96 } 97 }; 98 99 editApiPort = async () => { 100 const newPort = await this.numberSelector.show( 101 this.config.ports.apiPort, 102 "API port", 103 false, 104 ); 105 if (this.isInPortRange(newPort)) { 106 this.config.ports.apiPort = newPort; 107 } 108 }; 109 110 isInPortRange = (number) => { 111 if (number < 1024 || number > 65535) { 112 this.ui.showErrorMessage("Port should be between 1024 and 65535."); 113 return false; 114 } 115 return true; 116 }; 117 118 saveChangesAndExit = async () => { 119 this.configService.saveConfig(); 120 this.ui.showInfoMessage("Configuration changes saved."); 121 this.loop.stopLoop(); 122 }; 123 124 discardChangesAndExit = async () => { 125 this.configService.loadConfig(); 126 this.ui.showInfoMessage("Changes discarded."); 127 this.loop.stopLoop(); 128 }; 129 }