@@ -15,6 +15,7 @@ const { AgentManager } = require('./lib/AgentManager')
1515const { WebServer } = require ( './frontend/server' )
1616const ConfigLoader = require ( './lib/config' )
1717const webServer = new WebServer ( )
18+ const confirm = require ( '@inquirer/confirm' ) . default
1819
1920function main ( testOptions ) {
2021 const pkg = require ( './package.json' )
@@ -124,65 +125,83 @@ Please ensure the parent directory is writable, or set a different path with -d`
124125 info ( 'Device setup was successful' )
125126 info ( 'To start the Device Agent with the new configuration run the following command:' )
126127 info ( runCommandInfo . join ( ' ' ) )
127- quit ( )
128+ if ( ! options . otcDontStart ) {
129+ return confirm ( { message : 'Do you want to start the Device Agent now?' } )
130+ } else {
131+ quit ( )
132+ }
128133 } else {
129134 warn ( 'Device setup was unsuccessful' )
130135 quit ( null , 2 )
131136 }
137+ } ) . then ( ( startNow ) => {
138+ if ( startNow ) {
139+ info ( 'Starting Device Agent with new configuration' )
140+ delete options . otc
141+ delete options . ffUrl
142+ options . deviceFile = path . join ( options . dir , 'device.yml' )
143+ start ( options , true )
144+ } else {
145+ quit ( )
146+ }
132147 } ) . catch ( ( err ) => {
133148 quit ( err . message , 2 )
134149 } )
135150 return
136151 }
137152
138- info ( 'FlowFuse Device Agent' )
139- info ( '----------------------' )
153+ start ( options , configFound )
140154
141- if ( options . ui ) {
142- info ( 'Starting Web UI' )
143- if ( ! options . uiUser || ! options . uiPass ) {
144- quit ( 'Web UI cannot run without a username and password. These are set via with --ui-user and --ui-pass' , 2 )
145- }
146- const uiRuntime = Number ( options . uiRuntime )
147- if ( isNaN ( uiRuntime ) || uiRuntime === Infinity || uiRuntime < 0 ) {
148- quit ( 'Web UI runtime must be 0 or greater' , 2 )
149- }
150- const opts = {
151- port : options . uiPort || 1879 ,
152- host : options . uiHost || '0.0.0.0' ,
153- credentials : {
154- username : options . uiUser ,
155- password : options . uiPass
156- } ,
157- runtime : uiRuntime ,
158- dir : options . dir ,
159- config : options . config ,
160- deviceFile : options . deviceFile
155+ function start ( options , configFound ) {
156+ info ( 'FlowFuse Device Agent' )
157+ info ( '----------------------' )
158+
159+ if ( options . ui ) {
160+ info ( 'Starting Web UI' )
161+ if ( ! options . uiUser || ! options . uiPass ) {
162+ quit ( 'Web UI cannot run without a username and password. These are set via with --ui-user and --ui-pass' , 2 )
163+ }
164+ const uiRuntime = Number ( options . uiRuntime )
165+ if ( isNaN ( uiRuntime ) || uiRuntime === Infinity || uiRuntime < 0 ) {
166+ quit ( 'Web UI runtime must be 0 or greater' , 2 )
167+ }
168+ const opts = {
169+ port : options . uiPort || 1879 ,
170+ host : options . uiHost || '0.0.0.0' ,
171+ credentials : {
172+ username : options . uiUser ,
173+ password : options . uiPass
174+ } ,
175+ runtime : uiRuntime ,
176+ dir : options . dir ,
177+ config : options . config ,
178+ deviceFile : options . deviceFile
179+ }
180+ webServer . initialize ( AgentManager , opts )
181+ webServer . start ( ) . then ( ) . catch ( ( err ) => {
182+ info ( `Web UI failed to start: ${ err . message } ` )
183+ } )
161184 }
162- webServer . initialize ( AgentManager , opts )
163- webServer . start ( ) . then ( ) . catch ( ( err ) => {
164- info ( `Web UI failed to start: ${ err . message } ` )
165- } )
166- }
167185
168- process . on ( 'SIGINT' , closeAgentAndQuit )
169- process . on ( 'SIGTERM' , closeAgentAndQuit )
170- process . on ( 'SIGQUIT' , closeAgentAndQuit )
171-
172- const parsedConfig = configFound && ( ConfigLoader . parseDeviceConfigFile ( options . deviceFile ) || { valid : false } )
173- const isValidDeviceConfig = ! ! parsedConfig . valid
174-
175- if ( isValidDeviceConfig ) {
176- AgentManager . startAgent ( )
177- } else if ( configFound && options . ui === true ) {
178- info ( `Invalid config file '${ options . deviceFile } '.` )
179- } else if ( ! configFound && options . ui === true ) {
180- info ( `No config file found at '${ deviceFile1 } ' or '${ deviceFile2 } '` )
181- } else {
182- if ( configFound ) {
183- quit ( `Invalid config file '${ options . deviceFile } ': ${ parsedConfig ?. message || 'Unknown error' } '.` , 9 ) // Exit Code 9 - Invalid config file
186+ process . on ( 'SIGINT' , closeAgentAndQuit )
187+ process . on ( 'SIGTERM' , closeAgentAndQuit )
188+ process . on ( 'SIGQUIT' , closeAgentAndQuit )
189+
190+ const parsedConfig = configFound && ( ConfigLoader . parseDeviceConfigFile ( options . deviceFile ) || { valid : false } )
191+ const isValidDeviceConfig = ! ! parsedConfig . valid
192+
193+ if ( isValidDeviceConfig ) {
194+ AgentManager . startAgent ( )
195+ } else if ( configFound && options . ui === true ) {
196+ info ( `Invalid config file '${ options . deviceFile } '.` )
197+ } else if ( ! configFound && options . ui === true ) {
198+ info ( `No config file found at '${ deviceFile1 } ' or '${ deviceFile2 } '` )
184199 } else {
185- quit ( `No config file found at '${ deviceFile1 } ' or '${ deviceFile2 } '` , 2 ) // No such file or directory
200+ if ( configFound ) {
201+ quit ( `Invalid config file '${ options . deviceFile } ': ${ parsedConfig ?. message || 'Unknown error' } '.` , 9 ) // Exit Code 9 - Invalid config file
202+ } else {
203+ quit ( `No config file found at '${ deviceFile1 } ' or '${ deviceFile2 } '` , 2 ) // No such file or directory
204+ }
186205 }
187206 }
188207
0 commit comments