Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Last active October 18, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/5134656a88a66bc8a85e to your computer and use it in GitHub Desktop.
Save tcrowe/5134656a88a66bc8a85e to your computer and use it in GitHub Desktop.
create couchdb user programmatically through terminal
'use strict';
var inquirer = require('inquirer'),
chalk = require('chalk'),
nano = require('nano'),
questions;
questions = [
{
type: 'list',
name: 'protocol',
message: 'protocol',
default: 'https',
choices: [
'https',
'http'
]
},
{
type: 'input',
name: 'host',
message: 'host or ip',
'default': '127.0.0.1'
},
{
type: 'input',
name: 'port',
message: 'port',
'default': 5984
},
{
type: 'input',
name: 'adminUsername',
message: 'admin username',
'default': process.env.USER
},
{
type: 'password',
name: 'adminPassword',
message: 'admin password'
},
{
type: 'input',
name: 'username',
message: 'new username'
},
{
type: 'password',
name: 'password',
message: 'new password'
}
];
console.log('------------------------');
console.log(' ' + chalk.blue('create couchdb user'));
console.log('------------------------');
function insertUserResponse(err, body) {
if (err !== null) {
console.log(chalk.red('⨯'), 'user document insert error');
return console.dir(err);
}
console.log(chalk.green('✓'), 'user created');
console.log('body', body);
}
function answersResponse(answers) {
var url,
docId,
doc,
db;
url = [
answers.protocol,
'://',
answers.adminUsername,
':',
answers.adminPassword,
'@',
answers.host,
':',
answers.port,
'/_users'
];
url = url.join('');
docId = 'org.couchdb.user:' + answers.username;
doc = {
name: answers.username,
password: answers.password,
roles: [],
type: 'user'
};
db = nano(url);
db.insert(doc, docId, insertUserResponse);
}
inquirer.prompt(questions, answersResponse);
install dependencies:
npm install inquirer chalk nano
the output will look something like this but with colors:
tcrowe:tools/ $ node create-couchdb-user.js
------------------------
create couchdb user
------------------------
? protocol http
? host or ip 127.0.0.1
? port 5984
? admin username tcrowe
? admin password ***********
? new username new-user-test
? new password **************
✓ user created
body { ok: true,
id: 'org.couchdb.user:new-user-test',
rev: '1-00a0bbd610aa9480f454342ab8a99aaa' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment