Source: typerepo_cli.js

/*
* (c) Copyright, Real-Time Innovations, 2012.  All rights reserved.
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the software solely for use with RTI Connext DDS. Licensee may
* redistribute copies of the software provided that all such copies are subject
* to this license. The software is provided "as is", with no warranty of any
* type, including any warranty for fitness for any purpose. RTI is under no
* obligation to maintain or support the software. RTI shall not be liable for
* any incidental or consequential damages arising out of the use or inability
* to use the software.
*/
const { Command } = require('commander')
const fs = require('fs').promises;

const libdb = require('./libdb.js')


const RTITypeUtilsTypeProvider = require('@connextdev/rticonnextdds-typeutils')
const typeprovider = new RTITypeUtilsTypeProvider()


/**
 * Command line application to interact with RTI Type Repository
<pre>
Options:
  -V, --version                 output the version number
  -h, --help                    display help for command

Commands:
  printCompleteXML [options]
  printCompleteIDL [options]
  describe
  add [options]
  get [options]
  print [options]
  delete [options]
  query [options]
  queryWithLastIndex [options]
  previous [options]
  getDomains
  getGroups
  getModules
  getPurposes
  help [command]                display help for command

  </pre>
 */


 function main() {
  args = new Command()
  typeRepoDbApi = undefined

  args.hook('preAction', async function (thisCommand) {
    if (thisCommand.opts().dbsettings) {
      let dbsettingsFile = thisCommand.opts().dbsettings
      try {
        const data = await fs.readFile(dbsettingsFile)
        const dbsettings = JSON.parse(data)
        typeRepoDbApi = new libdb.libdbapi(dbsettings)
      } catch (e) {
        console.log("Error reading dbsettings file " + dbsettingsFile)
        console.log(e)
        process.exit(1)
      }
    } else {
      typeRepoDbApi = new libdb.libdbapi()
    }
  })

  args.version('v0.0.1')
    .description('Command line application to interact with RTI Type Repository')

  args.option('-s, --dbsettings <dbsettingsfile>', 'Database settings File')

  //Command to get XML (with dependent types) from file path and type
  args.command('printCompleteXML')
    .requiredOption('-p, --path <path>', 'Path to XML file')
    .requiredOption('-t, --type <type>', 'Type name')
    .action((options, command) => {
      typeprovider.loadXmlFromUrl(options.path)
      let serializedTypeObject = typeprovider.getSerializedTypeObject(options.type)
      let xml = typeprovider.getCompleteXMLFromSerializedTypeObject(serializedTypeObject)
      typeprovider.finalizeInstance()
      console.log(xml)
      
    })
        

  //Command to get IDL (with dependent types) from file path and type
  args.command('printCompleteIDL')
    .requiredOption('-p, --path <path>', 'Path to XML file')
    .requiredOption('-t, --type <type>', 'Type name')
    .action((options, command) => {
      typeprovider.loadXmlFromUrl(options.path)
      let serializedTypeObject = typeprovider.getSerializedTypeObject(options.type)
      let idl = typeprovider.getCompleteIDLFromSerializedTypeObject(serializedTypeObject)
      typeprovider.finalizeInstance()
      console.log(idl)
    })
        
  //Commnd to describe type-repository
  args.command('describe')
    .action((options, command) => {
      typeRepoDbApi.describe()
      .then(
        res => console.log(res)
      ).catch(
        res => console.log(res)
      )
    })
        
  //Commnd to add type to type-repository
  args.command('add')
    .requiredOption('-p, --path <path>', 'Path to XML file')
    .requiredOption('-t, --type <type>', 'Type name')
    .requiredOption('-n, --domain <domain>', 'Domain')
    .requiredOption('-c, --contact <contact>', 'Contact')
    .option('-g, --group <group>', 'Group')
    .option('-d, --description <description>', 'Description of Type')
    .option('-l, --documentation <documentation>', 'Link to documentation')
    .option('-u, --purpose <Control | Status | Response>', 'Purpose of type')
    .option('-v, --version <version>', 'Logical version (e.g. v1.2.3')
    .option('-m, --previous_type <type_id>', 'Type ID of the type\'s previous version')
    .option('-o, --topic <topic>', 'Name of topic')
    .action((options, command) => {
      typeRepoDbApi.add(options.path, options.type, options.description, options.documentation, options.purpose, options.version, options.previous_type, options.topic, options.domain, options.group, options.contact)
      .then(
        res => console.log("Type " + options.type + " added successfully")
      ).catch(
        res => console.log(res)
      )
    })
        
  //Commnd to get type from type-repository
  args.command('get')
    .requiredOption('-i, --id <typeid>', 'Type id in database')
    .action((options, command) => {
      typeRepoDbApi.get(options.id).then(
        record => console.log(record)
      ).catch(
        e => console.log(e)
      );
    })
    
  //Commnd to get type from type-repository
  args.command('print')
    .requiredOption('-i, --id <typeid>', 'Type id in database')
    .requiredOption('-k, --kind <XML|IDL>', 'Type of output', 'XML')
    .requiredOption('-w, --with_dependencies <0|1>', 'complete type', 0)
    .action((options, command) => {
       typeRepoDbApi.get(options.id)
        .then(record => {
          if (record === {}) {
            console.log("Unable to find type " + options.id)
          } else {
            if (options.kind === "IDL") {
                if (options.with_dependencies === '0') {
                  typeprovider.printIDLFromSerializedTypeObject(record.Item.typeobject.B)
                } else if (options.with_dependencies === '1') {
                  typeprovider.printCompleteIDLFromSerializedTypeObject(record.Item.typeobject.B)
                } else {
                    console.log("Option -w must be 0 or 1")
                }
            } else if (options.kind == "XML") {
                if (options.with_dependencies === '0') {
                  typeprovider.printXMLFromSerializedTypeObject(record.Item.typeobject.B)
                } else if (options.with_dependencies === "1") {
                  typeprovider.printCompleteXMLFromSerializedTypeObject(record.Item.typeobject.B)
                } else {
                    console.log("Option -w must be 0 or 1")
                }
            } else {
                console.log("Invalid print kind (not XML or IDL)" + options.id)
            }
          }
        }).catch(
          e => console.log(e)
        )
    })
    
  //Commnd to get type from type-repository
  args.command('delete')
    .requiredOption('-i, --id <typeid>', 'Type id in database')
    .action((options, command) => {
       typeRepoDbApi.delete(options.id)
       .then(
         res => console.log(res)
       ).catch(
         e => console.log(e)
       )
    })

  //Commnd to query types from type-repository
  args.command('query')
    .option('-d, --domain <domain>', 'Type domain in database', undefined)
    .option('-g, --group <group>', 'Group domain in database', undefined)
    .option('-m, --modules <group>', 'Group domain in database', undefined)
    .option('-o, --topic <topic>', 'Topic domain in database', undefined)
    .option('-t, --type <type>', 'Type name in database', undefined)
    .option('-c, --contains <0|1>', 'Whether to check for complete or partial matches', 0)
    .option('-b, --begin <begin>', 'Start Index (default 0)', 0)
    .option('-e, --end <end>', 'End index, other wise return all', 1000)
    .action((options, command) => {
      typeRepoDbApi.query(options.domain, options.group, options.modules, options.topic, options.type, options.contains, options.begin, options.end)
      .then(res => {
        for (var i = 0; i < res.length; i++) {
          console.log(res[i])
        }
      }).catch(
        e => console.log(e)
      )
    })
    
  //Commnd to query types from type-repository
  args.command('queryWithLastIndex')
    .option('-d, --domain <domain>', 'Type domain in database', undefined)
    .option('-g, --group <group>', 'Group domain in database', undefined)
    .option('-m, --modules <group>', 'Group domain in database', undefined)
    .option('-o, --topic <topic>', 'Topic domain in database', undefined)
    .option('-t, --type <type>', 'Type name in database', undefined)
    .option('-c, --contains <0|1>', 'Whether to check for complete or partial matches', 0)
    .option('-l, --last_typeid <last_typeid>', 'Start Index (default 0)', undefined)
    .option('-i, --limit <limit>', 'End index, other wise return all', 1000)
    .action((options, command) => {
      typeRepoDbApi.queryWithLastIndex(options.domain, options.group, options.modules, options.topic, options.type, options.contains, options.last_typeid, options.limit)
      .then(res => {
        for (var i = 0; i < res.length; i++) {
          console.log(res[i])
        }
      }).catch(
        e => console.log(e)
      )
    })
    
  //Command to get previous type from typ-repository
  args.command('previous')
    .requiredOption('-i, --id <typeid>', 'Type id in database')
    .action((options, command) => {
      typeRepoDbApi.get(options.id)
      .then(record => {
        if (!(Object.keys(record) === 0)) {
          typeRepoDbApi.get(record.Item.parent_type_id.N)
          .then(
            p_record => console.log(p_record)
          ).catch(
            e => console.log(e)
          )
        } else {
          console.log("Unable to find type " + options.id)
        }
      }).catch(
        e => console.log(e)
      )
    })
    
  //Command to get all unique domains as well as their count
  args.command('getDomains')
    .action((options, command) => {
      typeRepoDbApi.getDomains().then(modules => console.log(modules));
    })
    
  //Command to get all unique groups as well as their count
  args.command('getGroups')
    .action((options, command) => {
      typeRepoDbApi.getGroups().then(groups => console.log(groups));
    })
    
  //Command to get all unique domains as well as their count
  args.command('getModules')
    .action((options, command) => {
      typeRepoDbApi.getModules().then(modules => console.log(modules));
    })
    
  //Command to get all unique purposes as well as their count
  args.command('getPurposes')
    .action((options, command) => {
      typeRepoDbApi.getPurpose().then(purposes => console.log(purposes));
    })

  //Process commandline args
  args.parse()

  

}

if (require.main === module) {
   main()
}