Last active
October 18, 2015 19:04
-
-
Save tcrowe/5134656a88a66bc8a85e to your computer and use it in GitHub Desktop.
create couchdb user programmatically through terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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