-
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 4 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,34 @@ | ||
| import { HistoryCommand } from './history-command' | ||
| import { getHistory } from '../../service/history' | ||
| import Table from 'cli-table3' | ||
| import { LeafCommand } from 'furious-commander' | ||
| import { ellipsis } from '../../utils/text' | ||
|
|
||
| export class List extends HistoryCommand implements LeafCommand { | ||
| public readonly name = 'list' | ||
|
|
||
| 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 = getHistory(this.console) | ||
| table.push( | ||
| ...history.map(h => [ | ||
| 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 { getHistory } from '../../service/history' | ||
| import { Argument, LeafCommand } from 'furious-commander' | ||
| import { createKeyValue } from '../../utils/text' | ||
| import { exit } from 'process' | ||
|
|
||
| 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, | ||
| autocompletePath: true, | ||
|
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. Remove, this is not a path. |
||
| }) | ||
| public index!: string | ||
|
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. You can add You can remove the |
||
|
|
||
| public run() { | ||
| super.init() | ||
| const history = getHistory(this.console) | ||
| const historyItem = history.find(item => item.index === parseInt(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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
| import { homedir } from 'os' | ||
| import { join } from 'path' | ||
| import { History } from './types/history' | ||
| import { exit } from 'process' | ||
| import { CommandLog } from '../../command/root-command/command-log' | ||
|
|
||
| const historyFilePath = join(homedir(), '.swarm-upload-history.json') | ||
|
|
||
| export function getHistory(console: CommandLog): History[] { | ||
| if (!existsSync(historyFilePath)) { | ||
| return [] | ||
| } | ||
| const historyData = readFileSync(historyFilePath) | ||
| try { | ||
| const historyList = JSON.parse(historyData.toString()) as History[] | ||
|
|
||
| return historyList | ||
| } catch (err) { | ||
| console.error(`There has been an error parsing history JSON from path: '${historyFilePath}'`) | ||
|
|
||
| exit(1) | ||
| } | ||
| } | ||
|
|
||
| export function saveHistory(historyEntry: History, console: CommandLog) { | ||
| const history = getHistory(console) | ||
| historyEntry.index = history.length + 1 | ||
| history.push(historyEntry) | ||
| writeFileSync(historyFilePath, JSON.stringify(history)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export type History = { | ||
| index?: number | ||
| timestamp: number | ||
| reference: string | ||
| stamp: string | ||
| path: string | null | ||
| uploadType: 'file' | 'folder' | 'stdin' | ||
| feedAddress?: string | null | ||
| feedIdentity?: string | null | ||
| } |
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