All files / commands config.js

0% Statements 0/71
0% Branches 0/25
0% Functions 0/7
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * Configuration Command
 * Manage CLI configuration settings
 */
 
const { Command } = require('commander')
const chalk = require('chalk')
const inquirer = require('inquirer')
const { table } = require('table')
 
const ConfigManager = require('../utils/config-manager')
 
const configCommand = new Command('config')
  .description('Manage CLI configuration')
 
// Set configuration
configCommand
  .command('set')
  .description('Set configuration value')
  .argument('<key>', 'configuration key')
  .argument('<value>', 'configuration value')
  .action(async (key, value) => {
    try {
      ConfigManager.set(key, value)
      console.log(chalk.green(`āœ… Set ${key} = ${value}`))
    } catch (error) {
      console.error(chalk.red(`Failed to set config: ${error.message}`))
      process.exit(1)
    }
  })
 
// Get configuration
configCommand
  .command('get')
  .description('Get configuration value')
  .argument('[key]', 'configuration key (optional)')
  .action(async (key) => {
    try {
      if (key) {
        const value = ConfigManager.get(key)
        if (value !== undefined) {
          console.log(value)
        } else {
          console.log(chalk.gray(`${key} is not set`))
        }
      } else {
        // Show all config
        const config = ConfigManager.getConfig()
        
        if (Object.keys(config).length === 0) {
          console.log(chalk.gray('No configuration found'))
          return
        }
        
        console.log(chalk.bold('šŸ”§ Vaultace Configuration\n'))
        
        const tableData = [[chalk.bold('Key'), chalk.bold('Value')]]
        
        // Flatten config and hide sensitive data
        const flatConfig = flattenConfig(config)
        Object.entries(flatConfig).forEach(([k, v]) => {
          let displayValue = v
          
          // Hide sensitive values
          if (k.includes('token') || k.includes('secret') || k.includes('key')) {
            displayValue = v ? '***hidden***' : chalk.gray('not set')
          }
          
          tableData.push([k, displayValue])
        })
        
        console.log(table(tableData))
      }
    } catch (error) {
      console.error(chalk.red(`Failed to get config: ${error.message}`))
      process.exit(1)
    }
  })
 
// Reset configuration
configCommand
  .command('reset')
  .description('Reset configuration to defaults')
  .option('-f, --force', 'skip confirmation prompt')
  .action(async (options) => {
    try {
      if (!options.force) {
        const { confirm } = await inquirer.prompt([{
          type: 'confirm',
          name: 'confirm',
          message: 'Reset all configuration to defaults?',
          default: false
        }])
        
        if (!confirm) {
          console.log(chalk.gray('Operation cancelled'))
          return
        }
      }
      
      ConfigManager.reset()
      console.log(chalk.green('āœ… Configuration reset to defaults'))
      console.log(chalk.gray('You will need to login again: vaultace auth login'))
      
    } catch (error) {
      console.error(chalk.red(`Failed to reset config: ${error.message}`))
      process.exit(1)
    }
  })
 
// Configure interactive setup
configCommand
  .command('setup')
  .description('Interactive configuration setup')
  .action(async () => {
    console.log(chalk.bold.cyan('āš™ļø Vaultace CLI Setup\n'))
    
    try {
      const answers = await inquirer.prompt([
        {
          type: 'input',
          name: 'apiUrl',
          message: 'Vaultace API URL:',
          default: 'https://api.vaultace.com',
          validate: (input) => {
            try {
              new URL(input)
              return true
            } catch {
              return 'Please enter a valid URL'
            }
          }
        },
        {
          type: 'list',
          name: 'defaultSeverity',
          message: 'Default minimum severity for scans:',
          choices: [
            { name: 'Low (show all vulnerabilities)', value: 'low' },
            { name: 'Medium (recommended)', value: 'medium' },
            { name: 'High (critical issues only)', value: 'high' },
            { name: 'Critical (only critical vulnerabilities)', value: 'critical' }
          ],
          default: 'medium'
        },
        {
          type: 'list',
          name: 'defaultFormat',
          message: 'Default output format:',
          choices: [
            { name: 'Table (human readable)', value: 'table' },
            { name: 'JSON (machine readable)', value: 'json' },
            { name: 'CSV (spreadsheet)', value: 'csv' },
            { name: 'SARIF (security tools)', value: 'sarif' }
          ],
          default: 'table'
        },
        {
          type: 'confirm',
          name: 'aiPatterns',
          message: 'Enable AI pattern detection by default?',
          default: true
        },
        {
          type: 'confirm',
          name: 'autoUpdate',
          message: 'Enable automatic CLI updates?',
          default: true
        }
      ])
      
      // Save configuration
      ConfigManager.set('apiUrl', answers.apiUrl)
      ConfigManager.set('defaults.severity', answers.defaultSeverity)
      ConfigManager.set('defaults.format', answers.defaultFormat)
      ConfigManager.set('defaults.aiPatterns', answers.aiPatterns)
      ConfigManager.set('preferences.autoUpdate', answers.autoUpdate)
      
      console.log(chalk.green('\nāœ… Configuration saved successfully!'))
      console.log(chalk.gray('Use vaultace auth login to authenticate'))
      
    } catch (error) {
      console.error(chalk.red(`Setup failed: ${error.message}`))
      process.exit(1)
    }
  })
 
function flattenConfig(obj, prefix = '') {
  let result = {}
  
  for (const key in obj) {
    const newKey = prefix ? `${prefix}.${key}` : key
    
    if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
      Object.assign(result, flattenConfig(obj[key], newKey))
    } else {
      result[newKey] = obj[key]
    }
  }
  
  return result
}
 
module.exports = configCommand