Skip to content
Snippets Groups Projects
Select Git revision
  • 4a7719cb9724312e8fc2f111d39af20b786e462e
  • dev default protected
  • vainamoinen197-transactiondocument-replace-vec-fields-by-smallvec-2
  • dvermd/200-keypairs-dewif
  • elois/wot
  • jawaka/155-dbex-add-dump-fork-tree-command
  • elois/195-bcdbwriteop
  • elois/deps-crypto
  • elois/gva-monetary-mass
  • elois/191-sled
  • elois/195
  • ji_emme/gva-humantimefield
  • 184-gva-rename-commontime-field-to-blockchaintime
  • ji_emme/182-gva-implement-block-meta-data
  • ji_emme/rml14
  • hugo/151-ws2pv2-sync
  • ji_emme/181-gva-implement-identity-request
  • ji_emme/89-implement-client-api-gva-graphql-verification-api
  • logo
  • test-juniper-from-schema
  • elois/exemple-gva-global-context
  • v0.2.0-a4 protected
  • v0.2.0-a2 protected
  • v0.2.0-a protected
  • v0.1.1-a1 protected
  • documents/v0.10.0-b1 protected
  • crypto/v0.4.0-b1 protected
  • crypto/v0.3.0-b3 protected
  • crypto/v0.3.0-b2 protected
  • crypto/v0.3.0-b1 protected
  • wot/v0.8.0-a0.9 protected
  • wot/v0.8.0-a0.8 protected
  • 0.1.0-a0.1 protected
  • v0.0.1-a0.12 protected
  • v0.0.1-a0.11 protected
  • v0.0.1-a0.10 protected
  • v0.0.1-a0.9 protected
  • v0.0.1-a0.8 protected
  • v0.0.1-a0.7 protected
  • v0.0.1-a0.6 protected
  • v0.0.1-a0.5 protected
41 results

lib.rs

Blame
  • cli.ts 4.56 KiB
    // Source file from duniter: Crypto-currency software to manage libre currency such as Ğ1
    // Copyright (C) 2018  Cedric Moreau <cem.moreau@gmail.com>
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    
    const Command = require('commander').Command;
    const pjson = require('../package.json');
    const duniter = require('../index');
     
    export const ExecuteCommand = () => {
    
      const options:any = [];
      const commands:any = [];
    
      return {
    
        addOption: (optFormat:string, optDesc:string, optParser:any) => options.push({ optFormat, optDesc, optParser }),
    
        addCommand: (command:any, executionCallback:any) => commands.push({ command, executionCallback }),
    
        // To execute the provided command
        execute: async (programArgs:string[]) => {
    
          const program = new Command();
    
          // Callback for command success
          let onResolve:any;
    
          // Callback for command rejection
          let onReject:any = () => Promise.reject(Error("Uninitilized rejection throw"));
    
          // Command execution promise
          const currentCommand = new Promise((resolve, reject) => {
            onResolve = resolve;
            onReject = reject;
          });
    
          program
            .version(pjson.version)
            .usage('<command> [options]')
    
            .option('--home <path>', 'Path to Duniter HOME (defaults to "$HOME/.config/duniter").')
            .option('-d, --mdb <name>', 'Database name (defaults to "duniter_default").')
    
            .option('--autoconf', 'With `config` and `init` commands, will guess the best network and key options witout asking for confirmation')
            .option('--addep <endpoint>', 'With `config` command, add given endpoint to the list of endpoints of this node')
            .option('--remep <endpoint>', 'With `config` command, remove given endpoint to the list of endpoints of this node')
    
            .option('--cpu <percent>', 'Percent of CPU usage for proof-of-work computation', parsePercent)
            .option('--nb-cores <number>', 'Number of cores uses for proof-of-work computation', parseInt)
            .option('--prefix <nodeId>', 'Prefix node id for the first character of nonce', parseInt)
    
            .option('-c, --currency <name>', 'Name of the currency managed by this node.')
    
            .option('--nostdout', 'Disable stdout printing for `export-bc` command')
            .option('--noshuffle', 'Disable peers shuffling for `sync` command')
    
            .option('--socks-proxy <host:port>', 'Use Socks Proxy')
            .option('--tor-proxy <host:port>', 'Use Tor Socks Proxy')
            .option('--reaching-clear-ep <clear|tor|none>', 'method for reaching an clear endpoint')
            .option('--force-tor', 'force duniter to contact endpoint tor (if you redirect the traffic to tor yourself)')
            .option('--rm-proxies', 'Remove all proxies')
    
            .option('--timeout <milliseconds>', 'Timeout to use when contacting peers', parseInt)
            .option('--httplogs', 'Enable HTTP logs')
            .option('--nohttplogs', 'Disable HTTP logs')
            .option('--isolate', 'Avoid the node to send peering or status informations to the network')
            .option('--forksize <size>', 'Maximum size of fork window', parseInt)
            .option('--memory', 'Memory mode')
          ;
    
          for (const opt of options) {
            program
              .option(opt.optFormat, opt.optDesc, opt.optParser);
          }
    
          for (const cmd of commands) {
            program
              .command(cmd.command.name)
              .description(cmd.command.desc)
              .action(async function() {
                const args = Array.from(arguments);
                try {
                  const resOfExecution = await cmd.executionCallback.apply(null, [program].concat(args));
                  onResolve(resOfExecution);
                } catch (e) {
                  onReject(e);
                }
              });
          }
    
          program
            .on('*', function (cmd:any) {
              console.log("Unknown command '%s'. Try --help for a listing of commands & options.", cmd);
              onResolve();
            });
    
          program.parse(programArgs);
    
          if (programArgs.length <= 2) {
            onReject('No command given.');
          }
          return currentCommand;
        }
      };
    };
    
    function parsePercent(s:string) {
      const f = parseFloat(s);
      return isNaN(f) ? 0 : f;
    }