Skip to content

Commit 92e3ad0

Browse files
committed
[HASHES]: file support added
1 parent 345aa6e commit 92e3ad0

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

src/commands/hash.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Command, flags} from '@oclif/command'
2+
import * as fs from 'fs'
23
// @ts-ignore
34
import * as Hashes from 'jshashes'
45

@@ -22,31 +23,38 @@ export default class Hash extends Command {
2223

2324
const type: string = flags.type || 'sha1' //by default let it be sha1
2425

25-
// if -s is not passed we will take it from args
26+
// if -s or -f is not passed we will take it from args
27+
let str = ''
2628

27-
let str: string
28-
29-
if (flags.string)
29+
if (flags.string) //if -s given
3030
str = flags.string
31-
else
32-
str = args.string
31+
else if (flags.file) {
32+
str = this.getStringFromFile(flags.file)
33+
} else
34+
str = args.string
3335

36+
this.log(str)
3437
this.calculateHash(type, str)
3538
}
3639

3740
private calculateHash(type: string, str: string) {
3841
let hash: Hashes
3942
switch (type.toUpperCase()) {
4043
case 'SHA1':
41-
hash = new Hashes.SHA1(); break
44+
hash = new Hashes.SHA1()
45+
break
4246
case 'SHA256':
43-
hash = new Hashes.SHA256(); break
47+
hash = new Hashes.SHA256()
48+
break
4449
case 'SHA512':
45-
hash = new Hashes.SHA512(); break
50+
hash = new Hashes.SHA512()
51+
break
4652
case 'MD5':
47-
hash = new Hashes.MD5(); break
53+
hash = new Hashes.MD5()
54+
break
4855
case 'RMD160':
49-
hash = new Hashes.RMD160(); break
56+
hash = new Hashes.RMD160()
57+
break
5058
default:
5159
hash = undefined
5260
}
@@ -57,6 +65,21 @@ export default class Hash extends Command {
5765
} else {
5866
this.log('[ERROR]: invalid hash type')
5967
}
68+
}
69+
70+
private getStringFromFile(filePath: string) {
71+
let fileStr = ''
72+
if (!fs.existsSync(filePath)) {
73+
this.error('reading File') // this will output error and exit command
74+
} else {
75+
fileStr = fs.readFileSync(filePath, 'utf8')
76+
77+
// TODO: fix this Issue #3
78+
if (fileStr.charAt(fileStr.length - 1) === '\n') {
79+
fileStr = fileStr.substring(0, fileStr.length - 1)
80+
}
81+
}
82+
return fileStr
6083

6184
}
6285
}

test/commands/hash.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('hash', () => {
1111
// passing sha1 as option
1212
test
1313
.stdout()
14-
.command(['hash', 'ashish' ,'-t','sha1'])
14+
.command(['hash', 'ashish' , '-t', 'sha1'])
1515
.it("cdt hash 'ashish' -t 'sha1'", ctx => {
1616
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
1717
})
@@ -63,4 +63,23 @@ describe('hash', () => {
6363
.it("cdt hash --type rmd160 'ashish'", ctx => {
6464
expect(ctx.stdout).to.contain('a85a72b0a240abecdf27f127aa75fd8663d6d5be')
6565
})
66+
67+
//file input - file not found
68+
test
69+
.stdout()
70+
.command(['hash', '-f', 'test/resources/tes.txt'])
71+
.it("cdt hash -f 'test/resources/tes.txt'", ctx => {
72+
// @ts-ignore
73+
expect(ctx.stderr).to.contain('Error: reading File')
74+
expect(ctx.stdout).to.contain('')
75+
})
76+
77+
//file input
78+
test
79+
.stdout()
80+
.command(['hash', '-f', 'test/resources/test.txt'])
81+
.it("cdt hash -f 'test/resources/test.txt'", ctx => {
82+
expect(ctx.stdout).to.contain('97ee6255ffc855e79e2324d5495b6538e29034f9')
83+
})
84+
6685
})

test/resources/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
2+
t is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

0 commit comments

Comments
 (0)