Skip to content

Commit 52445cb

Browse files
authored
Merge pull request #6 from codingtools/feature/file-hashing
Feature/file hashing
2 parents 71250d3 + ff2c70a commit 52445cb

File tree

5 files changed

+156
-14
lines changed

5 files changed

+156
-14
lines changed

.circleci/config.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
version: 2
3+
jobs:
4+
node-latest: &test
5+
docker:
6+
- image: node:latest
7+
working_directory: ~/cli
8+
steps:
9+
- checkout
10+
- restore_cache: &restore_cache
11+
keys:
12+
- v1-npm-{{checksum ".circleci/config.yml"}}-{{ checksum "package-lock.json"}}
13+
- v1-npm-{{checksum ".circleci/config.yml"}}
14+
- run:
15+
name: Install dependencies
16+
command: .circleci/greenkeeper
17+
- run: ./bin/run --version
18+
- run: ./bin/run --help
19+
- run:
20+
name: Testing
21+
command: npm test
22+
- run:
23+
name: Submitting code coverage to codecov
24+
command: |
25+
./node_modules/.bin/nyc report --reporter text-lcov > coverage.lcov
26+
curl -s https://codecov.io/bash | bash
27+
node-8:
28+
<<: *test
29+
docker:
30+
- image: node:8
31+
release:
32+
<<: *test
33+
steps:
34+
- add_ssh_keys
35+
- checkout
36+
- restore_cache: *restore_cache
37+
- run:
38+
name: Install dependencies
39+
command: |
40+
npm install -g @oclif/semantic-release@3 semantic-release@15
41+
npm install
42+
- run:
43+
name: Cutting release
44+
command: |
45+
semantic-release -e @oclif/semantic-release
46+
- save_cache:
47+
key: v1-npm-{{checksum ".circleci/config.yml"}}-{{checksum "package-lock.json"}}
48+
paths:
49+
- ~/cli/node_modules
50+
- ~/.npm
51+
- /usr/local/lib/node_modules
52+
53+
workflows:
54+
version: 2
55+
"cdt":
56+
jobs:
57+
- node-latest
58+
- node-8
59+
- release:
60+
context: org-global
61+
filters:
62+
branches: {only: master}
63+
requires:
64+
- node-latest
65+
- node-8

.circleci/greenkeeper

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
PATH=/usr/local/share/.config/npm/global/node_modules/.bin:$PATH
6+
7+
if [[ "$CIRCLE_BRANCH" != greenkeeper/* ]]; then
8+
npm install
9+
exit 0
10+
fi
11+
12+
if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then
13+
git config --global push.default simple
14+
git config --global user.email "$GIT_EMAIL"
15+
git config --global user.name "$GIT_USERNAME"
16+
fi
17+
18+
if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then
19+
npm install -g greenkeeper-lockfile@1
20+
fi
21+
22+
greenkeeper-lockfile-update
23+
npm install
24+
greenkeeper-lockfile-upload

src/commands/hash.ts

Lines changed: 45 additions & 13 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,32 +23,63 @@ 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
35+
36+
this.log(str)
37+
this.calculateHash(type, str)
38+
}
3339

40+
private calculateHash(type: string, str: string) {
3441
let hash: Hashes
3542
switch (type.toUpperCase()) {
3643
case 'SHA1':
37-
hash = new Hashes.SHA1(); break
44+
hash = new Hashes.SHA1()
45+
break
3846
case 'SHA256':
39-
hash = new Hashes.SHA256(); break
47+
hash = new Hashes.SHA256()
48+
break
4049
case 'SHA512':
41-
hash = new Hashes.SHA512(); break
50+
hash = new Hashes.SHA512()
51+
break
4252
case 'MD5':
43-
hash = new Hashes.MD5(); break
53+
hash = new Hashes.MD5()
54+
break
4455
case 'RMD160':
45-
hash = new Hashes.RMD160(); break
56+
hash = new Hashes.RMD160()
57+
break
4658
default:
4759
hash = undefined
4860
}
4961

50-
let hashed: string = hash.hex(str)
51-
this.log(`[HASH]: ${hashed}`)
62+
if (hash) {
63+
let hashed: string = hash.hex(str)
64+
this.log(`[HASH]: ${hashed}`)
65+
} else {
66+
this.log('[ERROR]: invalid hash type')
67+
}
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
83+
5284
}
5385
}

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 TODO: solve issue #4
68+
/*
69+
test
70+
.stderr()
71+
.command(['hash', '-f', 'test/resources/filenotfound.txt'])
72+
.it("cdt hash -f 'test/resources/filenotfound.txt'", ctx => {
73+
expect(ctx.stderr).to.contain('Error: reading File')
74+
})
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)