Skip to content

Commit e68ee34

Browse files
committed
add guiConfig
1 parent b955a7c commit e68ee34

File tree

6 files changed

+107
-29
lines changed

6 files changed

+107
-29
lines changed

app/const.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { version } from '../package.json';
22
import { computeLogPath, logger } from './utils/logger';
3-
import { API_MODES, CliApiService } from './mainProcess/cliApiService';
3+
import { CliApiService } from './mainProcess/cliApiService';
4+
import electron from 'electron';
45

56
export const IS_DEV = (process.env.NODE_ENV === 'development')
67

78
/* shared with mainProcess */
89

9-
//const API_MODE = API_MODES.RELEASE;
10-
const API_MODE = API_MODES.RELEASE;
1110
const API_VERSION = '0.9';
12-
export const cliApiService = new CliApiService(API_MODE, API_VERSION)
11+
export const cliApiService = new CliApiService(API_VERSION)
1312

1413
export const GUI_VERSION = version;
1514

@@ -57,3 +56,5 @@ export const CLI_LOG_ERROR_FILE = computeLogPath('whirlpool-cli.error.log');
5756
export const GUI_LOG_FILE = logger.getFile();
5857
export const CLI_CONFIG_FILENAME = 'whirlpool-cli-config.properties';
5958

59+
const app = electron.app || electron.remote.app;
60+
export const APP_USERDATA = app.getPath('userData')

app/containers/StatusPage.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
44
import { cliLocalService } from '../services/cliLocalService';
55
import cliService from '../services/cliService';
66
import {
7+
APP_USERDATA,
78
CLI_CONFIG_FILENAME,
89
CLI_LOG_ERROR_FILE,
9-
CLI_LOG_FILE, cliApiService,
10+
CLI_LOG_FILE,
11+
cliApiService,
1012
GUI_LOG_FILE,
1113
GUI_VERSION
1214
} from '../const';
@@ -59,12 +61,14 @@ export default class StatusPage extends Component<Props> {
5961
<Card.Body>
6062
<div className='row'>
6163
<div className='col-sm-2'>
62-
<strong>GUI logs:</strong><br/>
63-
<strong>Version:</strong>
64+
<strong>Version:</strong><br/>
65+
<strong>Path:</strong><br/>
66+
<strong>GUI logs:</strong>
6467
</div>
6568
<div className='col-sm-10'>
66-
<div><LinkExternal href={'file://'+this.guiLogFile}>{this.guiLogFile}</LinkExternal></div>
6769
<div>GUI <strong>{GUI_VERSION}</strong>, CLI API <strong>{cliApiService.getVersionName()}</strong></div>
70+
<div><LinkExternal href={'file://'+APP_USERDATA}>{APP_USERDATA}</LinkExternal></div>
71+
<div><LinkExternal href={'file://'+this.guiLogFile}>{this.guiLogFile}</LinkExternal></div>
6872
</div>
6973
</div>
7074
</Card.Body>
@@ -77,7 +81,7 @@ export default class StatusPage extends Component<Props> {
7781
<Card.Header>
7882
<div className='row'>
7983
<div className='col-sm-6'>
80-
<strong>Remote CLI</strong>
84+
<strong>CLI: remote</strong>
8185
</div>
8286
<div className='col-sm-6'>
8387
{cliStatusIcon}
@@ -98,7 +102,7 @@ export default class StatusPage extends Component<Props> {
98102
<Card.Header>
99103
<div className='row'>
100104
<div className='col-sm-6'>
101-
<strong>Standalone GUI</strong>
105+
<strong>CLI: standalone</strong>
102106
</div>
103107
<div className='col-sm-6'>
104108
{cliStatusIcon}
@@ -122,8 +126,8 @@ export default class StatusPage extends Component<Props> {
122126
{cliLocalService.hasCliApi() && <strong>Version:<br/></strong>}<br/>
123127
</div>
124128
<div className='col-sm-10'>
125-
<LinkExternal href={'file://'+cliApiService.getDownloadPath()}>{cliApiService.getDownloadPath()}</LinkExternal><br/>
126-
<LinkExternal href={'file://'+cliApiService.getDownloadPath()+CLI_CONFIG_FILENAME}>{CLI_CONFIG_FILENAME}</LinkExternal><br/>
129+
<LinkExternal href={'file://'+cliApiService.getCliPath()}>{cliApiService.getCliPath()}</LinkExternal><br/>
130+
<LinkExternal href={'file://'+cliApiService.getCliPath()+CLI_CONFIG_FILENAME}>{CLI_CONFIG_FILENAME}</LinkExternal><br/>
127131
{cliLocalService.hasCliApi() && <div>{cliLocalService.getCliFilename()} ({cliLocalService.getCliChecksum()})</div>}
128132
{cliLocalService.hasCliApi() && <div>{cliLocalService.getCliVersionStr()}</div>}
129133
{!cliLocalService.hasCliApi() && <div><strong>CLI_API {cliApiService.getVersionName()} could not be resolved</strong><br/></div>}

app/mainProcess/cliApiService.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { IS_DEV } from '../const';
2-
import electron from "electron";
1+
import { APP_USERDATA, IS_DEV } from '../const';
32
import cliVersion from './cliVersion';
43
import { logger } from '../utils/logger';
4+
import guiConfig from './guiConfig';
55

66
export const API_MODES = {
77
RELEASE: 'RELEASE',
@@ -11,7 +11,8 @@ export const API_MODES = {
1111
const DL_PATH_LOCAL = '/zl/workspaces/whirlpool/whirlpool-client-cli4/target/'
1212

1313
export class CliApiService {
14-
constructor (apiMode, apiVersion) {
14+
constructor (apiVersion) {
15+
let apiMode = guiConfig.getApiMode()
1516
if (IS_DEV) {
1617
// use local jar when started with "yarn dev"
1718
apiMode = API_MODES.LOCAL
@@ -42,12 +43,14 @@ export class CliApiService {
4243
return !IS_DEV
4344
}
4445

45-
getDownloadPath() {
46+
getCliPath() {
4647
if (this.isApiModeLocal()) {
48+
// local CLI
4749
return DL_PATH_LOCAL
4850
}
49-
const app = electron.app || electron.remote.app;
50-
return app.getPath('userData')
51+
52+
// standard CLI download path
53+
return APP_USERDATA
5154
}
5255

5356
async fetchCliApi() {
@@ -64,10 +67,11 @@ export class CliApiService {
6467
try {
6568
let cliApi = await cliVersion.fetchCliApi(fetchVersion)
6669
logger.info('Using CLI_API ' + fetchVersion, cliApi)
70+
const projectUrl = fetchVersion === API_MODES.QA ? 'SamouraiDev/QA' : 'Samourai-Wallet/whirlpool-client-cli'
6771
return {
6872
cliVersion: cliApi.CLI_VERSION,
6973
filename: 'whirlpool-client-cli-' + cliApi.CLI_VERSION + '-run.jar',
70-
url: 'https://github.com/Samourai-Wallet/whirlpool-client-cli/releases/download/' + cliApi.CLI_VERSION + '/whirlpool-client-cli-' + cliApi.CLI_VERSION + '-run.jar',
74+
url: 'https://github.com/'+projectUrl+'/releases/download/' + cliApi.CLI_VERSION + '/whirlpool-client-cli-' + cliApi.CLI_VERSION + '-run.jar',
7175
checksum: cliApi.CLI_CHECKSUM
7276
}
7377
} catch(e) {

app/mainProcess/cliLocal.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class CliLocal {
118118
}
119119

120120
async onDeleteConfig(gotMutex=false) {
121-
const cliConfigPath = cliApiService.getDownloadPath()+'/'+CLI_CONFIG_FILENAME
121+
const cliConfigPath = cliApiService.getCliPath()+'/'+CLI_CONFIG_FILENAME
122122
logger.info("CLI deleting local config... "+cliConfigPath)
123123

124124
await this.stop(gotMutex)
@@ -272,7 +272,7 @@ export class CliLocal {
272272
if (IS_DEV) {
273273
args.push('--debug-client')
274274
}
275-
myThis.startProc(cmd, args, cliApiService.getDownloadPath(), CLI_LOG_FILE, CLI_LOG_ERROR_FILE)
275+
myThis.startProc(cmd, args, cliApiService.getCliPath(), CLI_LOG_FILE, CLI_LOG_ERROR_FILE)
276276
}, (e) => {
277277
// port in use => cannot start proc
278278
logger.error("[CLI_LOCAL] cannot start: port "+DEFAULT_CLIPORT+" already in use")
@@ -438,7 +438,7 @@ export class CliLocal {
438438
}
439439

440440
async verifyChecksum() {
441-
const dlPathFile = cliApiService.getDownloadPath()+'/'+this.getCliFilename()
441+
const dlPathFile = cliApiService.getCliPath()+'/'+this.getCliFilename()
442442
const expectedChecksum = this.getCliChecksum()
443443
try {
444444
const checksum = await this.sha256File(dlPathFile)
@@ -460,7 +460,7 @@ export class CliLocal {
460460

461461
async download(url) {
462462
// delete existing file if any
463-
const dlPathFile = cliApiService.getDownloadPath()+'/'+this.getCliFilename()
463+
const dlPathFile = cliApiService.getCliPath()+'/'+this.getCliFilename()
464464
if (fs.existsSync(dlPathFile)) {
465465
logger.verbose('CLI overwriting '+dlPathFile)
466466
try {
@@ -479,7 +479,7 @@ export class CliLocal {
479479
this.updateState(CLILOCAL_STATUS.DOWNLOADING)
480480
}
481481
logger.info('CLI downloading: '+url)
482-
return download(this.window, url, {directory: cliApiService.getDownloadPath(), onProgress: onProgress.bind(this)})
482+
return download(this.window, url, {directory: cliApiService.getCliPath(), onProgress: onProgress.bind(this)})
483483
}
484484

485485
updateState(status) {

app/mainProcess/guiConfig.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { logger } from '../utils/logger';
2+
import fs from 'fs';
3+
import electron from 'electron';
4+
5+
// for some reason cliApiService.API_MODES is undefined here
6+
const API_MODES = {
7+
RELEASE: 'RELEASE',
8+
LOCAL: 'LOCAL',
9+
QA: 'QA'
10+
}
11+
12+
const CONFIG_DEFAULT = {
13+
API_MODE: API_MODES.RELEASE
14+
}
15+
16+
// for some reason const.APP_USERDATA is undefined here...
17+
const app = electron.app || electron.remote.app;
18+
const APP_USERDATA = app.getPath('userData')
19+
20+
const GUI_CONFIG_FILENAME = 'whirlpool-gui-config.json';
21+
const GUI_CONFIG_FILEPATH = APP_USERDATA+'/'+GUI_CONFIG_FILENAME
22+
23+
class GuiConfig {
24+
25+
constructor() {
26+
this.cfg = this.loadConfig()
27+
}
28+
29+
loadConfig() {
30+
let config = undefined
31+
try {
32+
config = JSON.parse(fs.readFileSync(GUI_CONFIG_FILEPATH, 'utf8'));
33+
} catch(e) {
34+
}
35+
if (!this.validate(config)) {
36+
logger.info("Using GUI configuration: default")
37+
config = CONFIG_DEFAULT
38+
this.hasConfig = false
39+
} else {
40+
logger.info("Using GUI configuration: "+GUI_CONFIG_FILEPATH)
41+
this.hasConfig = true
42+
}
43+
return config
44+
}
45+
46+
validate(config) {
47+
if (!config || !config.API_MODE || !API_MODES[config.API_MODE]) {
48+
logger.error("guiConfig is invalid: "+GUI_CONFIG_FILEPATH)
49+
return false
50+
}
51+
return true
52+
}
53+
54+
getApiMode() {
55+
return this.cfg.API_MODE
56+
}
57+
58+
hasConfig() {
59+
return this.hasConfig
60+
}
61+
62+
getConfigFile() {
63+
return GUI_CONFIG_FILEPATH
64+
}
65+
}
66+
67+
const guiConfig = new GuiConfig()
68+
export default guiConfig

app/services/guiService.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import ifNot from 'if-not-running';
2-
import fetch from 'node-fetch';
3-
import { GUI_VERSION, VERSIONS_URL } from '../const';
2+
import { GUI_VERSION } from '../const';
43
import cliService from './cliService';
54
import backendService from './backendService';
5+
import guiConfig from '../mainProcess/guiConfig';
6+
import { API_MODES } from '../mainProcess/cliApiService';
67

78
const REFRESH_RATE = 1800000; //30min
89
class GuiService {
@@ -49,12 +50,12 @@ class GuiService {
4950

5051
// getUpdate
5152

52-
5353
getGuiUpdate () {
54-
if (!this.isReady() || !cliService.isConnected()) {
54+
if (!this.isReady() || !cliService.isConnected() || guiConfig.getApiMode() !== API_MODES.RELEASE) {
5555
return undefined
5656
}
57-
const serverName = cliService.getServerName()
57+
const apiMode = guiConfig.getApiMode()
58+
const serverName = apiMode === API_MODES.QA ? API_MODES.QA : cliService.getServerName()
5859
if (serverName && this.state.guiLast[serverName] && this.state.guiLast[serverName] !== GUI_VERSION) {
5960
// update available
6061
return this.state.guiLast[serverName]

0 commit comments

Comments
 (0)