baidubce-cli
Version:
baidu cloud engine command line tools
75 lines (55 loc) • 1.98 kB
JavaScript
/*
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
* 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.
*/
var path = require('path');
var fs = require('fs');
var kConfigHome = process.env[require('os').platform() === 'win32' ? 'APPDATA' : 'HOME'];
var kConfigFile = path.join(kConfigHome, '.baidubcerc');
exports.dump = function () {
if (!fs.existsSync(kConfigFile)) {
console.log('There is no any configuration.');
return;
}
console.log('@' + kConfigFile);
console.log();
console.log(fs.readFileSync(kConfigFile, 'utf-8'));
};
exports.set = function (args) {
var currentConfig = {};
if (fs.existsSync(kConfigFile)) {
currentConfig = JSON.parse(
fs.readFileSync(kConfigFile, 'utf-8'));
}
if (args.length >= 2) {
currentConfig[args[0]] = args[1];
}
else if (args.length === 1) {
if (args[0].indexOf('=') !== -1) {
var chunks = args[0].split('=', 2);
currentConfig[chunks[0]] = chunks[1];
}
else {
currentConfig[args[0]] = true;
}
}
fs.writeFileSync(kConfigFile, JSON.stringify(currentConfig, null, 2));
};
exports.get = function (key) {
if (!fs.existsSync(kConfigFile)) {
throw new Error('There is no any configuration');
}
var currentConfig = JSON.parse(
fs.readFileSync(kConfigFile, 'utf-8')
);
return currentConfig[key];
};
/* vim: set ts=4 sw=4 sts=4 tw=120: */