Skip to content

Feature/csv show tool #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 20, 2020
113 changes: 113 additions & 0 deletions src/commands/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// code here to write the tool for csv tool show
import {Command, flags} from '@oclif/command'
import * as chalk from 'chalk'

import Logger from '../utilities/logger'
import Utilities from '../utilities/utilities'

export default class View extends Command {
static description = 'View file content and more'

static DEFAULT_COUNT = 10
static flags = {
help: flags.help({char: 'h'}),
file: flags.string({char: 'f' , description: 'formatted file to be shown'}),
num: flags.string({char: 'n' , description: `no. of rows to show, default:${View.DEFAULT_COUNT}`})
}

static args = [{name: 'file'}]

// required FILE
async run() {
const {args, flags} = this.parse(View)

args.file = this.getFilePath(flags, args)
args.num = this.getFileLinesToShow(flags)

// args.rowsToShow

this.checkParameters(flags, args)
this.showFile(args)
}

private getFilePath(flags: any, args: any) {
if (args.file)
return args.file
if (flags.file)
return flags.file9
Logger.error(this, 'File path not passed')
}

private getFileLinesToShow(flags: any) {
if (flags.num && flags.num > 0) // if value available and valid
return flags.num
else
return View.DEFAULT_COUNT
}
// tslint:disable-next-line:no-unused
private checkParameters(flags: unknown, args: { [p: string]: any }) {
if (args.file === undefined || args.file === '')
Logger.error(this, 'File path is empty')
// others already checked
}

private showFile(args: any) {
let data = Utilities.getStringFromFile(this, args.file)
let rows = data.split('\n')
let widthArray = []

let recordsToShow = parseInt(args.num, 10) + 1

for (let i = 0; i < rows[0].length; i++) {
widthArray[i] = 0
}

if (recordsToShow > rows.length) {
recordsToShow = rows.length - 1
}

for (let i = 0; i < recordsToShow; i++) {
let row = rows[i].split(',')
for (let j = 0; j < row.length; j ++) {
if (widthArray[j] < row[j].length) {
widthArray[j] = row[j].length
}
}
}

// -1 need to be done to exclude header
// ${chalk.yellow('Avro Schema')
Logger.info(this, `Total ${chalk.greenBright(rows.length - 2)} records in file.`)
Logger.info(this, `showing top ${chalk.yellow(recordsToShow - 1)} records.`)

this.printRow(rows[0].split(','), widthArray, '+', true)
for (let i = 0; i < recordsToShow; i++) {
let row = rows[i]
this.printRow(row.split(','), widthArray, '|', false)
this.printRow(row.split(','), widthArray, '+', true)
}

Logger.success(this, 'done.\n')
}

private printRow(row: any, widthArray: any, seperator: any, isLineSeparator: any) {
let output = seperator
for (let i = 0; i < row.length; i ++) {
let totalSize = widthArray[i]
let dataLength = 0
let space = '-'
if (!isLineSeparator) {
let data = row[i]
data = data.split(/\r/)[0]
output += data
dataLength = data.length
space = ' '
}
for (let j = 0; j < totalSize - dataLength; j++) {
output += space
}
output += seperator
}
this.log('' + output)
}
}
20 changes: 10 additions & 10 deletions test/commands/avro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ describe('avro', () => {
expect(ctx.stdout).to.contain('Schema file is not provided')
})


// TODO: BUG this is just skipping test , find a way to implement wait() etc.
test
.stdout()
.command(['avro', '-f', 'test/resources/avro/person.avro', 'get_schema'])
.it('if get_schema outputs to console', ctx => {
setTimeout(() =>
expect(ctx.stdout).to.contain('success')
, 5000) // wait for it to write stuff on console
})
//
// // TODO: this was is just skipping test , find a way to implement wait() etc.
// test
// .stdout()
// .command(['avro', '-f', 'test/resources/avro/person.avro', 'get_schema'])
// .it('if get_schema outputs to console', ctx => {
// setTimeout(() =>
// expect(ctx.stdout).to.contain('success')
// , 9000) // wait for it to write stuff on console
// })

test
.stdout()
Expand Down
45 changes: 45 additions & 0 deletions test/commands/view.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {expect, test} from '@oclif/test'

describe('view', () => {
test
.stdout()
.command(['view'])
.exit(0)
.it('File path not passed', ctx => {
expect(ctx.stdout).to.contain('File path not passed')
})

test
.stdout()
.command(['view', 'test/resources/csv/test_view.csv'])
.it('check 10 lines are found', ctx => {
expect(ctx.stdout).to.contain('|1 |2 |1 |14.0 |30.0708|1 |0 |')
})
test
.stdout()
.command(['view', 'test/resources/csv/test_view.csv', '-n', '5'])
.it('check 5th line is found', ctx => {
expect(ctx.stdout).to.contain('|0 |3 |0 |35.0|8.05 |0 |0 |')
})

test
.stdout()
.command(['view', 'test/resources/csv/test_view.csv', '-n', '900'])
.it('check if the given number is greater than total lines', ctx => {
expect(ctx.stdout).to.contain('|0 |3 |0 |32.0 |7.75 |0 |0 |')
})

test
.stdout()
.command(['view', 'test/resources/csv/test_view.csv', '-n', '900'])
.it('check if 900 count is used, it shows only total present rows', ctx => {
expect(ctx.stdout).to.contain('showing top 891 records.')
})

test
.stdout()
.command(['view', 'test/resources/csv/test_view.csv', '-n', '0'])
.it('check if the given number is invalid then show default 10 lines', ctx => {
expect(ctx.stdout).to.contain('|1 |2 |1 |14.0 |30.0708|1 |0 |')
})
})
Loading