Skip to content

Commit

Permalink
Add shell completion for bash
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 1, 2017
1 parent 6e48698 commit 0610dc1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Medic Project Configurer

npm install -g medic-configurer-beta
python -m pip install -e git+https://github.com/alxndrsn/pyxform.git@master#egg=medic-pyxform
eval "$(medic-conf --shell-completion bash)"

# Use

Expand Down Expand Up @@ -88,7 +89,6 @@ This tool expects a project to be sctructured as follows:

# TODO

* add bash tab-completion
* only upload things which have changed (this could be a separate mode - e.g. `update` vs `configure`)
* require manual confirmation of upload if url is *.app.medicmobile.org and either git is not available, or the working directory is dirty or has new files
* move `npm publish` to travis and rename module to `medic-configurer`
Expand Down
35 changes: 23 additions & 12 deletions bin/medic-conf.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
#!/usr/bin/env node

const error = require('../src/lib/log').error;
const fs = require('../src/lib/sync-fs');
const info = require('../src/lib/log').info;
const path = require('path');
const usage = require('../src/cli/usage');
const supportedActions = require('../src/cli/supported-actions');

const args = process.argv.slice(2);
if(args.length === 1) {
switch(args[0]) {
case '--help': usage(); return process.exit(0);
case '--version':
console.log(require('../package.json').version);

switch(args[0]) {
case '--help': return usage(0);
case '--shell-completion':
const shell = args.length > 1 && args[1] || 'bash';
const completionFile = `${fs.path.dirname(require.main.filename)}/../src/shell-completion.${shell}`;
if(fs.exists(completionFile)) {
console.log(fs.read(completionFile));
process.exit(0);
}
}
if(args.length < 2) {
usage();
process.exit(1);
} else {
console.log('# ERROR medic-conf shell completion not yet supported for', shell);
process.exit(1);
}
return;
case '--supported-actions':
console.log('Supported actions:\n ', supportedActions.join('\n '));
return process.exit(0);
case '--version':
console.log(require('../package.json').version);
return process.exit(0);
}

const project = path.normalize(args[0]).replace(/\/$/, '');
if(args.length < 2) return usage(1);

const project = fs.path.normalize(args[0]).replace(/\/$/, '');
const instanceUrl = args[1];
const couchUrl = `${instanceUrl}/medic`;

Expand Down
20 changes: 20 additions & 0 deletions bin/shell-completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const cur = process.argv[3] || '';
const idx = Number.parseInt(process.argv[2]);

let options = [];

if(idx === 1 && cur.match(/^-/)) {
options = [ '--help', '--shell-completion', '--supported-actions', '--version' ];
}

if(idx === 2) {
options = ['http\\://', 'https\\://'];
}

if(idx >= 3) {
options = require('../src/cli/supported-actions');
}

console.log(...options);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "medic-configurer-beta",
"version": "1.0.34",
"version": "1.0.35",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
"test": "grunt jshint test"
},
"bin": {
"medic-conf": "bin/medic-conf.js",
"medic-conf-shell-complete": "bin/medic-conf-shell-complete.js",
"medic-pngout": "bin/pngout"
},
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions src/cli/shell-completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

completions() {
local current_word="${COMP_WORDS[COMP_CWORD]}"

options="$(medic-conf-shell-completion $COMP_CWORD $current_word)"

# Tell complete what stuff to show.
COMPREPLY=($(compgen -o dirnames -W "$options" -- "$current_word"))
}

complete -o dirnames -F completions medic-conf
4 changes: 3 additions & 1 deletion src/cli/usage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const supportedActions = require('./supported-actions');

module.exports = () => {
module.exports = exitCode => {
console.log(`
# This script will update and upload all of a particular project configuration
# to a particular instance.
Expand All @@ -18,4 +18,6 @@ Or:
Supported actions:
* ${supportedActions.join('\n * ')}
`);

process.exit(exitCode);
};

0 comments on commit 0610dc1

Please sign in to comment.