UNPKG

6.32 kBJavaScriptView Raw
1
2var Common = require('../Common.js');
3var cst = require('../../constants.js');
4var UX = require('./UX');
5var chalk = require('chalk');
6var Configuration = require('../Configuration.js');
7
8module.exports = function(CLI) {
9
10 CLI.prototype.get = function(key, cb) {
11 var that = this;
12
13 if (!key || key == 'all') {
14 displayConf(function(err, data) {
15 if (err)
16 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
17 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
18 });
19 return false;
20 }
21 Configuration.get(key, function(err, data) {
22 if (err) {
23 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
24 }
25 // pm2 conf module-name
26 if (key.indexOf(':') === -1 && key.indexOf('.') === -1) {
27 displayConf(key, function() {
28 console.log('Modules configuration. Copy/Paste line to edit values.')
29 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
30 });
31 return false;
32 }
33 // pm2 conf module-name:key
34 var module_name, key_name;
35
36 if (key.indexOf(':') > -1) {
37 module_name = key.split(':')[0];
38 key_name = key.split(':')[1];
39 } else if (key.indexOf('.') > -1) {
40 module_name = key.split('.')[0];
41 key_name = key.split('.')[1];
42 }
43
44 Common.printOut('Value for module ' + chalk.blue(module_name), 'key ' + chalk.blue(key_name) + ': ' + chalk.bold.green(data));
45
46
47 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
48 });
49 };
50
51 CLI.prototype.set = function(key, value, cb) {
52 var that = this;
53
54 if (!key) {
55 interactiveConfigEdit(function(err) {
56 if (err)
57 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
58 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
59 });
60 return false;
61 }
62
63 /**
64 * Set value
65 */
66 Configuration.set(key, value, function(err) {
67 if (err)
68 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
69
70 var values = [];
71
72 if (key.indexOf('.') > -1)
73 values = key.split('.');
74
75 if (key.indexOf(':') > -1)
76 values = key.split(':');
77
78 if (values && values.length > 1) {
79 // The first element is the app name (module_conf.json)
80 var app_name = values[0];
81
82 process.env.PM2_PROGRAMMATIC = 'true';
83 that.restart(app_name, {
84 updateEnv : true
85 }, function(err, data) {
86 process.env.PM2_PROGRAMMATIC = 'false';
87 if (!err)
88 Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name);
89 Common.log('Setting changed')
90 displayConf(app_name, function() {
91 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
92 });
93 });
94 return false;
95 }
96 displayConf(null, function() {
97 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
98 });
99 });
100 };
101
102 CLI.prototype.multiset = function(serial, cb) {
103 var that = this;
104
105 Configuration.multiset(serial, function(err, data) {
106 if (err)
107 return cb ? cb({success:false, err:err}) : that.exitCli(cst.ERROR_EXIT);
108
109 var values = [];
110 var key = serial.match(/(?:[^ "]+|"[^"]*")+/g)[0];
111
112 if (key.indexOf('.') > -1)
113 values = key.split('.');
114
115 if (key.indexOf(':') > -1)
116 values = key.split(':');
117
118 if (values && values.length > 1) {
119 // The first element is the app name (module_conf.json)
120 var app_name = values[0];
121
122 process.env.PM2_PROGRAMMATIC = 'true';
123 that.restart(app_name, {
124 updateEnv : true
125 }, function(err, data) {
126 process.env.PM2_PROGRAMMATIC = 'false';
127 if (!err)
128 Common.printOut(cst.PREFIX_MSG + 'Module %s restarted', app_name);
129 displayConf(app_name, function() {
130 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
131 });
132 });
133 return false;
134 }
135 displayConf(app_name, function() {
136 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT)
137 });
138 });
139 };
140
141 CLI.prototype.unset = function(key, cb) {
142 var that = this;
143
144 Configuration.unset(key, function(err) {
145 if (err) {
146 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
147 }
148
149 displayConf(function() { cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT) });
150 });
151 };
152
153 CLI.prototype.conf = function(key, value, cb) {
154 var that = this;
155
156 if (typeof(value) === 'function') {
157 cb = value;
158 value = null;
159 }
160
161 // If key + value = set
162 if (key && value) {
163 that.set(key, value, function(err) {
164 if (err)
165 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
166 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
167 });
168 }
169 // If only key = get
170 else if (key) {
171 that.get(key, function(err, data) {
172 if (err)
173 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
174 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
175 });
176 }
177 else {
178 interactiveConfigEdit(function(err) {
179 if (err)
180 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
181 return cb ? cb(null, {success:true}) : that.exitCli(cst.SUCCESS_EXIT);
182 });
183 }
184 };
185
186};
187
188function interactiveConfigEdit(cb) {
189 UX.helpers.openEditor(cst.PM2_MODULE_CONF_FILE, function(err, data) {
190 Common.printOut(chalk.bold('Module configuration (%s) edited.'), cst.PM2_MODULE_CONF_FILE);
191 Common.printOut(chalk.bold('To take changes into account, please restart module related.'), cst.PM2_MODULE_CONF_FILE);
192 if (err)
193 return cb(Common.retErr(err));
194 return cb(null, {success:true});
195 });
196
197}
198
199/**
200 * Configuration
201 */
202function displayConf(target_app, cb) {
203 if (typeof(target_app) == 'function') {
204 cb = target_app;
205 target_app = null;
206 }
207
208 Configuration.getAll(function(err, data) {
209 UX.helpers.dispKeys(data, target_app);
210 return cb();
211 });
212}