Skip to content

Commit 8f8a8bc

Browse files
author
James Pogran
authored
Merge pull request #632 from jpogran/GH-630-puppet-toolbar
GH 630 Puppet Toolbar and Puppet Facts View
2 parents 7a13475 + c0c8b87 commit 8f8a8bc

File tree

8 files changed

+155
-2
lines changed

8 files changed

+155
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- ([GH-630](https://github.com/puppetlabs/puppet-vscode/issues/630)) Add Puppet ToolBar
12+
- ([GH-631](https://github.com/puppetlabs/puppet-vscode/issues/631)) Add Puppet Facts View
13+
914
## [0.25.2] - 2020-03-27
1015

1116
### Fixed

assets/icons/dark/sync.svg

Lines changed: 3 additions & 0 deletions
Loading

assets/icons/light/sync.svg

Lines changed: 3 additions & 0 deletions
Loading

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,33 @@
197197
"command": "puppet-bolt.OpenUserInventoryFile",
198198
"title": "Open Bolt User Inventory File",
199199
"category": "Puppet"
200+
},
201+
{
202+
"command": "puppet.refreshFacts",
203+
"title": "Refresh",
204+
"icon": {
205+
"light": "assets/icons/light/sync.svg",
206+
"dark": "assets/icons/dark/sync.svg"
207+
}
200208
}
201209
],
210+
"viewsContainers": {
211+
"activitybar": [
212+
{
213+
"id": "puppet-toolbar",
214+
"title": "Puppet Toolbar",
215+
"icon": "images/puppet-dag-dark.svg"
216+
}
217+
]
218+
},
219+
"views": {
220+
"puppet-toolbar": [
221+
{
222+
"id": "puppetFacts",
223+
"name": "Facts"
224+
}
225+
]
226+
},
202227
"menus": {
203228
"commandPalette": [
204229
{
@@ -296,6 +321,13 @@
296321
"command": "extension.puppetResource",
297322
"group": "puppet"
298323
}
324+
],
325+
"view/title": [
326+
{
327+
"command": "puppet.refreshFacts",
328+
"when": "view == puppetFacts",
329+
"group": "navigation"
330+
}
299331
]
300332
},
301333
"configurationDefaults": {

src/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class AggregateConfiguration implements IAggregateConfiguration {
143143
if (settings.editorService.tcp.address === '127.0.0.1' ||
144144
settings.editorService.tcp.address === 'localhost' ||
145145
settings.editorService.tcp.address === '') {
146-
return ConnectionType.Remote;
146+
return ConnectionType.Local;
147147
} else {
148148
return ConnectionType.Remote;
149149
}

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { legacySettings, SettingsFromWorkspace } from './settings';
2121
import { reporter } from './telemetry';
2222
import { PuppetModuleHoverFeature } from './feature/PuppetModuleHoverFeature';
2323
import { PuppetNodeGraphFeature } from './feature/PuppetNodeGraphFeature';
24+
import { PuppetFactsProvider } from './views/facts';
2425

2526
const axios = require('axios');
2627

@@ -116,6 +117,10 @@ export function activate(context: vscode.ExtensionContext) {
116117
if (settings.hover.showMetadataInfo) {
117118
extensionFeatures.push(new PuppetModuleHoverFeature(extContext, logger));
118119
}
120+
121+
let facts = new PuppetFactsProvider(connectionHandler);
122+
vscode.window.registerTreeDataProvider('puppetFacts', facts);
123+
119124
}
120125

121126
export function deactivate() {

src/handlers/tcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class TcpConnectionHandler extends ConnectionHandler {
7676
this.config.workspace.editorService.tcp.address === 'localhost' ||
7777
this.config.workspace.editorService.tcp.address === ''
7878
) {
79-
return ConnectionType.Remote;
79+
return ConnectionType.Local;
8080
} else {
8181
return ConnectionType.Remote;
8282
}

src/views/facts.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {
2+
TreeItem,
3+
TreeItemCollapsibleState,
4+
ThemeIcon,
5+
TreeDataProvider,
6+
Event,
7+
EventEmitter,
8+
commands,
9+
ProviderResult,
10+
window,
11+
} from 'vscode';
12+
import { RequestType, RequestType0 } from 'vscode-languageclient';
13+
import { ConnectionHandler } from '../handler';
14+
import { PuppetVersionRequest } from '../messages';
15+
16+
class PuppetFact extends TreeItem {
17+
constructor(
18+
public readonly label: string,
19+
private value: string,
20+
public readonly collapsibleState: TreeItemCollapsibleState,
21+
public readonly children?: Array<[string, PuppetFact]>
22+
) {
23+
super(label, collapsibleState);
24+
if (children) {
25+
this.iconPath = ThemeIcon.Folder;
26+
} else {
27+
this.iconPath = ThemeIcon.File;
28+
}
29+
}
30+
31+
get tooltip(): string {
32+
return `${this.label}-${this.value}`;
33+
}
34+
35+
get description(): string {
36+
return this.value;
37+
}
38+
}
39+
40+
interface PuppetFactResponse {
41+
facts: string;
42+
error: string;
43+
}
44+
45+
export class PuppetFactsProvider implements TreeDataProvider<PuppetFact> {
46+
private elements: Array<[string, PuppetFact]> = [];
47+
private _onDidChangeTreeData: EventEmitter<PuppetFact | undefined> = new EventEmitter<PuppetFact | undefined>();
48+
readonly onDidChangeTreeData: Event<PuppetFact | undefined> = this._onDidChangeTreeData.event;
49+
50+
constructor(protected handler: ConnectionHandler) {
51+
commands.registerCommand('puppet.refreshFacts', () => this.refresh());
52+
}
53+
54+
refresh(): void {
55+
this._onDidChangeTreeData.fire();
56+
}
57+
58+
getTreeItem(element: PuppetFact): TreeItem | Thenable<PuppetFact> {
59+
return element;
60+
}
61+
62+
getChildren(element?: PuppetFact): Promise<PuppetFact[]> {
63+
if (element) {
64+
return Promise.resolve(element.children.map((e) => e[1]));
65+
} else {
66+
return this.getFactsFromLanguageServer();
67+
}
68+
}
69+
70+
private async getFactsFromLanguageServer(): Promise<PuppetFact[]> {
71+
/*
72+
this is problematic because we both store this and return the value
73+
but this allows us to cache the info for quick expands of the node.
74+
if we didn't cache, we would have to call out for each expand and getting
75+
facts is slow.
76+
*/
77+
await this.handler.languageClient.onReady();
78+
const results = await this.handler.languageClient.sendRequest(
79+
new RequestType0<PuppetFactResponse, void, void>('puppet/getFacts')
80+
);
81+
this.elements = this.toList(results.facts);
82+
return this.elements.map((e) => e[1]);
83+
}
84+
85+
getParent?(element: PuppetFact): ProviderResult<PuppetFact> {
86+
throw new Error('Method not implemented.');
87+
}
88+
89+
toList(data: any): Array<[string, PuppetFact]> {
90+
let things: Array<[string, PuppetFact]> = [];
91+
92+
for (let key of Object.keys(data)) {
93+
let value = data[key];
94+
if (Object.prototype.toString.call(value) === '[object Object]') {
95+
let children = this.toList(value);
96+
const item = new PuppetFact(key, value, TreeItemCollapsibleState.Collapsed, children);
97+
things.push([key, item]);
98+
} else {
99+
things.push([key, new PuppetFact(key, value.toString(), TreeItemCollapsibleState.None)]);
100+
}
101+
}
102+
103+
return things;
104+
}
105+
}

0 commit comments

Comments
 (0)