Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
fix password prompt when update
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenDjango committed Feb 26, 2020
1 parent 809a3cb commit 278af9c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -170,18 +173,15 @@ 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') {
console.log('v' + APP_VERSION)
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)
}
}
Expand All @@ -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`')
)
}
4 changes: 2 additions & 2 deletions src/blih_api.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
109 changes: 109 additions & 0 deletions src/git_menu.ts
Original file line number Diff line number Diff line change
@@ -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))
}
}
*/
1 change: 1 addition & 0 deletions src/repository_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}

0 comments on commit 278af9c

Please sign in to comment.