2017-06-08 10:52:47 +00:00
// Generated by CoffeeScript 1.12.6
2016-01-11 19:58:35 +00:00
/ *
2017-06-14 21:20:15 +00:00
Copyright 2016 - 2017 Resin . io
2016-01-11 19:58:35 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2017-03-22 09:46:06 +00:00
var commandOptions ;
2016-01-11 19:58:35 +00:00
2017-03-22 09:46:06 +00:00
commandOptions = require ( './command-options' ) ;
2016-03-21 19:42:54 +00:00
2017-03-22 09:46:06 +00:00
exports . read = {
signature : 'config read' ,
description : 'read a device configuration' ,
help : 'Use this command to read the config.json file from the mounted filesystem (e.g. SD card) of a provisioned device"\n\nExamples:\n\n $ resin config read --type raspberry-pi\n $ resin config read --type raspberry-pi --drive /dev/disk2' ,
options : [
{
signature : 'type' ,
2017-05-12 10:01:37 +00:00
description : 'device type (Check available types with `resin devices supported`)' ,
2017-03-22 09:46:06 +00:00
parameter : 'type' ,
alias : 't' ,
required : 'You have to specify a device type'
} , {
signature : 'drive' ,
description : 'drive' ,
parameter : 'drive' ,
alias : 'd'
2015-11-10 18:27:01 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
permission : 'user' ,
root : true ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , config , prettyjson , umountAsync , visuals ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
config = require ( 'resin-config-json' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2017-03-27 09:14:55 +00:00
umountAsync = Promise . promisify ( require ( 'umount' ) . umount ) ;
2017-03-22 09:46:06 +00:00
prettyjson = require ( 'prettyjson' ) ;
return Promise [ "try" ] ( function ( ) {
return options . drive || visuals . drive ( 'Select the device drive' ) ;
2017-03-27 09:14:55 +00:00
} ) . tap ( umountAsync ) . then ( function ( drive ) {
2017-03-22 09:46:06 +00:00
return config . read ( drive , options . type ) ;
} ) . tap ( function ( configJSON ) {
return console . info ( prettyjson . render ( configJSON ) ) ;
} ) . nodeify ( done ) ;
}
} ;
2015-11-10 18:27:01 +00:00
2017-03-22 09:46:06 +00:00
exports . write = {
signature : 'config write <key> <value>' ,
description : 'write a device configuration' ,
help : 'Use this command to write the config.json file to the mounted filesystem (e.g. SD card) of a provisioned device\n\nExamples:\n\n $ resin config write --type raspberry-pi username johndoe\n $ resin config write --type raspberry-pi --drive /dev/disk2 username johndoe\n $ resin config write --type raspberry-pi files.network/settings "..."' ,
options : [
{
signature : 'type' ,
2017-05-12 10:01:37 +00:00
description : 'device type (Check available types with `resin devices supported`)' ,
2017-03-22 09:46:06 +00:00
parameter : 'type' ,
alias : 't' ,
required : 'You have to specify a device type'
} , {
signature : 'drive' ,
description : 'drive' ,
parameter : 'drive' ,
alias : 'd'
2015-11-10 16:53:34 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
permission : 'user' ,
root : true ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , _ , config , umountAsync , visuals ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
_ = require ( 'lodash' ) ;
config = require ( 'resin-config-json' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2017-03-27 09:14:55 +00:00
umountAsync = Promise . promisify ( require ( 'umount' ) . umount ) ;
2017-03-22 09:46:06 +00:00
return Promise [ "try" ] ( function ( ) {
return options . drive || visuals . drive ( 'Select the device drive' ) ;
2017-03-27 09:14:55 +00:00
} ) . tap ( umountAsync ) . then ( function ( drive ) {
2017-03-22 09:46:06 +00:00
return config . read ( drive , options . type ) . then ( function ( configJSON ) {
console . info ( "Setting " + params . key + " to " + params . value ) ;
_ . set ( configJSON , params . key , params . value ) ;
return configJSON ;
2016-03-17 20:07:19 +00:00
} ) . tap ( function ( ) {
2017-03-27 09:14:55 +00:00
return umountAsync ( drive ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( configJSON ) {
return config . write ( drive , options . type , configJSON ) ;
} ) ;
} ) . tap ( function ( ) {
return console . info ( 'Done' ) ;
} ) . nodeify ( done ) ;
}
} ;
exports . inject = {
signature : 'config inject <file>' ,
description : 'inject a device configuration file' ,
help : 'Use this command to inject a config.json file to the mounted filesystem (e.g. SD card) of a provisioned device"\n\nExamples:\n\n $ resin config inject my/config.json --type raspberry-pi\n $ resin config inject my/config.json --type raspberry-pi --drive /dev/disk2' ,
options : [
{
signature : 'type' ,
2017-05-12 10:01:37 +00:00
description : 'device type (Check available types with `resin devices supported`)' ,
2017-03-22 09:46:06 +00:00
parameter : 'type' ,
alias : 't' ,
required : 'You have to specify a device type'
} , {
signature : 'drive' ,
description : 'drive' ,
parameter : 'drive' ,
alias : 'd'
2016-03-17 20:07:19 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
permission : 'user' ,
root : true ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , config , readFileAsync , umountAsync , visuals ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
config = require ( 'resin-config-json' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2017-03-27 09:14:55 +00:00
umountAsync = Promise . promisify ( require ( 'umount' ) . umount ) ;
readFileAsync = Promise . promisify ( require ( 'fs' ) . readFile ) ;
2017-03-22 09:46:06 +00:00
return Promise [ "try" ] ( function ( ) {
return options . drive || visuals . drive ( 'Select the device drive' ) ;
2017-03-27 09:14:55 +00:00
} ) . tap ( umountAsync ) . then ( function ( drive ) {
return readFileAsync ( params . file , 'utf8' ) . then ( JSON . parse ) . then ( function ( configJSON ) {
2017-03-22 09:46:06 +00:00
return config . write ( drive , options . type , configJSON ) ;
} ) ;
} ) . tap ( function ( ) {
return console . info ( 'Done' ) ;
} ) . nodeify ( done ) ;
}
} ;
2016-03-17 20:07:19 +00:00
2017-03-22 09:46:06 +00:00
exports . reconfigure = {
signature : 'config reconfigure' ,
description : 'reconfigure a provisioned device' ,
help : 'Use this command to reconfigure a provisioned device\n\nExamples:\n\n $ resin config reconfigure --type raspberry-pi\n $ resin config reconfigure --type raspberry-pi --advanced\n $ resin config reconfigure --type raspberry-pi --drive /dev/disk2' ,
options : [
{
signature : 'type' ,
2017-05-12 10:01:37 +00:00
description : 'device type (Check available types with `resin devices supported`)' ,
2017-03-22 09:46:06 +00:00
parameter : 'type' ,
alias : 't' ,
required : 'You have to specify a device type'
} , {
signature : 'drive' ,
description : 'drive' ,
parameter : 'drive' ,
alias : 'd'
} , {
signature : 'advanced' ,
description : 'show advanced commands' ,
boolean : true ,
alias : 'v'
2015-11-11 14:38:45 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
permission : 'user' ,
root : true ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , capitanoRunAsync , config , umountAsync , visuals ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
config = require ( 'resin-config-json' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2017-03-27 09:14:55 +00:00
capitanoRunAsync = Promise . promisify ( require ( 'capitano' ) . run ) ;
umountAsync = Promise . promisify ( require ( 'umount' ) . umount ) ;
2017-03-22 09:46:06 +00:00
return Promise [ "try" ] ( function ( ) {
return options . drive || visuals . drive ( 'Select the device drive' ) ;
2017-03-27 09:14:55 +00:00
} ) . tap ( umountAsync ) . then ( function ( drive ) {
2017-03-22 09:46:06 +00:00
return config . read ( drive , options . type ) . get ( 'uuid' ) . tap ( function ( ) {
2017-03-27 09:14:55 +00:00
return umountAsync ( drive ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( uuid ) {
var configureCommand ;
configureCommand = "os configure " + drive + " " + uuid ;
if ( options . advanced ) {
configureCommand += ' --advanced' ;
}
2017-03-27 09:14:55 +00:00
return capitanoRunAsync ( configureCommand ) ;
2017-03-22 09:46:06 +00:00
} ) ;
} ) . then ( function ( ) {
return console . info ( 'Done' ) ;
} ) . nodeify ( done ) ;
}
} ;
2015-11-11 14:38:45 +00:00
2017-03-22 09:46:06 +00:00
exports . generate = {
signature : 'config generate' ,
description : 'generate a config.json file' ,
help : 'Use this command to generate a config.json for a device or application\n\nExamples:\n\n $ resin config generate --device 7cf02a6\n $ resin config generate --device 7cf02a6 --output config.json\n $ resin config generate --app MyApp\n $ resin config generate --app MyApp --output config.json' ,
options : [
commandOptions . optionalApplication , commandOptions . optionalDevice , {
signature : 'output' ,
description : 'output' ,
parameter : 'output' ,
alias : 'o'
}
] ,
permission : 'user' ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , _ , deviceConfig , form , prettyjson , resin , writeFileAsync ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
2017-03-27 09:14:55 +00:00
writeFileAsync = Promise . promisify ( require ( 'fs' ) . writeFile ) ;
2017-03-22 09:46:06 +00:00
resin = require ( 'resin-sdk-preconfigured' ) ;
_ = require ( 'lodash' ) ;
form = require ( 'resin-cli-form' ) ;
deviceConfig = require ( 'resin-device-config' ) ;
prettyjson = require ( 'prettyjson' ) ;
if ( ( options . device == null ) && ( options . application == null ) ) {
throw new Error ( 'You have to pass either a device or an application.\n\nSee the help page for examples:\n\n $ resin help config generate' ) ;
}
return Promise [ "try" ] ( function ( ) {
if ( options . device != null ) {
return resin . models . device . get ( options . device ) ;
2016-03-21 19:42:54 +00:00
}
2017-03-22 09:46:06 +00:00
return resin . models . application . get ( options . application ) ;
} ) . then ( function ( resource ) {
return resin . models . device . getManifestBySlug ( resource . device _type ) . get ( 'options' ) . then ( form . run ) . then ( function ( answers ) {
if ( resource . uuid != null ) {
return deviceConfig . getByDevice ( resource . uuid , answers ) ;
2016-03-21 19:42:54 +00:00
}
2017-03-22 09:46:06 +00:00
return deviceConfig . getByApplication ( resource . app _name , answers ) ;
} ) ;
} ) . then ( function ( config ) {
if ( options . output != null ) {
2017-03-27 09:14:55 +00:00
return writeFileAsync ( options . output , JSON . stringify ( config ) ) ;
2017-03-22 09:46:06 +00:00
}
return console . log ( prettyjson . render ( config ) ) ;
} ) . nodeify ( done ) ;
}
} ;