diff --git a/src/app.ts b/src/app.ts index 3d6f3c9..e0a8909 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,7 +2,7 @@ import ora from 'ora' import chalk from 'chalk' import { BlihApi } from './blih_api' import { ask_list, ask_email, ask_password, ask_question } from './ui' -import { ConfigType, APP_VERSION, open_config, write_config, print_message, sh } from './utils' +import { ConfigType, APP_VERSION, open_config, write_config, print_message, sh_live } from './utils' import { repo_menu, create_repo, change_acl } from './repository_menu' import { key_menu } from './key_menu' @@ -32,6 +32,9 @@ export const run = async () => { "Let's do some works" ) switch (choice) { + /*TODO: case 'Git clone': + await repo_menu(api, config) + break*/ case 'Repositories management': await repo_menu(api, config) break @@ -170,7 +173,7 @@ async function fast_mode(api: BlihApi, config: ConfigType) { async function parse_args(args: string[], config: ConfigType) { if (args[2]) { if (args[2] === '-h' || args[2] === '-H' || args[2] === '--help') { - show_help() + await sh_live('man blih_cli') process.exit(0) } if (args[2] === '-v' || args[2] === '-V' || args[2] === '--version') { @@ -178,10 +181,7 @@ async function parse_args(args: string[], config: ConfigType) { process.exit(0) } if (args[2] === '-u' || args[2] === '-U' || args[2] === '--update' || args[2] === '--UPDATE') { - const spinner = ora().start(chalk.green('Check for update...')) - const res = await sh(`sudo sh ${__dirname}/../update.sh 2>&1`) - spinner.stop() - console.log(res.stdout.slice(0, -1)) + await sh_live(`sudo sh ${__dirname}/../update.sh`) process.exit(0) } } @@ -190,11 +190,6 @@ async function parse_args(args: string[], config: ConfigType) { function show_help() { ora().info( - chalk.blue( - 'Invalid option\n Usage blih_cli -[ica] [OPTION]...' + - '\n\n -i interactive mode, default mode' + - '\n -c create new repository' + - '\n -a [REPO], --acl=REPO change repository acl' - ) + chalk.blue('Invalid option\n Usage blih_cli -[aci] [OPTION]...' + '\n or use `man blih_cli`') ) } diff --git a/src/blih_api.ts b/src/blih_api.ts index e5c5878..d4a6809 100644 --- a/src/blih_api.ts +++ b/src/blih_api.ts @@ -1,8 +1,8 @@ -// MIT https://www.npmjs.com/package/blih - import crypto from 'crypto' import axios, { AxiosInstance, AxiosResponse, AxiosRequestConfig } from 'axios' +// MIT https://www.npmjs.com/package/blih + const options = { baseURL: 'https://blih.epitech.eu/', timeout: 10000, diff --git a/src/git_menu.ts b/src/git_menu.ts new file mode 100644 index 0000000..ef8ceb7 --- /dev/null +++ b/src/git_menu.ts @@ -0,0 +1,109 @@ +/* +TODO: +import ora from 'ora' +import chalk from 'chalk' +import { homedir } from 'os' +import fs from 'fs' +import { BlihApi } from './blih_api' +import { ask_list, ask_question, ask_path, ask_input } from './ui' +import { WAIT_MSG, sh } from './utils' + +const HOME_DIR = homedir() + +export async function git_menu(api: BlihApi) { + let should_quit = false + + while (!should_quit) { + const choices = ['↵ Back', 'Git clone my repo', 'Other repo'] + const choice = await ask_list(choices, 'Repository') + switch (choice) { + case choices[1]: + await add_key(api) + break + case choices[2]: + await delete_key(api) + break + case choices[0]: + default: + should_quit = true + } + } +} + +async function add_key(api: BlihApi) { + const new_ssh = await ask_question('Create new ssh key ?') + const spinner = ora() + spinner.color = 'blue' + + try { + let path = '' + if (new_ssh) { + let name = 'epitech_key' + if (fs.existsSync(`${HOME_DIR}/.ssh/${name}.pub`)) { + do { + name = await ask_input(`${name} already exist, new Key name ?`) + } while (fs.existsSync(`${HOME_DIR}/.ssh/${name}.pub`)) + } + if (!fs.existsSync(`${HOME_DIR}/.ssh`)) { + fs.mkdirSync(`${HOME_DIR}/.ssh`) + } + spinner.start(chalk.green(WAIT_MSG)) + await sh(`ssh-keygen -f ${HOME_DIR}/.ssh/${name} -N ""`) + await sh(`ssh-add ${HOME_DIR}/.ssh/${name}`) + path = `${HOME_DIR}/.ssh/${name}.pub` + } else { + const input = await ask_path('Ssh key path:', '\\.pub$', `${HOME_DIR}/`) + spinner.info(chalk.blue('Use `ssh-add ' + input + '` for enable the key')) + path = input + spinner.start(chalk.green(WAIT_MSG)) + } + let key = fs.readFileSync(path, 'utf8') + key = key.replace('\n', '') + const res = await api.uploadKey(key) + spinner.succeed(chalk.green(res)) + } catch (err) { + spinner.fail(chalk.red(err)) + } +} + +async function delete_key(api: BlihApi) { + const spinner = ora().start(chalk.green(WAIT_MSG)) + spinner.color = 'blue' + + try { + const key_list = await api.listKeys() + spinner.stop() + const choice = await ask_list( + ['↵ Back', ...key_list.map(value => value.name + ' ...' + value.data.substr(-20))], + 'Select a key' + ) + if (choice === '↵ Back' || !(await ask_question('Are you sure ?'))) return + const key = choice.split(' ')[0] + spinner.start(chalk.green(WAIT_MSG)) + const res = await api.deleteKey(key) + spinner.succeed(chalk.green(res)) + } catch (err) { + spinner.fail(chalk.red(err)) + } +} + +async function show_key(api: BlihApi) { + const spinner = ora().start(chalk.green(WAIT_MSG)) + spinner.color = 'blue' + + try { + const key_list = await api.listKeys() + spinner.stop() + const idx = await ask_list( + ['↵ Back', ...key_list.map(value => value.name + ' ...' + value.data.substr(-20))], + undefined, + true + ) + if (idx === '0') return + const key = key_list[+idx - 1] + spinner.info(chalk.blue(`Name: ${key.name}` + `\n Data: ${key.data}`)) + } catch (err) { + spinner.fail(chalk.red(err)) + } +} +*/ diff --git a/src/repository_menu.ts b/src/repository_menu.ts index bebf3ef..6c987f2 100644 --- a/src/repository_menu.ts +++ b/src/repository_menu.ts @@ -62,6 +62,7 @@ export async function create_repo(api: BlihApi, config: ConfigType, repo_name?: ) to_change = await acl_menu(acl_list, config) } + //TODO: if (await ask_question(`Git clone ${input} ?`)) 1 } catch (err) { spinner.fail(chalk.red(err)) } diff --git a/src/utils.ts b/src/utils.ts index fdf170b..8d6bd2e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -188,3 +188,18 @@ export async function sh(cmd: string): Promise<{ stdout: string; stderr: string }) }) } + +export async function sh_live(cmd: string): Promise<{ stdout: string; stderr: string }> { + return new Promise(function(resolve, reject) { + const child = exec(cmd, (err, stdout, stderr) => { + if (err) reject(err) + else resolve({ stdout, stderr }) + }) + child.stdout?.on('data', data => { + process.stdout.write(data) + }) + child.stderr?.on('data', data => { + process.stderr.write(data) + }) + }) +}