Skip to content

Commit

Permalink
Add feature to change gitlab server. Closes #87 (#88)
Browse files Browse the repository at this point in the history
* Adds feature to change gitlab server.
*Updates unit, feature and integration tests.
*Updates README file.
  • Loading branch information
1AmNegan authored and prasadtalasila committed May 9, 2018
1 parent c557747 commit 48c7166
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 103 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ A client command line interface for submissions to [AutolabJS](https://github.co
## Commands ##
* `autolabjs init [-u <username> -p <password>]` - Log into AutolabJS.
* `autolabjs exit` - Logout off AutolabJS
* `autolabjs prefs changeserver [--host <host> --port <port>]` - To change the host of the gitlab server
* `autolabjs prefs changelang [--lang <language>]` - To change the language of the code submission
* `autolabjs prefs changeserver [ --type ms --host <host> --port <port>]` - To change the host for main server.
* `autolabjs prefs changeserver [ --type gitlab --host <host>]` - To change the host for gitlab server.
* `autolabjs prefs show` - Show the saved preferences
* `autolabjs eval [-l <lab_name> --lang <language>]` - To submit your code for evaluation
* `autolabjs help` - Print help manual
Expand Down
100 changes: 82 additions & 18 deletions lib/cli/input/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,48 @@ const portValidator = (port, flags) => {
return flags === true ? false : 'Enter a valid port';
};

const changeServerPrompt = async () => {
const hostPromptGenerator = new PromptGenerator();
hostPromptGenerator.addProperty('name', 'host');
hostPromptGenerator.addProperty('type', 'input');
hostPromptGenerator.addProperty('message', 'Enter the host:');
hostPromptGenerator.addProperty('validate', hostValidator);
const portPromptGenerator = new PromptGenerator();
portPromptGenerator.addProperty('name', 'port');
portPromptGenerator.addProperty('type', 'input');
portPromptGenerator.addProperty('message', 'Enter the port:');
portPromptGenerator.addProperty('validate', portValidator);
const getPromptGenerator = (name, type, message, validate) => {
const promptGenerator = new PromptGenerator();
promptGenerator.addProperty('name', name);
promptGenerator.addProperty('type', type);
promptGenerator.addProperty('message', message);
promptGenerator.addProperty('validate', validate);
return promptGenerator;
};

const getHostPrompt = () => {
const hostPromptGenerator = getPromptGenerator('host', 'input', 'Enter the host:', hostValidator);
return hostPromptGenerator.getPrompt();
};

const getPortPrompt = () => {
const portPromptGenerator = getPromptGenerator('port', 'input', 'Enter the port:', portValidator);
return portPromptGenerator.getPrompt();
};

const getTypePrompt = () => {
const typePromptGenerator = getPromptGenerator('type', 'list', 'Choose the server type:');
typePromptGenerator.addProperty('choices', ['gitlab', 'ms']);
return typePromptGenerator.getPrompt();
};

const changeMainServerPrompt = async () => {
const prompts = [
hostPromptGenerator.getPrompt(),
portPromptGenerator.getPrompt(),
getHostPrompt(),
getPortPrompt(),
];
const serverPrefs = await inquirer.prompt(prompts);
return {
name: 'server_changed',
details: {
host: serverPrefs.host,
port: serverPrefs.port,
type: 'ms',
},
};
};

const changeServerFlag = (host, port) => {
const changeMainServerFlag = (host, port) => {
if (!hostValidator(host, true)) {
return {
name: 'invalid_host',
Expand All @@ -105,16 +120,65 @@ const changeServerFlag = (host, port) => {
details: {
host,
port,
type: 'ms',
},
};
};

const changeServer = async (options) => {
const { host, port } = options;
const changeGitlabPrompt = async () => {
const prompts = [
getHostPrompt(),
];
const serverPrefs = await inquirer.prompt(prompts);
return {
name: 'server_changed',
details: {
host: serverPrefs.host,
type: 'gitlab',
},
};
};

const changeGitlabFlag = (host) => {
if (!hostValidator(host, true)) {
return {
name: 'invalid_host',
};
}
return {
name: 'server_changed',
details: {
host,
type: 'gitlab',
},
};
};

const changeMainServer = (host, port) => {
if (!host || !port) {
return changeServerPrompt();
return changeMainServerPrompt();
}
return changeMainServerFlag(host, port);
};

const changeGitlab = (host) => {
if (!host) {
return changeGitlabPrompt();
}
return changeGitlabFlag(host);
};

const changeServer = async (options) => {
let { host, port, type } = options;
if (!type) {
const typePrompt = getTypePrompt();
type = (await inquirer.prompt(typePrompt)).type;
}
if (type === 'ms') {
return changeMainServer(host, port);
} else if (type === 'gitlab') {
return changeGitlab(host);
}
return changeServerFlag(host, port);
};

const getInput = async (args, options) => {
Expand Down
14 changes: 9 additions & 5 deletions lib/cli/output/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const Table = require('cli-table');
const showPrefs = (prefs) => {
const table = new Table({
head: [chalk.cyan('Preferences'), chalk.cyan('Values')],
colWidths: [15, 25],
colWidths: [20, 25],
});
table.push(
['Language', prefs.lang],
['Server host', prefs.mainserver_host],
['Server port', prefs.mainserver_port],
['Gitlab host', prefs.gitlab_host],
['Main Server host', prefs.mainserver_host],
['Main Server port', prefs.mainserver_port],
);
console.log(table.toString());
};
Expand All @@ -20,7 +20,11 @@ const sendOutput = (event) => {
console.log(chalk.green(`Your submission language has been changed to ${event.details.lang}`));
break;
case 'server_changed':
console.log(chalk.green(`Your submission server has been changed to ${event.details.host} at port ${event.details.port}`));
if (event.details.type === 'ms') {
console.log(chalk.green(`Your main server has been changed to ${event.details.host} at port ${event.details.port}`));
} else if (event.details.type === 'gitlab') {
console.log(chalk.green(`Your gitlab server has been changed to ${event.details.host}`));
}
break;
case 'show_prefs':
showPrefs(event.details);
Expand Down
1 change: 1 addition & 0 deletions lib/controller/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const addTo = (program) => {
program
.command('prefs', 'Show and change language and server preferences')
.argument('<preference>', 'Argument for prefs command', /^show|changelang|changeserver$/)
.option('--type', 'Server type ( gitlab or main_server)', /^ms|gitlab$/)
.option('--host <server_host>', 'Host name / IP address for server ')
.option('--port <server_port>', 'Port for the server')
.option('--lang <lang>', 'Change language')
Expand Down
53 changes: 42 additions & 11 deletions lib/model/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,61 @@ const preferenceManager = require('../utils/preference-manager');
const eventForShowPref = (changePrefEvent) => {
const cliPrefs = preferenceManager.getPreference({ name: 'cliPrefs' });
changePrefEvent.details = {
lang: cliPrefs.submission.language,
gitlab_host: cliPrefs.gitlab.host,
mainserver_host: cliPrefs.main_server.host,
mainserver_port: cliPrefs.main_server.port,
};
return changePrefEvent;
};

const getPrefsObjectToStore = (prefsName, values) => ({
name: prefsName,
values,
});

const storeMainServer = (host, port) => {
preferenceManager.setPreference(getPrefsObjectToStore(
'cliPrefs',
{
main_server: {
host,
port,
},
},
));
};

const storeGitlab = (host) => {
preferenceManager.setPreference(getPrefsObjectToStore(
'cliPrefs',
{
gitlab: {
host,
},
},
));
};

const storeServer = (details) => {
if (details.type === 'ms') {
storeMainServer(details.host, details.port);
} else if (details.type === 'gitlab') {
storeGitlab(details.host);
}
}

const storePrefs = (changePrefEvent) => {
if (changePrefEvent.name === 'lang_changed') {
preferenceManager.setPreference({
name: 'cliPrefs',
values: {
preferenceManager.setPreference(getPrefsObjectToStore(
'cliPrefs',
{
submission: {
language: changePrefEvent.details.lang,
},
},
});
));
} else if (changePrefEvent.name === 'server_changed') {
preferenceManager.setPreference({
name: 'cliPrefs',
values: {
main_server: changePrefEvent.details,
},
});
storeServer(changePrefEvent.details);
} else if (changePrefEvent.name === 'show_prefs') {
changePrefEvent = eventForShowPref(changePrefEvent);
}
Expand Down
6 changes: 0 additions & 6 deletions lib/utils/preference-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const cliPrefs = new Preferences('autolabjs.cli.config', {}, {
format: 'json',
});

const locations = new Preferences('autolabjs.locations', {}, {
encrypt: false,
file: `${prefDirectory}/locations.json`,
format: 'json',
});

const gitLabPrefs = new Preferences('autolabjs.gitlab.credentials', {}, {
encrypt: true,
file: `${prefDirectory}/gitlab-credentials`,
Expand Down
34 changes: 24 additions & 10 deletions test/feature/features/prefs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,53 @@ Feature: Init

Scenario: Prefs command to change language using flags
Given I have already logged in
When I run prefs command with 'changelang' using 'flags'
When I run prefs command with changelang using 'flags'
Then I should be able to change the submission language

Scenario: Prefs command to change language using prompt
Given I have already logged in
When I run prefs command with 'changelang' using 'prompt'
When I run prefs command with changelang using 'prompt'
Then I should be able to change the submission language

Scenario: Prefs command to change language for invalid language
Given I have already logged in
When I run prefs command with changelang with invalid language
Then I should be displayed an error message for invalid language

Scenario: Prefs command to change server using flags
Scenario: Prefs command to change gitlab server using flags
Given I have already logged in
When I run prefs command with 'changeserver' using 'flags'
Then I should be able to change the submission server
When I run prefs command with changeserver using 'flags' for 'gitlab'
Then I should be able to change the gitlab server

Scenario: Prefs command with invalid host
Scenario: Prefs command to change main server using flags
Given I have already logged in
When I run change server with invalid host
When I run prefs command with changeserver using 'flags' for 'ms'
Then I should be able to change the main server

Scenario: Prefs command with invalid host for main server
Given I have already logged in
When I run change server with invalid host for 'ms'
Then I should be displayed an error message for invalid host

Scenario: Prefs command with invalid host for gitlab server
Given I have already logged in
When I run change server with invalid host for 'gitlab'
Then I should be displayed an error message for invalid host

Scenario: Prefs command with invalid port
Given I have already logged in
When I run change server with invalid port
Then I should be displayed an error message for invalid port

Scenario: Prefs command to change main server using prompt
Given I have already logged in
When I run prefs command with changeserver using 'prompt' for 'ms'
Then I should be able to change the main server

Scenario: Prefs command to change server using prompt
Scenario: Prefs command to change gitlab server using prompt
Given I have already logged in
When I run prefs command with 'changeserver' using 'prompt'
Then I should be able to change the submission server
When I run prefs command with changeserver using 'prompt' for 'gitlab'
Then I should be able to change the gitlab server

Scenario: Prefs command to show preferences
Given I have already logged in
Expand Down
2 changes: 1 addition & 1 deletion test/feature/steps/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Given('I have NOT logged in', () => {
});

Given('I have logged in as root', () => {
preferenceManager.setPreference({ name: 'gitLabPrefs', values: { username: 'root' }});
preferenceManager.setPreference({ name: 'gitLabPrefs', values: { username: 'root' } });
});

When('I run eval command with using {string}', async (inputType) => {
Expand Down
2 changes: 1 addition & 1 deletion test/feature/steps/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ chai.use(sinonChai);
chai.should();

Given('I have already logged in', async () => {
preferenceManager.setPreference({ name: 'gitLabPrefs', values: { username: 'AutolabJS_Tester' }});
preferenceManager.setPreference({ name: 'gitLabPrefs', values: { username: 'AutolabJS_Tester' } });
});

When('I run exit command', async () => {
Expand Down
Loading

0 comments on commit 48c7166

Please # to comment.