mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-01 06:13:10 +01:00
Turn provider setup into a class
This commit is contained in:
106
lib/cli.js
106
lib/cli.js
@@ -27,7 +27,7 @@ export default function() {
|
|||||||
program
|
program
|
||||||
.command('setup [provider]')
|
.command('setup [provider]')
|
||||||
.description('Sets up a new HomeBridge provider or re-configures an existing one.')
|
.description('Sets up a new HomeBridge provider or re-configures an existing one.')
|
||||||
.action(setupProvider);
|
.action((providerName, options) => new CliProviderSetup(providerName, options).setup());
|
||||||
|
|
||||||
// Parse options and execute HomeBridge
|
// Parse options and execute HomeBridge
|
||||||
program.parse(process.argv);
|
program.parse(process.argv);
|
||||||
@@ -57,62 +57,74 @@ function listInstalledProviders(options) {
|
|||||||
Provider.installed().forEach((provider) => console.log(provider.name));
|
Provider.installed().forEach((provider) => console.log(provider.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupProvider(providerName, options) {
|
// Encapsulates configuring a provider via the command line.
|
||||||
|
class CliProviderSetup {
|
||||||
|
constructor(providerName: string, options:object) {
|
||||||
|
|
||||||
// if you didn't specify a provider, print help
|
// if you didn't specify a provider, print help
|
||||||
if (!providerName) {
|
if (!providerName) {
|
||||||
log.error("You must specify the name of the provider to setup. Type 'homebridge providers' to list the providers currently installed.");
|
log.error("You must specify the name of the provider to setup. Type 'homebridge providers' to list the providers currently installed.");
|
||||||
program.help();
|
program.help();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.providerName = providerName;
|
||||||
|
this.options = options; // command-line options (currently none)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
setup() {
|
||||||
let provider = new Provider(providerName);
|
try {
|
||||||
let providerModule:object = provider.load({skipConfigCheck: true});
|
let provider = new Provider(this.providerName);
|
||||||
|
this.providerModule = provider.load({skipConfigCheck: true});
|
||||||
|
|
||||||
if (providerModule.config) {
|
if (this.providerModule.config) {
|
||||||
|
|
||||||
prompt.message = "";
|
prompt.message = "";
|
||||||
prompt.delimiter = "";
|
prompt.delimiter = "";
|
||||||
prompt.start();
|
prompt.start();
|
||||||
prompt.get(buildPromptSchema(providerName, providerModule.config), (err, result) => {
|
prompt.get(this.buildPromptSchema(), (err, result) => {
|
||||||
|
|
||||||
// apply configuration values entered by the user
|
// apply configuration values entered by the user
|
||||||
for (let key:string in result) {
|
for (let key:string in result) {
|
||||||
let value:object = result[key];
|
let value:object = result[key];
|
||||||
|
|
||||||
User.config.set(`${providerName}.${key}`, value);
|
User.config.set(`${this.providerName}.${key}`, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
providerModule.validateConfig();
|
this.validateProviderConfig();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.validateProviderConfig();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
catch (err) {
|
||||||
providerModule.validateConfig();
|
log.error(`Setup failed: ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (err) {
|
|
||||||
log.error(`Setup failed: ${err.message}`);
|
validateProviderConfig() {
|
||||||
|
this.providerModule.validateConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
// builds a "schema" obejct for the prompt lib based on the provider's config spec
|
||||||
|
buildPromptSchema(): object {
|
||||||
|
let properties = {};
|
||||||
|
|
||||||
|
for (let key:string in this.providerModule.config) {
|
||||||
|
let spec:object = this.providerModule.config[key];
|
||||||
|
|
||||||
|
// do we have a value for this config key currently?
|
||||||
|
let currentValue = User.config.get(`${this.providerName}.${key}`);
|
||||||
|
|
||||||
|
// copy over config spec with some modifications
|
||||||
|
properties[key] = {
|
||||||
|
description: `\n${spec.description}\n${camelCaseToRegularForm(key).white}:`,
|
||||||
|
type: spec.type,
|
||||||
|
required: spec.required,
|
||||||
|
default: currentValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { properties };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// builds a "schema" obejct for the prompt lib based on the provider's config spec
|
|
||||||
function buildPromptSchema(providerName: string, providerConfig: object): object {
|
|
||||||
let properties = {};
|
|
||||||
|
|
||||||
for (let key:string in providerConfig) {
|
|
||||||
let spec:object = providerConfig[key];
|
|
||||||
|
|
||||||
// do we have a value for this config key currently?
|
|
||||||
let currentValue = User.config.get(`${providerName}.${key}`);
|
|
||||||
|
|
||||||
// copy over config spec with some modifications
|
|
||||||
properties[key] = {
|
|
||||||
description: `\n${spec.description}\n${camelCaseToRegularForm(key).white}:`,
|
|
||||||
type: spec.type,
|
|
||||||
required: spec.required,
|
|
||||||
default: currentValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { properties };
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user