11#!/usr/bin/env node
22
3+ import * as readline from 'node:readline/promises' ;
4+ import { stdin as input , stdout as output } from 'node:process' ;
5+
36import yargs from 'yargs' ;
47import { hideBin } from 'yargs/helpers' ;
58
69import {
710 getConfig , updateConfig , GLOBAL_CONFIG , PROJECT_CONFIG , LOCAL_CONFIG
811} from '../lib/config.js' ;
912import { setVerbosityFromEnv } from '../lib/verbosity.js' ;
13+ import { runSync } from '../lib/run.js' ;
1014
1115setVerbosityFromEnv ( ) ;
1216
1317const args = yargs ( hideBin ( process . argv ) )
1418 . completion ( 'completion' )
1519 . command ( {
16- command : 'set <key> <value>' ,
20+ command : 'set <key> [ <value>] ' ,
1721 desc : 'Set a config variable' ,
1822 builder : ( yargs ) => {
1923 yargs
24+ . option ( 'encrypt' , {
25+ describe : 'Store the value encrypted using gpg' ,
26+ alias : 'x' ,
27+ type : 'boolean'
28+ } )
2029 . positional ( 'key' , {
2130 describe : 'key of the configuration' ,
2231 type : 'string'
@@ -61,8 +70,6 @@ const args = yargs(hideBin(process.argv))
6170 . conflicts ( 'global' , 'project' )
6271 . help ( ) ;
6372
64- const argv = args . parse ( ) ;
65-
6673function getConfigType ( argv ) {
6774 if ( argv . global ) {
6875 return { configName : 'global' , configType : GLOBAL_CONFIG } ;
@@ -73,9 +80,21 @@ function getConfigType(argv) {
7380 return { configName : 'local' , configType : LOCAL_CONFIG } ;
7481}
7582
76- function setHandler ( argv ) {
83+ async function setHandler ( argv ) {
7784 const { configName, configType } = getConfigType ( argv ) ;
7885 const config = getConfig ( configType ) ;
86+ if ( ! argv . value ) {
87+ const rl = readline . createInterface ( { input, output } ) ;
88+ argv . value = await rl . question ( 'What value do you want to set? ' ) ;
89+ rl . close ( ) ;
90+ } else if ( argv . encrypt ) {
91+ console . warn ( 'Passing sensitive config value via the shell is discouraged' ) ;
92+ }
93+ if ( argv . encrypt ) {
94+ argv . value = runSync ( 'gpg' , [ '--default-recipient-self' , '--encrypt' , '--armor' ] , {
95+ input : argv . value
96+ } ) ;
97+ }
7998 console . log (
8099 `Updating ${ configName } configuration ` +
81100 `[${ argv . key } ]: ${ config [ argv . key ] } -> ${ argv . value } ` ) ;
@@ -96,6 +115,8 @@ function listHandler(argv) {
96115 }
97116}
98117
118+ const argv = await args . parse ( ) ;
119+
99120if ( ! [ 'get' , 'set' , 'list' ] . includes ( argv . _ [ 0 ] ) ) {
100121 args . showHelp ( ) ;
101122}
0 commit comments