Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Encode and decode Hashes from and to base58 #138

Merged
merged 4 commits into from
Aug 30, 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
97 changes: 57 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"is-git-url": "^1.0.0",
"js-yaml": "^3.13.1",
"lodash.pick": "^4.4.0",
"mesg-js": "^4.3.0-beta",
"mesg-js": "^4.3.0-beta.1",
"node-docker-api": "^1.1.22",
"rimraf": "^2.6.3",
"tar": "^4.4.8",
Expand Down
5 changes: 3 additions & 2 deletions src/commands/service/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {flags} from '@oclif/command'
import {ServiceCreateOutputs} from 'mesg-js/lib/api'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'

Expand Down Expand Up @@ -27,10 +28,10 @@ export default class ServiceCreate extends Command {
this.spinner.start('Create service')
const resp = await this.api.service.create(JSON.parse(args.DEFINITION))
if (!resp.hash) { throw new Error('invalid response') }
this.spinner.stop(resp.hash)
this.spinner.stop(base58.encode(resp.hash))
if (flags.start) {
this.spinner.start('Starting service')
const start = await ServiceStart.run([resp.hash])
const start = await ServiceStart.run([base58.encode(resp.hash)])
this.spinner.stop(start.hash)
}
return resp
Expand Down
3 changes: 2 additions & 1 deletion src/commands/service/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {flags} from '@oclif/command'
import cli from 'cli-ux'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'
import serviceResolver from '../../utils/service-resolver'
Expand All @@ -25,7 +26,7 @@ export default class ServiceDelete extends Command {
this.spinner.start('Deleting service(s)')
for (const hash of argv) {
const serviceHash = await serviceResolver(this.api, hash)
this.spinner.status = serviceHash
this.spinner.status = base58.encode(serviceHash)
await this.api.service.delete({hash: serviceHash})
}
this.spinner.stop()
Expand Down
12 changes: 7 additions & 5 deletions src/commands/service/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Service} from 'mesg-js/lib/api'
import {hash} from 'mesg-js/lib/api/types'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'
import {IsAlreadyExistsError} from '../../utils/error'
Expand Down Expand Up @@ -34,16 +36,16 @@ export default class ServiceDev extends Command {
const definition = await ServiceCompile.run([args.SERVICE, '--silent'])
const serviceHash = await this.createService(definition)
const instanceHash = await this.startService(serviceHash, flags.env)
const stream = await ServiceLog.run([instanceHash])
const stream = await ServiceLog.run([base58.encode(instanceHash)])

process.once('SIGINT', async () => {
stream.destroy()
if (this.instanceCreated) await ServiceStop.run([instanceHash])
if (this.serviceCreated) await ServiceDelete.run([serviceHash, '--confirm'])
if (this.instanceCreated) await ServiceStop.run([base58.encode(instanceHash)])
if (this.serviceCreated) await ServiceDelete.run([base58.encode(serviceHash), '--confirm'])
})
}

async createService(definition: Service): Promise<string> {
async createService(definition: Service): Promise<hash> {
try {
const service = await this.api.service.create(definition)
if (!service.hash) throw new Error('invalid hash')
Expand All @@ -56,7 +58,7 @@ export default class ServiceDev extends Command {
}
}

async startService(serviceHash: string, env: string[]): Promise<string> {
async startService(serviceHash: hash, env: string[]): Promise<hash> {
try {
const instance = await this.api.instance.create({
serviceHash,
Expand Down
3 changes: 2 additions & 1 deletion src/commands/service/execute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {flags} from '@oclif/command'
import {readFileSync} from 'fs'
import {ExecutionCreateOutputs} from 'mesg-js/lib/api'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'
import instanceResolver from '../../utils/instance-resolver'
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class ServiceExecute extends Command {
hash: instanceHash
})
if (!instance.serviceHash) { throw new Error('invalid service hash') }
const service = await ServiceDetail.run([instance.serviceHash, '--silent'])
const service = await ServiceDetail.run([base58.encode(instance.serviceHash), '--silent'])

const task = service.tasks.find((x: any) => x.key === args.TASK)
if (!task) {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/service/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cli from 'cli-ux'
import {Instance, Service} from 'mesg-js/lib/api/types'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'

Expand All @@ -17,13 +18,13 @@ export default class ServiceList extends Command {
const {instances} = await this.api.instance.list({})
if (!services) return []
cli.table<Service>(services, {
hash: {header: 'HASH', get: x => x.hash},
hash: {header: 'HASH', get: x => x.hash ? base58.encode(x.hash) : ''},
sid: {header: 'SID', get: x => x.sid},
instances: {
header: 'INSTANCES',
get: x => ((instances || []) as Instance[])
.filter(y => y.serviceHash === x.hash)
.map(y => y.hash)
.filter(y => y.serviceHash && x.hash && y.serviceHash.toString() === x.hash.toString())
.map(y => y.hash && base58.encode(y.hash))
.join('\n')
}
}, {printLine: this.log, ...flags})
Expand Down
3 changes: 2 additions & 1 deletion src/commands/service/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {flags} from '@oclif/command'
import {InstanceCreateOutputs} from 'mesg-js/lib/api'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'
import serviceResolver from '../../utils/service-resolver'
Expand Down Expand Up @@ -30,7 +31,7 @@ export default class ServiceStart extends Command {
env: flags.env
})
if (!instance.hash) throw new Error('invalid instance')
this.spinner.stop(instance.hash)
this.spinner.stop(base58.encode(instance.hash))
return instance
}
}
14 changes: 8 additions & 6 deletions src/commands/workflow/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Command from '../../root-command'
import * as compile from '../../utils/compiler'
import {IsAlreadyExistsError} from '../../utils/error'
import ServiceCompile from '../service/compile'
import { hash } from 'mesg-js/lib/api/types';

export default class WorkflowCompile extends Command {
static description = 'Compile a workflow'
Expand Down Expand Up @@ -42,10 +43,10 @@ export default class WorkflowCompile extends Command {
return definition
}

async sourceToInstance(dir: string, src: string): Promise<string> {
async sourceToInstance(dir: string, src: string): Promise<hash> {
const directory = join(dirname(dir), src)
const definition = await ServiceCompile.run([existsSync(directory) ? directory : src, '--silent'])
let hash: string | null = null
let hash: hash
try {
const resp = await this.api.service.create(definition)
if (!resp.hash) throw new Error('invalid hash')
Expand All @@ -58,12 +59,13 @@ export default class WorkflowCompile extends Command {
return this.serviceToInstance(hash)
}

async serviceToInstance(key: string): Promise<string> {
async serviceToInstance(sidOrHash: hash | string): Promise<hash> {
const {services} = await this.api.service.list({})
if (!services) throw new Error('no services deployed, please deploy your service first')
const match = services.filter(x => x.hash === key || x.sid === key)
if (!match || match.length === 0) throw new Error(`cannot find any service with the following: ${key}`)
if (match.length > 1) throw new Error(`multiple services match the following sid: ${key}, provide a service's hash instead`)
const sidOrHashStr = sidOrHash.toString()
const match = services.filter(x => x.hash && x.hash.toString() === sidOrHashStr || x.sid && x.sid.toString() === sidOrHashStr)
if (!match || match.length === 0) throw new Error(`cannot find any service with the following: ${sidOrHashStr}`)
if (match.length > 1) throw new Error(`multiple services match the following sid: ${sidOrHashStr}, provide a service's hash instead`)
const service = match[0]
if (!service.hash) throw new Error('invalid service')
const {instances} = await this.api.instance.list({serviceHash: service.hash})
Expand Down
3 changes: 2 additions & 1 deletion src/commands/workflow/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {WorkflowCreateOutputs} from 'mesg-js/lib/api'
import * as base58 from 'mesg-js/lib/util/base58'

import Command from '../../root-command'

Expand All @@ -20,7 +21,7 @@ export default class WorkflowCreate extends Command {
this.spinner.start('Create workflow')
const resp = await this.api.workflow.create(JSON.parse(args.DEFINITION))
if (!resp.hash) { throw new Error('invalid response') }
this.spinner.stop(resp.hash)
this.spinner.stop(base58.encode(resp.hash))
return resp
}
}
Loading