Skip to content

Commit 8062ed6

Browse files
committed
limit commands to cases where they are dont throw error
1 parent bffa367 commit 8062ed6

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,42 +1327,42 @@
13271327
{
13281328
"command": "aws.ec2.openTerminal",
13291329
"group": "0@1",
1330-
"when": "viewItem == awsEc2Node"
1330+
"when": "viewItem =~ /^(awsEc2(Parent|Running|Stopped|Pending)Node)$/"
13311331
},
13321332
{
13331333
"command": "aws.ec2.openTerminal",
13341334
"group": "inline@1",
1335-
"when": "viewItem == awsEc2Node"
1335+
"when": "viewItem =~ /^(awsEc2(Parent|Running|Stopped|Pending)Node)$/"
13361336
},
13371337
{
13381338
"command": "aws.ec2.startInstance",
13391339
"group": "0@1",
1340-
"when": "viewItem == awsEc2Node"
1340+
"when": "viewItem =~ /^(awsEc2(Stopped|Pending)Node)$/"
13411341
},
13421342
{
13431343
"command": "aws.ec2.startInstance",
13441344
"group": "inline@1",
1345-
"when": "viewItem == awsEc2Node"
1345+
"when": "viewItem =~ /^(awsEc2(Stopped|Pending)Node)$/"
13461346
},
13471347
{
13481348
"command": "aws.ec2.stopInstance",
13491349
"group": "0@1",
1350-
"when": "viewItem == awsEc2Node"
1350+
"when": "viewItem =~ /^(awsEc2(Running|Pending)Node)$/"
13511351
},
13521352
{
13531353
"command": "aws.ec2.stopInstance",
13541354
"group": "inline@1",
1355-
"when": "viewItem == awsEc2Node"
1355+
"when": "viewItem =~ /^(awsEc2(Running|Pending)Node)$/"
13561356
},
13571357
{
13581358
"command": "aws.ec2.rebootInstance",
13591359
"group": "0@1",
1360-
"when": "viewItem == awsEc2Node"
1360+
"when": "viewItem =~ /^(awsEc2(Running|Pending)Node)$/"
13611361
},
13621362
{
13631363
"command": "aws.ec2.rebootInstance",
13641364
"group": "inline@1",
1365-
"when": "viewItem == awsEc2Node"
1365+
"when": "viewItem =~ /^(awsEc2(Running|Pending)Node)$/"
13661366
},
13671367
{
13681368
"command": "aws.ecr.createRepository",

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import { Ec2Instance } from '../../shared/clients/ec2Client'
1010
import globals from '../../shared/extensionGlobals'
1111
import { Ec2Selection, getIconCodeForInstanceStatus } from '../utils'
1212

13+
type Ec2InstanceNodeContext = 'awsEc2RunningNode' | 'awsEc2StoppedNode' | 'awsEc2PendingNode'
14+
1315
export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode {
1416
public constructor(
1517
public override readonly regionCode: string,
1618
private readonly partitionId: string,
17-
private instance: Ec2Instance,
18-
public override readonly contextValue: string
19+
private instance: Ec2Instance
1920
) {
2021
super('')
2122
this.update(instance)
@@ -24,10 +25,23 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
2425
public update(newInstance: Ec2Instance) {
2526
this.setInstance(newInstance)
2627
this.label = `${this.name} (${this.InstanceId})`
28+
this.contextValue = this.getContext()
2729
this.iconPath = new vscode.ThemeIcon(getIconCodeForInstanceStatus(this.instance))
2830
this.tooltip = `${this.name}\n${this.InstanceId}\n${this.instance.status}\n${this.arn}`
2931
}
3032

33+
private getContext(): Ec2InstanceNodeContext {
34+
if (this.instance.status == 'running') {
35+
return 'awsEc2RunningNode'
36+
}
37+
38+
if (this.instance.status == 'stopped') {
39+
return 'awsEc2StoppedNode'
40+
}
41+
42+
return 'awsEc2PendingNode'
43+
}
44+
3145
public setInstance(newInstance: Ec2Instance) {
3246
this.instance = newInstance
3347
}

src/ec2/explorer/ec2ParentNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import { Ec2InstanceNode } from './ec2InstanceNode'
1010
import { Ec2Client } from '../../shared/clients/ec2Client'
1111
import { updateInPlace } from '../../shared/utilities/collectionUtils'
1212

13-
export const contextValueEc2 = 'awsEc2Node'
13+
export const parentContextValue = 'awsEc2ParentNode'
1414
export type Ec2Node = Ec2InstanceNode | Ec2ParentNode
1515

1616
export class Ec2ParentNode extends AWSTreeNodeBase {
1717
protected readonly placeHolderMessage = '[No EC2 Instances Found]'
1818
protected readonly ec2InstanceNodes: Map<string, Ec2InstanceNode>
19-
public override readonly contextValue: string = contextValueEc2
19+
public override readonly contextValue: string = parentContextValue
2020

2121
public constructor(
2222
public override readonly regionCode: string,
@@ -45,7 +45,7 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
4545
this.ec2InstanceNodes,
4646
ec2Instances.keys(),
4747
key => this.ec2InstanceNodes.get(key)!.update(ec2Instances.get(key)!),
48-
key => new Ec2InstanceNode(this.regionCode, this.partitionId, ec2Instances.get(key)!, contextValueEc2)
48+
key => new Ec2InstanceNode(this.regionCode, this.partitionId, ec2Instances.get(key)!)
4949
)
5050
}
5151
}

src/test/ec2/explorer/ec2InstanceNode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as assert from 'assert'
77
import { Ec2InstanceNode } from '../../../ec2/explorer/ec2InstanceNode'
88
import { Ec2Instance, getNameOfInstance } from '../../../shared/clients/ec2Client'
9-
import { contextValueEc2 } from '../../../ec2/explorer/ec2ParentNode'
9+
import { parentContextValue } from '../../../ec2/explorer/ec2ParentNode'
1010

1111
describe('ec2InstanceNode', function () {
1212
let testNode: Ec2InstanceNode
@@ -23,7 +23,7 @@ describe('ec2InstanceNode', function () {
2323
],
2424
}
2525

26-
testNode = new Ec2InstanceNode('testRegion', 'testPartition', testInstance, contextValueEc2)
26+
testNode = new Ec2InstanceNode('testRegion', 'testPartition', testInstance, parentContextValue)
2727
})
2828

2929
it('instantiates without issue', async function () {

src/test/ec2/explorer/ec2ParentNode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { Ec2ParentNode, contextValueEc2 } from '../../../ec2/explorer/ec2ParentNode'
7+
import { Ec2ParentNode, parentContextValue } from '../../../ec2/explorer/ec2ParentNode'
88
import { stub } from '../../utilities/stubber'
99
import { Ec2Client, Ec2Instance } from '../../../shared/clients/ec2Client'
1010
import { intoCollection } from '../../../shared/utilities/collectionUtils'
@@ -65,7 +65,7 @@ describe('ec2ParentNode', function () {
6565
const childNodes = await testNode.getChildren()
6666

6767
childNodes.forEach(node =>
68-
assert.strictEqual(node.contextValue, contextValueEc2, 'expected the node to have a ec2 contextValue')
68+
assert.strictEqual(node.contextValue, parentContextValue, 'expected the node to have a ec2 contextValue')
6969
)
7070
})
7171

0 commit comments

Comments
 (0)