Browse Source

Api uri initialization prompt

Leszek Wiesner 4 years ago
parent
commit
21ab443445

+ 1 - 1
cli/src/Api.ts

@@ -50,7 +50,7 @@ import { LinkageResult } from '@polkadot/types/codec/Linkage'
 
 import { InputValidationLengthConstraint } from '@joystream/types/common'
 
-export const DEFAULT_API_URI = 'wss://rome-rpc-endpoint.joystream.org:9944/'
+export const DEFAULT_API_URI = 'ws://localhost:9944/'
 const DEFAULT_DECIMALS = new u32(12)
 
 // Mapping of working group to api module

+ 53 - 2
cli/src/base/ApiCommandBase.ts

@@ -5,7 +5,7 @@ import Api from '../Api'
 import { getTypeDef, createType, Option, Tuple, Bytes } from '@polkadot/types'
 import { Codec, TypeDef, TypeDefInfo, Constructor } from '@polkadot/types/types'
 import { Vec, Struct, Enum } from '@polkadot/types/codec'
-import { ApiPromise } from '@polkadot/api'
+import { ApiPromise, WsProvider } from '@polkadot/api'
 import { KeyringPair } from '@polkadot/keyring/types'
 import chalk from 'chalk'
 import { SubmittableResultImpl } from '@polkadot/api/types'
@@ -20,6 +20,7 @@ class ExtrinsicFailedError extends Error {}
  */
 export default abstract class ApiCommandBase extends StateAwareCommandBase {
   private api: Api | null = null
+  forceSkipApiUriPrompt = false
 
   getApi(): Api {
     if (!this.api) throw new CLIError('Tried to get API before initialization.', { exit: ExitCodes.ApiError })
@@ -33,10 +34,60 @@ export default abstract class ApiCommandBase extends StateAwareCommandBase {
 
   async init() {
     await super.init()
-    const apiUri: string = this.getPreservedState().apiUri
+    let apiUri: string = this.getPreservedState().apiUri
+    if (!apiUri) {
+      this.warn("You haven't provided a node/endpoint for the CLI to connect to yet!")
+      apiUri = await this.promptForApiUri()
+    }
     this.api = await Api.create(apiUri)
   }
 
+  async promptForApiUri(): Promise<string> {
+    let selectedNodeUri = await this.simplePrompt({
+      type: 'list',
+      message: 'Choose a node/endpoint:',
+      choices: [
+        {
+          name: 'Local node (ws://localhost:9944)',
+          value: 'ws://localhost:9944',
+        },
+        {
+          name: 'Current Testnet official Joystream node (wss://rome-rpc-endpoint.joystream.org:9944/)',
+          value: 'wss://rome-rpc-endpoint.joystream.org:9944/',
+        },
+        {
+          name: 'Custom endpoint',
+          value: '',
+        },
+      ],
+    })
+
+    if (!selectedNodeUri) {
+      do {
+        selectedNodeUri = await this.simplePrompt({
+          type: 'input',
+          message: 'Provide a WS endpoint uri',
+        })
+        if (!this.isApiUriValid(selectedNodeUri)) {
+          this.warn('Provided uri seems incorrect! Please try again...')
+        }
+      } while (!this.isApiUriValid(selectedNodeUri))
+    }
+
+    await this.setPreservedState({ apiUri: selectedNodeUri })
+
+    return selectedNodeUri
+  }
+
+  isApiUriValid(uri: string) {
+    try {
+      new WsProvider(uri)
+    } catch (e) {
+      return false
+    }
+    return true
+  }
+
   // This is needed to correctly handle some structs, enums etc.
   // Where the main typeDef doesn't provide enough information
   protected getRawTypeDef(type: string) {

+ 1 - 2
cli/src/base/StateAwareCommandBase.ts

@@ -2,7 +2,6 @@ import fs from 'fs'
 import path from 'path'
 import ExitCodes from '../ExitCodes'
 import { CLIError } from '@oclif/errors'
-import { DEFAULT_API_URI } from '../Api'
 import lockFile from 'proper-lockfile'
 import DefaultCommandBase from './DefaultCommandBase'
 import os from 'os'
@@ -16,7 +15,7 @@ type StateObject = {
 // State object default values
 const DEFAULT_STATE: StateObject = {
   selectedAccountFilename: '',
-  apiUri: DEFAULT_API_URI,
+  apiUri: '',
 }
 
 // State file path (relative to getAppDataPath())

+ 20 - 11
cli/src/commands/api/setUri.ts

@@ -1,28 +1,37 @@
-import StateAwareCommandBase from '../../base/StateAwareCommandBase'
 import chalk from 'chalk'
-import { WsProvider } from '@polkadot/api'
+import ApiCommandBase from '../../base/ApiCommandBase'
 import ExitCodes from '../../ExitCodes'
 
 type ApiSetUriArgs = { uri: string }
 
-export default class ApiSetUri extends StateAwareCommandBase {
+export default class ApiSetUri extends ApiCommandBase {
   static description = 'Set api WS provider uri'
   static args = [
     {
       name: 'uri',
-      required: true,
-      description: 'Uri of the node api WS provider',
+      required: false,
+      description: 'Uri of the node api WS provider (if skipped, a prompt will be displayed)',
     },
   ]
 
+  async init() {
+    this.forceSkipApiUriPrompt = true
+    super.init()
+  }
+
   async run() {
     const args: ApiSetUriArgs = this.parse(ApiSetUri).args as ApiSetUriArgs
-    try {
-      new WsProvider(args.uri)
-    } catch (e) {
-      this.error('The WS provider uri seems to be incorrect', { exit: ExitCodes.InvalidInput })
+    let newUri = ''
+    if (args.uri) {
+      if (this.isApiUriValid(args.uri)) {
+        await this.setPreservedState({ apiUri: args.uri })
+        newUri = args.uri
+      } else {
+        this.error('Provided uri seems to be incorrect!', { exit: ExitCodes.InvalidInput })
+      }
+    } else {
+      newUri = await this.promptForApiUri()
     }
-    await this.setPreservedState({ apiUri: args.uri })
-    this.log(chalk.greenBright('Api uri successfuly changed! New uri: ') + chalk.white(args.uri))
+    this.log(chalk.greenBright('Api uri successfuly changed! New uri: ') + chalk.white(newUri))
   }
 }