@@ -3,12 +3,65 @@ const _ = require('lodash');
3
3
const got = require ( 'got' ) ;
4
4
const chalk = require ( 'chalk' ) ;
5
5
const ora = require ( 'ora' ) ;
6
+ const inquirer = require ( 'inquirer' ) ;
6
7
7
8
// our packages
8
9
const { userConfig, isLoggedIn, logout} = require ( '../config' ) ;
9
10
10
11
// valid targets list
11
- const validTargets = [ 'traefik' , 'server' ] ;
12
+ const validTargets = [ 'traefik' , 'server' , 'all' ] ;
13
+ // construct shared request params
14
+ const options = {
15
+ headers : {
16
+ Authorization : `Bearer ${ userConfig . token } ` ,
17
+ } ,
18
+ json : true ,
19
+ } ;
20
+
21
+ const runUpdate = async target => {
22
+ console . log ( chalk . bold ( `Updating ${ target } on:` ) , userConfig . endpoint ) ;
23
+
24
+ // services request url
25
+ const remoteUrl = `${ userConfig . endpoint } /update/${ target } ` ;
26
+ // try sending request
27
+ try {
28
+ const { body, statusCode} = await got . post ( remoteUrl , options ) ;
29
+ if ( statusCode !== 200 || body . error ) {
30
+ throw new Error ( body . error || 'Oops. Something went wrong! Try again please.' ) ;
31
+ }
32
+
33
+ if ( body . updated ) {
34
+ console . log ( chalk . green ( `Successfully updated ${ target } !` ) ) ;
35
+ return ;
36
+ }
37
+
38
+ console . log ( chalk . green ( `${ _ . capitalize ( target ) } is already up to date!` ) ) ;
39
+ } catch ( e ) {
40
+ // if authorization is expired/broken/etc
41
+ if ( e . statusCode === 401 ) {
42
+ logout ( userConfig ) ;
43
+ console . log ( chalk . red ( 'Error: authorization expired!' ) , 'Please, relogin and try again.' ) ;
44
+ return ;
45
+ }
46
+
47
+ const reason = e . response . body && e . response . body . error ? e . response . body . error : e . toString ( ) ;
48
+ console . log ( chalk . red ( `Error updating ${ target } :` ) , reason ) ;
49
+ console . log ( 'Update log:\n' ) ;
50
+ ( e . response . body . log || 'No log available' )
51
+ . split ( '\n' )
52
+ . map ( l => {
53
+ try {
54
+ return JSON . parse ( l ) ;
55
+ } catch ( e ) {
56
+ return l ;
57
+ }
58
+ } )
59
+ . filter ( l => l !== undefined )
60
+ . map ( l => l . trim ( ) )
61
+ . filter ( l => l && l . length > 0 )
62
+ . forEach ( line => console . log ( line ) ) ;
63
+ }
64
+ } ;
12
65
13
66
exports . command = [ 'update [target]' ] ;
14
67
exports . describe = 'check for updates or update given target' ;
@@ -23,14 +76,6 @@ exports.handler = async ({target}) => {
23
76
return ;
24
77
}
25
78
26
- // construct shared request params
27
- const options = {
28
- headers : {
29
- Authorization : `Bearer ${ userConfig . token } ` ,
30
- } ,
31
- json : true ,
32
- } ;
33
-
34
79
// if no target given - check for update
35
80
if ( ! target || ! target . length ) {
36
81
// show loader
@@ -60,7 +105,44 @@ exports.handler = async ({target}) => {
60
105
console . log ( chalk . bold ( 'Traefik:' ) ) ;
61
106
console . log ( ` current: ${ body . traefik } ` ) ;
62
107
console . log ( ` latest: ${ body . latestTraefik } ` ) ;
63
- return ;
108
+ console . log ( ) ;
109
+
110
+ // if updates are available - ask user if he want them immediately
111
+ if ( ! body . serverUpdate && ! body . traefikUpdate ) {
112
+ return ;
113
+ }
114
+
115
+ const prompts = [ ] ;
116
+ if ( body . serverUpdate ) {
117
+ prompts . push ( {
118
+ type : 'confirm' ,
119
+ name : 'upServer' ,
120
+ message : 'Update server now?' ,
121
+ default : true ,
122
+ } ) ;
123
+ }
124
+
125
+ if ( body . traefikUpdate ) {
126
+ prompts . push ( {
127
+ type : 'confirm' ,
128
+ name : 'upTraefik' ,
129
+ message : 'Update Traefik now?' ,
130
+ default : true ,
131
+ } ) ;
132
+ }
133
+ const { upServer, upTraefik} = await inquirer . prompt ( prompts ) ;
134
+ // if user doesn't want update - just exit
135
+ if ( ! upServer && ! upTraefik ) {
136
+ return ;
137
+ }
138
+ // define target based on user input
139
+ if ( upServer && upTraefik ) {
140
+ target = 'all' ;
141
+ } else if ( upServer ) {
142
+ target = 'server' ;
143
+ } else if ( upTraefik ) {
144
+ target = 'traefik' ;
145
+ }
64
146
}
65
147
66
148
if ( ! validTargets . includes ( target ) ) {
@@ -72,46 +154,13 @@ exports.handler = async ({target}) => {
72
154
return ;
73
155
}
74
156
75
- console . log ( chalk . bold ( `Updating ${ target } on:` ) , userConfig . endpoint ) ;
76
-
77
- // services request url
78
- const remoteUrl = `${ userConfig . endpoint } /update/${ target } ` ;
79
- // try sending request
80
- try {
81
- const { body, statusCode} = await got . post ( remoteUrl , options ) ;
82
- if ( statusCode !== 200 || body . error ) {
83
- throw new Error ( body . error || 'Oops. Something went wrong! Try again please.' ) ;
84
- }
85
-
86
- if ( body . updated ) {
87
- console . log ( chalk . green ( `Successfully updated ${ target } !` ) ) ;
88
- return ;
89
- }
90
-
91
- console . log ( chalk . green ( `${ _ . capitalize ( target ) } is already up to date!` ) ) ;
92
- } catch ( e ) {
93
- // if authorization is expired/broken/etc
94
- if ( e . statusCode === 401 ) {
95
- logout ( userConfig ) ;
96
- console . log ( chalk . red ( 'Error: authorization expired!' ) , 'Please, relogin and try again.' ) ;
97
- return ;
98
- }
99
-
100
- const reason = e . response . body && e . response . body . error ? e . response . body . error : e . toString ( ) ;
101
- console . log ( chalk . red ( `Error updating ${ target } :` ) , reason ) ;
102
- console . log ( 'Update log:\n' ) ;
103
- ( e . response . body . log || 'No log available' )
104
- . split ( '\n' )
105
- . map ( l => {
106
- try {
107
- return JSON . parse ( l ) ;
108
- } catch ( e ) {
109
- return l ;
110
- }
111
- } )
112
- . filter ( l => l !== undefined )
113
- . map ( l => l . trim ( ) )
114
- . filter ( l => l && l . length > 0 )
115
- . forEach ( line => console . log ( line ) ) ;
157
+ // if target is all - run updates sequentially
158
+ if ( target === 'all' ) {
159
+ await runUpdate ( 'traefik' ) ;
160
+ await runUpdate ( 'server' ) ;
161
+ return ;
116
162
}
163
+
164
+ // otherwise - just run given target
165
+ await runUpdate ( target ) ;
117
166
} ;
0 commit comments