"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/cli/commands/config.ts" (20 Feb 2024, 1476 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as snyk from '../../lib';
    2 
    3 const validMethods = ['set', 'get', 'unset', 'clear'];
    4 
    5 export default async function config(
    6   method?: 'set' | 'get' | 'unset' | 'clear',
    7   ...args: string[]
    8 ): Promise<string> {
    9   if (method && !validMethods.includes(method)) {
   10     throw new Error(`Unknown config command "${method}"`);
   11   }
   12 
   13   const key = args[0];
   14 
   15   if (method === 'set') {
   16     let res = '';
   17 
   18     args.forEach((item) => {
   19       const [key, val] = item.split(/=(.+)/);
   20       snyk.config.set(key, val);
   21       res += key + ' updated\n';
   22 
   23       // ensure we update the live library
   24       if (key === 'api') {
   25         (snyk as any).api = val;
   26       }
   27     });
   28 
   29     return res;
   30   }
   31 
   32   if (method === 'get') {
   33     if (!key) {
   34       throw new Error('config:get requires an argument');
   35     }
   36 
   37     return snyk.config.get(key) || '';
   38   }
   39 
   40   if (method === 'unset') {
   41     if (!key) {
   42       throw new Error('config:unset requires an argument');
   43     }
   44     snyk.config.delete(key);
   45 
   46     if (key === 'api') {
   47       // ensure we update the live library
   48       (snyk as any).api = null;
   49     }
   50 
   51     return `${key} deleted`;
   52   }
   53 
   54   if (method === 'clear') {
   55     snyk.config.clear();
   56     // ensure we update the live library
   57     (snyk as any).api = null;
   58     return 'config cleared';
   59   }
   60 
   61   return Object.keys(snyk.config.all)
   62     .sort((a, b) => Number(a.toLowerCase() < b.toLowerCase()))
   63     .map((configKey) => `${configKey}: ${snyk.config.all[configKey]}`)
   64     .join('\n')
   65     .trim();
   66 }