Skip to content

Commit

Permalink
cli: Load the Airbitz config file
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Dec 2, 2016
1 parent faadf2a commit 0bbb6d2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
"node-getopt": "^0.2.3",
"node-localstorage": "^1.3.0",
"scryptsy": "^1.2.1",
"xdg-basedir": "^2.0.0",
"xmlhttprequest": "^1.4.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Expand Up @@ -5,6 +5,7 @@ export default {
entry: 'src/abc.js',
external: [
'assert',
'fs',
'url'
].concat(Object.keys(packageJson.dependencies)),
plugins: [
Expand Down
83 changes: 57 additions & 26 deletions src/cli/node/index.js
@@ -1,7 +1,9 @@
// Command-line tools:
import chalk from 'chalk'
import fs from 'fs'
import Getopt from 'node-getopt'
import {LocalStorage} from 'node-localstorage'
import xdgBasedir from 'xdg-basedir'

// Airbitz context stuff:
import * as abc from '../../abc.js'
Expand All @@ -15,6 +17,7 @@ import '../commands/all.js'
const getopt = new Getopt([
['k', 'api-key=ARG', 'Auth server API key'],
['a', 'account-type=ARG', 'Account type'],
['c', 'config=ARG', 'Configuration file'],
['d', 'directory=ARG', 'Working directory'],
['u', 'username=ARG', 'Username'],
['p', 'password=ARG', 'Password'],
Expand Down Expand Up @@ -42,50 +45,77 @@ const helpCommand = command('help', {
}
})

/**
* Loads the config file,
* and returns its contents merged with the command-line options.
*/
function loadConfig (options) {
let config = {}

// Load the config file (if possible):
if (options['config'] || xdgBasedir.config) {
const path = options['config'] ||
xdgBasedir.config + '/airbitz/airbitz.conf'
try {
config = JSON.parse(fs.readFileSync(path, 'utf8'))
} catch (e) {
if (options['config']) {
const e = new Error(`Cannot load config file '${path}'`)
e.quiet = true
throw e
}
}
}

// Calculate the active settings:
return {
accountType: options['account-type'],
apiKey: options['api-key'] || config['apiKey'],
directory: options['directory'] || config['workingDir'],
username: options['username'] || config['username'],
password: options['password'] || config['password']
}
}

/**
* Sets up a session object with the Airbitz objects
* needed by the command.
* @return a promise
*/
function makeSession (options, cmd) {
function makeSession (config, cmd) {
const session = {}
let out = Promise.resolve(session)

// Create a context if we need one:
if (cmd.needsContext) {
// API key:
let apiKey = options['api-key']
if (!apiKey) {
try {
apiKey = require('./apiKey.js').apiKey
} catch (e) {
throw cmd.usageError('No API key')
}
if (!config.apiKey) {
throw cmd.usageError('No API key')
}
const fakeStorage = new LocalStorage(options['directory'] || './.cli')
const fakeStorage = new LocalStorage(config.directory || './.cli')
session.context = new abc.Context({
accountType: options['account-type'],
apiKey: apiKey,
authRequest: realServer.makeAuthRequest(apiKey),
accountType: config.accountType,
apiKey: config.apiKey,
authRequest: realServer.makeAuthRequest(config.apiKey),
localStorage: fakeStorage
})
}

// Create a login if we need one:
if (cmd.needsLogin) {
out = out.then(session => {
if (options['username'] && options['password']) {
return new Promise((resolve, reject) => {
session.context.loginWithPassword(options['username'], options['password'], null, {}, (err, account) => {
if (err) return reject(err)
session.account = account
session.login = account.login
resolve(session)
})
})
} else {
if (!config.username || !config.password) {
throw cmd.usageError('No login credentials')
}

return new Promise((resolve, reject) => {
session.context.loginWithPassword(config.username, config.password, null, {}, (err, account) => {
if (err) return reject(err)
session.account = account
session.login = account.login
resolve(session)
})
})
})
}

Expand Down Expand Up @@ -118,11 +148,12 @@ function main () {
? helpCommand
: command.find(opt.argv.shift())

// Set up the session:
const session = makeSession(opt.options, cmd)
// Load the config file:
const config = loadConfig(opt.options)

// Invoke the command:
session.then(session => {
// Set up the session:
makeSession(config, cmd).then(session => {
// Invoke the command:
return cmd.invoke(session, opt.argv)
}).catch(e => {
reportError(e)
Expand Down

0 comments on commit 0bbb6d2

Please sign in to comment.