shell-variables
Command-line service to read/write variables between shell processes
Last updated 2 years ago by stjohnjohnson .
MIT · Original npm · Tarball · package.json
$ cnpm install shell-variables 
SYNC missed versions from official npm registry.

Shell Variable Tunnel

wercker status

Don't you just hate running multiple shell commands but not being able to properly read any data back other than exit codes and standard out? No? Really? I guess it's a problem most people don't have, but I ended up having. So I wrote this simple client/server system to be able to read/write variables from Node.JS to/from shell commands.

Install

shell-variables should be added to your codebase as a dependency. You can do this with:

$ npm install --save shell-variables

Alternatively you can manually add it to your package.json file:

{
  "dependencies" : {
    "shell-variables": "latest"
  }
}

then install with:

$ npm install

Usage

The purpose of this is to have a service available to all child processes so that they can read/write data. Here's a simple example where we write back to the Node.JS process:

var spawn = require('child_process').spawn,
    shellVariables = require('shell-variables'),
    server,
    spawnInstance;

server = new shellVariables.Server({
    foo: 'bar'
});

server.start(function () {
    // Set the "coconut" variable from the command-line
    spawnInstance = spawn('shell_variables.js', ['set', 'coconut', 'monkey'], {
        stdio: 'inherit',
        env: process.env
    });
    spawnInstance.on('close', function (code) {
        console.log('child process exited with code ' + code);
        console.log('value of coconut: ', server.get('coconut'));
        console.log('value of variables: ', server.variables);
        server.stop();
    });
});

Dot Notation

All variables read via the server, client, or cmd-line interface can be accessed at varying levels of depth via dot-notation. An example of this is here:

var obj = {
        foo: {
            bar: {
                baz: 15
            }
        }
    };
console.log(server.get('foo.bar.baz'));
// Outputs 15

Server

Creating a new server has two optional arguments:

  • existingVariables - Some existing object map
  • timeout - Timeout for the service in milliseconds

Four methods are available to use:

  • start(callback) - takes a callback that returns the URL of the service
  • stop(callback) - takes a callback that returns nothing really
  • get(field) - returns the value of that field (dot-notation)
  • set(field, value) - sets the value of that field (dot-notation)

Two variables are available as well:

  • variables - the current fields and values
  • url - the URL of the service

Client

Creating a client (if you wanted to do so inside a child node process) is easy as well. It has two optional arguments:

  • serverUrl - the URL of the server service (by default read from the environment)
  • timeout - Timeout for the service in milliseconds

Two methods are available to use:

  • get(field) - returns the value of that field (dot-notation)
  • set(field, value) - sets the value of that field (dot-notation)

Command-line tool

The command-line is just as easy as the client if not more as it includes its own documentation:

$ shell_variables.js

Usage: shell_variables.js <command> [options]

command
  get     retrieve data from the variable tunnel
  set     write data to the variable tunnel (accepts pipes)

Options:
   --server    URL of the server, usually $(VARIABLE_TUNNEL_URL)
   --timeout   Timeout in milliseconds  [5000]
   --json      Return everything in JSON format  [false]

Get

$ shell_variables.js get

shell_variables.js get <field> [options]

field     dot-notation field to retrieve value

Options:
   --server    URL of the server, usually $(VARIABLE_TUNNEL_URL)
   --timeout   Timeout in milliseconds  [5000]
   --json      Return everything in JSON format  [false]

Set

$ shell_variables.js set

Usage: shell_variables.js set <field> [value] [options]

field     dot-notation field to write values to
value     value to set (if not provided, reads from STDIN)

Options:
   --server    URL of the server, usually $(VARIABLE_TUNNEL_URL)
   --timeout   Timeout in milliseconds  [5000]
   --json      Return everything in JSON format  [false]
   --format    Force the set value into specific formats (json, boolean, float, integer, string)  [string]

License

MIT © St. John Johnson

Current Tags

  • 1.3.1                                ...           latest (2 years ago)

7 Versions

  • 1.3.1                                ...           2 years ago
  • 1.3.0                                ...           3 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (5)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |