-
Notifications
You must be signed in to change notification settings - Fork 24
feat: implement upload history #679
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
Changes from 10 commits
a17aea2
bc7ebf0
ac7c5d2
90fbe04
22d75e7
0339b57
e6448c8
e545a7f
d78a7d7
458eaf3
a0f3c2f
351aab4
b0a4693
2645f16
4cf5f86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { RootCommand } from '../root-command' | ||
|
|
||
| export class HistoryCommand extends RootCommand {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { GroupCommand } from 'furious-commander' | ||
| import { List } from './list' | ||
| import { Show } from './show' | ||
|
|
||
| export class History implements GroupCommand { | ||
| public readonly name = 'history' | ||
|
|
||
| public readonly description = 'Get upload history' | ||
|
|
||
| public subCommandClasses = [List, Show] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { HistoryCommand } from './history-command' | ||
| import { History } from '../../service/history' | ||
| import Table from 'cli-table3' | ||
| import { LeafCommand } from 'furious-commander' | ||
| import { ellipsis } from '../../utils/text' | ||
| import { HistoryItem } from '../../service/history/types/historyItem' | ||
|
|
||
| export class List extends HistoryCommand implements LeafCommand { | ||
| public readonly name = 'list' | ||
|
|
||
| public readonly alias = 'ls' | ||
|
|
||
| public readonly description = 'Get upload history list' | ||
|
|
||
| public run() { | ||
| super.init() | ||
| const table = new Table({ | ||
| head: ['Index', 'Timestamp', 'Reference', 'Postage stamp batch ID', 'File path', 'Upload type'], | ||
| style: { | ||
| head: ['green', 'bold'], | ||
| }, | ||
| wordWrap: true, | ||
| }) | ||
|
|
||
| const history = new History(this.commandConfig.configFolderPath, this.console) | ||
| table.push( | ||
| ...history | ||
| .getItems() | ||
| .map((h: HistoryItem) => [ | ||
| h.index, | ||
| new Date(h.timestamp).toLocaleString(), | ||
| h.reference.slice(0, 12), | ||
| ellipsis(h.stamp, 6, -6), | ||
| h.path, | ||
| h.uploadType, | ||
| ]), | ||
| ) | ||
| this.console.log(table.toString()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { HistoryCommand } from './history-command' | ||
| import { Argument, LeafCommand } from 'furious-commander' | ||
| import { createKeyValue } from '../../utils/text' | ||
| import { exit } from 'process' | ||
| import { History } from '../../service/history' | ||
|
|
||
| export class Show extends HistoryCommand implements LeafCommand { | ||
| public readonly name = 'show' | ||
|
|
||
| public readonly description = 'Get upload history item' | ||
|
|
||
| @Argument({ | ||
| key: 'index', | ||
| description: 'Index of the history item', | ||
| required: true, | ||
| type: 'number', | ||
| }) | ||
| public index!: number | ||
|
|
||
| public run() { | ||
| super.init() | ||
| const history = new History(this.commandConfig.configFolderPath, this.console) | ||
| const historyItem = history.getItemByIndex(this.index) | ||
|
|
||
| if (historyItem === undefined) { | ||
| this.console.error(`Cound not find history item with index '${this.index}'`) | ||
| exit(1) | ||
| } | ||
| this.console.log(createKeyValue('Timestamp', new Date(historyItem.timestamp).toUTCString())) | ||
| this.console.log(createKeyValue('Swarm hash', historyItem.reference)) | ||
| this.console.log(createKeyValue('Stamp ID', historyItem.stamp)) | ||
| this.console.log(createKeyValue('Upload type', historyItem.uploadType)) | ||
|
|
||
| if (historyItem.path) { | ||
| this.console.log(createKeyValue('Path', historyItem.path)) | ||
| } | ||
|
|
||
| if (historyItem.feedAddress) { | ||
| this.console.log(createKeyValue('Feed address', historyItem.feedAddress)) | ||
| } | ||
|
|
||
| if (historyItem.feedIdentity) { | ||
| this.console.log(createKeyValue('Feed identity', historyItem.feedIdentity)) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { createSpinner } from '../utils/spinner' | |
| import { createKeyValue, warningSymbol, warningText } from '../utils/text' | ||
| import { RootCommand } from './root-command' | ||
| import { VerbosityLevel } from './root-command/command-log' | ||
| import { History } from '../service/history' | ||
|
|
||
| export class Upload extends RootCommand implements LeafCommand { | ||
| public readonly name = 'upload' | ||
|
|
@@ -166,7 +167,8 @@ export class Upload extends RootCommand implements LeafCommand { | |
| const url = await this.uploadAnyWithSpinner(tag, uploadingFolder) | ||
|
|
||
| this.console.dim('Data has been sent to the Bee node successfully!') | ||
| this.console.log(createKeyValue('Swarm hash', this.result.getOrThrow().toHex())) | ||
| const swarmHash = this.result.getOrThrow().toHex() | ||
| this.console.log(createKeyValue('Swarm hash', swarmHash)) | ||
|
|
||
| if (this.act) { | ||
| this.console.log(createKeyValue('Swarm history address', this.historyAddress.getOrThrow().toHex())) | ||
|
|
@@ -180,6 +182,18 @@ export class Upload extends RootCommand implements LeafCommand { | |
| this.console.dim('Uploading was successful!') | ||
| this.console.log(createKeyValue('URL', url)) | ||
|
|
||
| const history = new History(this.commandConfig.configFolderPath, this.console) | ||
|
|
||
| if (!usedFromOtherCommand) { | ||
| history.addItem({ | ||
| timestamp: Date.now(), | ||
| reference: swarmHash, | ||
| stamp: this.stamp, | ||
| path: this.path, | ||
| uploadType: this.path ? 'file' : 'stdin', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please see and use ( |
||
| }) | ||
| } | ||
|
|
||
| if (!usedFromOtherCommand) { | ||
| this.console.quiet(this.result.getOrThrow().toHex()) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
| import { join } from 'path' | ||
| import { HistoryItem } from './types/historyItem' | ||
| import { exit } from 'process' | ||
| import { CommandLog } from '../../command/root-command/command-log' | ||
|
|
||
| export class History { | ||
| public configFolderPath: string | ||
| private console: CommandLog | ||
|
|
||
| constructor(configFolderPath: string, console: CommandLog) { | ||
| this.configFolderPath = configFolderPath | ||
| this.console = console | ||
| } | ||
|
|
||
| public getItems(): HistoryItem[] { | ||
| const historyFilePath = this.getHistoryFilePath() | ||
|
|
||
| if (!existsSync(historyFilePath)) { | ||
| return [] | ||
| } | ||
| const historyData = readFileSync(historyFilePath) | ||
| try { | ||
| const historyList = JSON.parse(historyData.toString()) as HistoryItem[] | ||
|
|
||
| return historyList | ||
| } catch (err) { | ||
| this.console.error(`There has been an error parsing history JSON from path: '${historyFilePath}'`) | ||
|
|
||
| exit(1) | ||
| } | ||
| } | ||
|
|
||
| public getItemByIndex(index: number): HistoryItem | undefined { | ||
| return this.getItems().find((item: HistoryItem) => item.index === index) | ||
| } | ||
|
|
||
| public addItem(item: HistoryItem) { | ||
| const history = this.getItems() | ||
| item.index = history.length + 1 | ||
| history.push(item) | ||
| writeFileSync(this.getHistoryFilePath(), JSON.stringify(history)) | ||
| } | ||
|
|
||
| public getHistoryFilePath(): string { | ||
| return join(this.configFolderPath, 'upload-history.json') | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add alias
ls