Skip to content

Feature/file hashing #6

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 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
version: 2
jobs:
node-latest: &test
docker:
- image: node:latest
working_directory: ~/cli
steps:
- checkout
- restore_cache: &restore_cache
keys:
- v1-npm-{{checksum ".circleci/config.yml"}}-{{ checksum "package-lock.json"}}
- v1-npm-{{checksum ".circleci/config.yml"}}
- run:
name: Install dependencies
command: .circleci/greenkeeper
- run: ./bin/run --version
- run: ./bin/run --help
- run:
name: Testing
command: npm test
- run:
name: Submitting code coverage to codecov
command: |
./node_modules/.bin/nyc report --reporter text-lcov > coverage.lcov
curl -s https://codecov.io/bash | bash
node-8:
<<: *test
docker:
- image: node:8
release:
<<: *test
steps:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run:
name: Install dependencies
command: |
npm install -g @oclif/semantic-release@3 semantic-release@15
npm install
- run:
name: Cutting release
command: |
semantic-release -e @oclif/semantic-release
- save_cache:
key: v1-npm-{{checksum ".circleci/config.yml"}}-{{checksum "package-lock.json"}}
paths:
- ~/cli/node_modules
- ~/.npm
- /usr/local/lib/node_modules

workflows:
version: 2
"cdt":
jobs:
- node-latest
- node-8
- release:
context: org-global
filters:
branches: {only: master}
requires:
- node-latest
- node-8
24 changes: 24 additions & 0 deletions .circleci/greenkeeper
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -ex

PATH=/usr/local/share/.config/npm/global/node_modules/.bin:$PATH

if [[ "$CIRCLE_BRANCH" != greenkeeper/* ]]; then
npm install
exit 0
fi

if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then
git config --global push.default simple
git config --global user.email "$GIT_EMAIL"
git config --global user.name "$GIT_USERNAME"
fi

if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then
npm install -g greenkeeper-lockfile@1
fi

greenkeeper-lockfile-update
npm install
greenkeeper-lockfile-upload
58 changes: 45 additions & 13 deletions src/commands/hash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Command, flags} from '@oclif/command'
import * as fs from 'fs'
// @ts-ignore
import * as Hashes from 'jshashes'

Expand All @@ -22,32 +23,63 @@ export default class Hash extends Command {

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

// if -s is not passed we will take it from args
// if -s or -f is not passed we will take it from args
let str = ''

let str: string

if (flags.string)
if (flags.string) //if -s given
str = flags.string
else
str = args.string
else if (flags.file) {
str = this.getStringFromFile(flags.file)
} else
str = args.string

this.log(str)
this.calculateHash(type, str)
}

private calculateHash(type: string, str: string) {
let hash: Hashes
switch (type.toUpperCase()) {
case 'SHA1':
hash = new Hashes.SHA1(); break
hash = new Hashes.SHA1()
break
case 'SHA256':
hash = new Hashes.SHA256(); break
hash = new Hashes.SHA256()
break
case 'SHA512':
hash = new Hashes.SHA512(); break
hash = new Hashes.SHA512()
break
case 'MD5':
hash = new Hashes.MD5(); break
hash = new Hashes.MD5()
break
case 'RMD160':
hash = new Hashes.RMD160(); break
hash = new Hashes.RMD160()
break
default:
hash = undefined
}

let hashed: string = hash.hex(str)
this.log(`[HASH]: ${hashed}`)
if (hash) {
let hashed: string = hash.hex(str)
this.log(`[HASH]: ${hashed}`)
} else {
this.log('[ERROR]: invalid hash type')
}
}

private getStringFromFile(filePath: string) {
let fileStr = ''
if (!fs.existsSync(filePath)) {
this.error('reading File') // this will output error and exit command
} else {
fileStr = fs.readFileSync(filePath, 'utf8')

// TODO: fix this Issue #3
if (fileStr.charAt(fileStr.length - 1) === '\n') {
fileStr = fileStr.substring(0, fileStr.length - 1)
}
}
return fileStr

}
}
21 changes: 20 additions & 1 deletion test/commands/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('hash', () => {
// passing sha1 as option
test
.stdout()
.command(['hash', 'ashish' ,'-t','sha1'])
.command(['hash', 'ashish' , '-t', 'sha1'])
.it("cdt hash 'ashish' -t 'sha1'", ctx => {
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
})
Expand Down Expand Up @@ -63,4 +63,23 @@ describe('hash', () => {
.it("cdt hash --type rmd160 'ashish'", ctx => {
expect(ctx.stdout).to.contain('a85a72b0a240abecdf27f127aa75fd8663d6d5be')
})

//file input - file not found TODO: solve issue #4
/*
test
.stderr()
.command(['hash', '-f', 'test/resources/filenotfound.txt'])
.it("cdt hash -f 'test/resources/filenotfound.txt'", ctx => {
expect(ctx.stderr).to.contain('Error: reading File')
})
*/

//file input
test
.stdout()
.command(['hash', '-f', 'test/resources/test.txt'])
.it("cdt hash -f 'test/resources/test.txt'", ctx => {
expect(ctx.stdout).to.contain('97ee6255ffc855e79e2324d5495b6538e29034f9')
})

})
2 changes: 2 additions & 0 deletions test/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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.
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).