Skip to content

Commit

Permalink
+ Bashrc
Browse files Browse the repository at this point in the history
  • Loading branch information
atnartur committed Aug 15, 2015
1 parent 4db6250 commit 4532a50
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 17 deletions.
3 changes: 1 addition & 2 deletions config.json
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,4 @@
{ {
"proxy_host": "proxy.school.tatar.ru", "proxy_host": "proxy.school.tatar.ru",
"proxy_port": 8080, "proxy_port": 8080
"tasks": ["bashrc", "chrome", "apt"]
} }
48 changes: 33 additions & 15 deletions start.js
Original file line number Original file line Diff line number Diff line change
@@ -1,24 +1,33 @@
// системные модули
var fs = require('fs'); var fs = require('fs');
var argv = require('optimist').argv; var argv = require('optimist').argv;
var read = require('read'); var read = require('read');
var async = require('async'); var async = require('async');


// задачи
var tasks = {
bashrc: require('./tasks/bashrc'),
}

// загрузка конфигурации
try{ try{
var config = require('./config.json'); global.config = require('./config.json');
} }
catch(e){ catch(e){
console.error('Не удалось прочитать config.json - синтаксическая ошибка:'); console.error('Не удалось прочитать config.json - синтаксическая ошибка:');
console.log(e); console.log(e);
process.exit(); process.exit();
} }


if (typeof config.proxy_host === 'undefined' || typeof config.proxy_port === 'undefined') { // проверка конфигурации
if (typeof global.config.proxy_host === 'undefined' || typeof global.config.proxy_port === 'undefined') {
console.error("Ошибка: не указаны данные прокси-сервера. Укажите их в config.json"); console.error("Ошибка: не указаны данные прокси-сервера. Укажите их в config.json");
process.exit(); process.exit();
} }


async.series([ async.series([ // предотвращаем асинхронность
function(callback){ function(callback){
// получаем логин и пароль от пользователя с помощью ввода
read({ prompt : 'Введите логин от edu.tatar.ru: ' }, function (err, login) { read({ prompt : 'Введите логин от edu.tatar.ru: ' }, function (err, login) {
if (typeof login === 'undefined' || login == '') { if (typeof login === 'undefined' || login == '') {
console.error('Для работы программы необходим логин от edu.tatar.ru'); console.error('Для работы программы необходим логин от edu.tatar.ru');
Expand All @@ -34,8 +43,8 @@ async.series([
process.exit(); process.exit();
} }


config.edu_login = login; global.config.edu_login = login;
config.edu_pass = pass; global.config.edu_pass = pass;


process.stdin.destroy(); process.stdin.destroy();


Expand All @@ -46,16 +55,25 @@ async.series([
function (callback) { function (callback) {
console.log('Конфигурация загружена, начинаем...'); console.log('Конфигурация загружена, начинаем...');


config.tasks.forEach(function(task){ var operation;
switch (task) { var task = '*';
case 'bashrc':

if (typeof argv._[0] !== 'undefined' && (argv._[0] == 'write' || argv._[0] == 'check' || argv._[0] == 'remove')) {
break; operation = argv._[0];
default:
console.error('Задача', task, 'не найдена'); if (typeof argv._[1] !== 'undefined')
break; task = argv._[1];
} }
}); else
task = 'write';

if (task === '*')
task[operation]();
else{
tasks.forEach(function(task){ // пробегаемся по массиву задач и выполняем их
task[operation]();
});
}
} }
]); ]);


Expand Down
60 changes: 60 additions & 0 deletions tasks/bashrc.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,60 @@
var fs = require('fs');
var path = require('path');

module.exports = {
label_str: '# ClienD proxy',
get_path: function(){
return path.normalize(process.env.HOME + '/.bashrc');
},
get_code: function(){
var str = "http://" + global.config.edu_login + ":" + global.config.edu_pass + "@" + global.config.host + ":" + global.config.port + "/";

return
this.label_str + "\n" +
"export http_proxy='" + str + "'\n" +
"export ftp_proxy='" + str + "'\n" +
this.label_str
},
check: function(){
var filepath = this.get_path();
if(!fs.existsSync(filepath))
return false;
else{
var content = fs.readFileSync(filepath);
if(content.indexOf(this.label_str) !== -1){
if(content.indexOf(this.get_code()) !== -1)
return true;
else
return false;
}
else
return false;
}
},
write: function(){
if (this.check())
return true;
else{
var filepath = this.get_path();

if (!fs.existsSync(filepath)) {
if(!fs.writeFileSync(filepath, "\n")){
console.error("Не удалось создать файл", filepath, '. Запустите скрипт с правами администратора');
return false;
}
}

if (!fs.accessSync(filepath)) {
console.error('Ошибка: недоступен файл', filepath, '. Запустите скрипт с правами администратора');
return false;
}

fs.appendFileSync(filepath, this.get_code());

return true;
}
},
remove: function(){

}
}

0 comments on commit 4532a50

Please sign in to comment.