Skip to content

Commit

Permalink
--save and windows fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bahamas10 committed Jan 16, 2014
1 parent 0fef943 commit 719728a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Usage
-H, --host the hostname or ip of the bridge to control
-i, --init initialize the config file at ~/.hue.json
-j, --json force output to be in json
-s, --save save the config file at ~/.hue.json, same as --init
-u, --updates check for available updates
-v, --version print the version number and exit

Expand Down Expand Up @@ -113,11 +114,13 @@ Before we continue, let's create a configuration file. In the file we can
set the default host to connect to, so we don't have to keep supplying the
`-H` argument. Run:

$ hue --init
$ hue --host 10.0.1.218 --save
config file written to `~/.hue.json`
{
"host": "10.0.1.218"
}

Now, modify that file and replace `null` with `"10.0.1.218"`, or whatever
your IP or hostname is. Now we'll no longer have to supply the `-H` argument
Now we'll no longer have to supply the `-H` argument
with every command.

From here, we can get information about a single light like:
Expand Down
20 changes: 12 additions & 8 deletions hue-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ function printf() { console.log(sprintf.apply(this, arguments)); }
var package = require('./package.json');

var app = 'node-hue-cli';
var homedir = process.env.HOME || process.env.USERPROFILE;
var configfile = path.join(process.env.HOME, '.hue.json');
var config;
try {
config = require(configfile);
config = JSON.parse(fs.readFileSync(configfile, 'utf-8'));
} catch (e) {
config = {host: null};
}
Expand Down Expand Up @@ -72,6 +73,7 @@ function usage() {
' -H, --host the hostname or ip of the bridge to control',
' -i, --init initialize the config file at ' + configfile,
' -j, --json force output to be in json',
' -s, --save save the config file at ' + configfile + ', same as --init',
' -u, --updates check for available updates',
' -v, --version print the version number and exit'
].join('\n');
Expand All @@ -83,21 +85,23 @@ var options = [
'H:(host)',
'i(init)',
'j(json)',
's(save)',
'u(updates)',
'v(version)'
].join('');
var parser = new getopt.BasicParser(options, process.argv);

var option;
var json = false;
var huehost = config.host;
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'h': console.log(usage()); process.exit(0);
case 'H': huehost = option.optarg; break;
case 'i':
fs.writeFileSync(configfile, JSON.stringify(config, null, 2));
case 'H': config.host = option.optarg; break;
case 'i': case 's':
var s = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, s + '\n');
console.log('config file written to `%s`', configfile);
console.log(s);
process.exit(0);
case 'j': json = true; break;
case 'u': // check for updates
Expand Down Expand Up @@ -240,7 +244,7 @@ switch (args[0]) {
console.log('please go and press the link button on your base station');
client.register(function(err) {
if (err) {
console.error('failed to pair to Hue Base Station %s', huehost);
console.error('failed to pair to Hue Base Station %s', config.host);
throw err;
}
console.log('Hue Base Station paired!')
Expand Down Expand Up @@ -270,7 +274,7 @@ switch (args[0]) {

// wrapper around get client to error on failure
function getclient() {
if (!huehost) {
if (!config.host) {
console.error([
'error: host not set',
'',
Expand All @@ -282,7 +286,7 @@ function getclient() {

// create the client
var client = Hue.createClient({
stationIp: huehost,
stationIp: config.host,
appName: app
});
return client;
Expand Down

0 comments on commit 719728a

Please sign in to comment.