Skip to content

Commit 99846e7

Browse files
slapec93Gergely Békési
andauthored
feat: modify access list command and introduce access show (#747)
* feat: rename list command to show * feat: add new list command * feat: add help text in list output * fix: update spec --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org>
1 parent 93eb222 commit 99846e7

4 files changed

Lines changed: 77 additions & 29 deletions

File tree

src/command/access/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { History } from './history'
44
import { Init } from './init'
55
import { List } from './list'
66
import { Revoke } from './revoke'
7+
import { Show } from './show'
78

89
export class Access implements GroupCommand {
910
public readonly name = 'access'
1011

1112
public readonly description = 'Share access to your uploaded files/folders'
1213

13-
public subCommandClasses = [Init, Grant, Revoke, List, History]
14+
public subCommandClasses = [Init, Grant, Revoke, Show, History, List]
1415
}

src/command/access/list.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
1-
import { LeafCommand, Option } from 'furious-commander'
2-
import { exit } from 'process'
1+
import { LeafCommand } from 'furious-commander'
32
import { AccessHistory } from '../../service/access'
4-
import { granteeListNameProperties } from '../../utils/option'
5-
import { errorText } from '../../utils/text'
63
import { AccessCommand } from './access-command'
74

85
export class List extends AccessCommand implements LeafCommand {
96
public readonly name = 'list'
107

11-
public readonly description = 'List grantees of an existing grantee list'
8+
public readonly description = 'List grantee lists'
129

13-
@Option(granteeListNameProperties)
14-
public listName!: string
15-
16-
public async run(): Promise<void> {
10+
public run() {
1711
super.init()
1812

1913
const accessHistory = new AccessHistory(this.commandConfig, this.console)
20-
const lastHistoryEvent = accessHistory.getEvents(this.listName).sort((a, b) => b.createdAt - a.createdAt)[0]
21-
22-
if (!lastHistoryEvent) {
23-
this.console.error(errorText(`Grantee list with name '${this.listName}' does not exist!`))
24-
25-
exit(1)
26-
}
14+
const granteeListNames = Object.keys(accessHistory.getHistory())
2715

28-
const response = await this.bee.getGrantees(lastHistoryEvent.granteeListRef)
29-
30-
if (response.grantees.length === 0) {
31-
this.console.log(`Grantee list '${this.listName}' has no grantees.`)
16+
if (granteeListNames.length === 0) {
17+
this.console.log('No grantee lists found.')
3218

3319
return
3420
}
35-
this.console.log(`Grantees of list '${this.listName}':`)
36-
for (const grantee of response.grantees) {
37-
this.console.log(grantee.toCompressedHex())
21+
22+
this.console.log(`Grantee lists: \n`)
23+
for (const listName of granteeListNames) {
24+
this.console.log(listName)
3825
}
26+
27+
this.console.log('Run `swarm-cli access show --list-name <name>` to view grantees for a specific list')
3928
}
4029
}

src/command/access/show.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { LeafCommand, Option } from 'furious-commander'
2+
import { exit } from 'process'
3+
import { AccessHistory } from '../../service/access'
4+
import { granteeListNameProperties } from '../../utils/option'
5+
import { errorText } from '../../utils/text'
6+
import { AccessCommand } from './access-command'
7+
8+
export class Show extends AccessCommand implements LeafCommand {
9+
public readonly name = 'show'
10+
11+
public readonly description = 'Show grantees of an existing grantee list'
12+
13+
@Option(granteeListNameProperties)
14+
public listName!: string
15+
16+
public async run(): Promise<void> {
17+
super.init()
18+
19+
const accessHistory = new AccessHistory(this.commandConfig, this.console)
20+
const lastHistoryEvent = accessHistory.getEvents(this.listName).sort((a, b) => b.createdAt - a.createdAt)[0]
21+
22+
if (!lastHistoryEvent) {
23+
this.console.error(errorText(`Grantee list with name '${this.listName}' does not exist!`))
24+
25+
exit(1)
26+
}
27+
28+
const response = await this.bee.getGrantees(lastHistoryEvent.granteeListRef)
29+
30+
if (response.grantees.length === 0) {
31+
this.console.log(`Grantee list '${this.listName}' has no grantees.`)
32+
33+
return
34+
}
35+
this.console.log(`Grantees of list '${this.listName}':`)
36+
for (const grantee of response.grantees) {
37+
this.console.log(grantee.toCompressedHex())
38+
}
39+
}
40+
}

test/command/access.spec.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,50 @@ describeCommand(
123123
})
124124
})
125125

126-
describe('list', () => {
127-
it('should list all grantees in the list', async () => {
126+
describe('show', () => {
127+
it('should show all grantees in the list', async () => {
128128
const granteePubKey = await getPublicAddress('http://localhost:21633')
129129
await invokeTestCli(['access', 'init', ...getStampOption(), '-n', 'test-access'])
130130
await System.sleepMillis(1000)
131131
await invokeTestCli(['access', 'grant', '--list-name', 'test-access', '--grantee', granteePubKey])
132132
await System.sleepMillis(1000)
133-
await invokeTestCli(['access', 'list', '--list-name', 'test-access'])
133+
await invokeTestCli(['access', 'show', '--list-name', 'test-access'])
134134
expect(getNthLastMessage(2)).toContain(`Grantees of list 'test-access':`)
135135
expect(getLastMessage()).toContain(granteePubKey)
136136

137137
await invokeTestCli(['access', 'revoke', '--list-name', 'test-access', '--grantee', granteePubKey])
138138
await System.sleepMillis(1000)
139-
await invokeTestCli(['access', 'list', '--list-name', 'test-access'])
139+
await invokeTestCli(['access', 'show', '--list-name', 'test-access'])
140140
expect(getLastMessage()).toContain("Grantee list 'test-access' has no grantees.")
141141
})
142142

143143
describe('when grantee list does not exist', () => {
144144
it('should show error message', async () => {
145-
await invokeTestCli(['access', 'list', '-n', 'nonexistent-list'])
145+
await invokeTestCli(['access', 'show', '-n', 'nonexistent-list'])
146146
expect(consoleMessages[0]).toContain("Grantee list with name 'nonexistent-list' does not exist!")
147147
expect(consoleMessages[1]).toContain('process.exit() was called with code 1')
148148
})
149149
})
150150
})
151151

152+
describe('list', () => {
153+
it('should show all grantee list names', async () => {
154+
await invokeTestCli(['access', 'init', ...getStampOption(), '-n', 'test-access'])
155+
await invokeTestCli(['access', 'init', ...getStampOption(), '-n', 'test-access-2'])
156+
await System.sleepMillis(1000)
157+
await invokeTestCli(['access', 'list'])
158+
expect(getNthLastMessage(3)).toContain('test-access')
159+
expect(getNthLastMessage(2)).toContain('test-access-2')
160+
})
161+
162+
describe('when grantee list does not exist', () => {
163+
it('should show error message', async () => {
164+
await invokeTestCli(['access', 'list'])
165+
expect(consoleMessages[0]).toContain('No grantee lists found.')
166+
})
167+
})
168+
})
169+
152170
describe('history', () => {
153171
it('should show the history of operations on a grantee list', async () => {
154172
const granteePubKey = await getPublicAddress('http://localhost:21633')

0 commit comments

Comments
 (0)