Skip to content

Commit

Permalink
Add View File command
Browse files Browse the repository at this point in the history
  • Loading branch information
sandipchitale committed Nov 1, 2019
1 parent 6ddf65b commit b94eb8c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.0.2

- Add command `k8s.pod.container.file.view` (View file) - Show the content of the file in editor.

## 1.0.1

- Add screenshot and description
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ It basically starts at `/` by running the following Kubectl command in the Pod t

`> kubectl exec -it pod-name -- ls -f /`

As the treed nodes for directories are expanded e.g. `/etc/`, it runs
As the treed nodes for directories are expanded e.g. `/etc/`, it basically runs:

`> kubectl exec -it pod-name -- ls -f /etc/`

It also supports the following command:

`k8s.pod.container.file.view` (View file) - Show the content of the file in editor. It basically run:

`> kubectl exec -it pod-name -- cat /path/to/file`


## Requirements

Expand All @@ -35,4 +41,8 @@ Initial release.

### 1.0.1

Add screenshot and description.
Add screenshot and description.

### 1.0.2

Add command `k8s.pod.container.file.view` (View file) - Show the content of the file in editor.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Kubernetes Pod File System Explorer",
"icon": "images/icon.png",
"description": "Kubernetes Pod File System Explorer.",
"version": "1.0.1",
"version": "1.0.2",
"keywords": [
"kubernetes"
],
Expand Down Expand Up @@ -34,16 +34,16 @@
"contributes": {
"commands": [
{
"command": "k8s.container.explorer",
"title": "Pod ls -F",
"command": "k8s.pod.container.file.view",
"title": "View file",
"category": "Kubernetes"
}
],
"menus": {
"view/item/context": [
{
"command": "k8s.container.explorer",
"when": "viewItem =~ /vsKubernetes\\.resource\\.(pod)/i"
"command": "k8s.pod.container.file.view",
"when": "view == extension.vsKubernetesExplorer"
}
]
}
Expand Down
58 changes: 38 additions & 20 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import * as k8s from 'vscode-kubernetes-tools-api';


class FileNode implements k8s.ClusterExplorerV1.Node {
private kubectl: k8s.KubectlV1;
private podName: string;
private path: string;
private name: string;

constructor(path: string, name: string) {
constructor(kubectl: k8s.KubectlV1, podName: string, path: string, name: string) {
this.kubectl = kubectl;
this.podName = podName;
this.path = path;
this.name = name;
}
Expand All @@ -23,6 +27,24 @@ class FileNode implements k8s.ClusterExplorerV1.Node {
treeItem.iconPath = vscode.ThemeIcon.File;
return treeItem;
}

async viewFile() {
const catResult = await this.kubectl.invokeCommand(`exec -it ${this.podName} -- cat ${this.path}${this.name}`);
if (!catResult || catResult.code !== 0) {
vscode.window.showErrorMessage(`Can't get contents of file: ${catResult ? catResult.stderr : 'unable to run cat command on file ${this.path}${this.name}'}`);
return;
}
const catCommandOutput = catResult.stdout;
if (catCommandOutput.trim().length > 0) {
vscode.workspace.openTextDocument({
content: catCommandOutput
}).then((doc) => {
vscode.window.showTextDocument(doc).then((editor) => {
vscode.window.showInformationMessage(`Showing ${this.podName}:${this.path}${this.name}`);
});
});
}
}
}

class FolderNode implements k8s.ClusterExplorerV1.Node {
Expand Down Expand Up @@ -52,7 +74,7 @@ class FolderNode implements k8s.ClusterExplorerV1.Node {
if (fileName.endsWith('/')) {
return new FolderNode(this.kubectl, this.podName, this.path + this.name, fileName)
} else {
return new FileNode(this.path+this.name, fileName)
return new FileNode(this.kubectl, this.podName, this.path+this.name, fileName)
}
});
}
Expand Down Expand Up @@ -82,23 +104,6 @@ class FileSystemNodeContributor {
async getChildren(parent: k8s.ClusterExplorerV1.ClusterExplorerNode | undefined): Promise<k8s.ClusterExplorerV1.Node[]> {
if (parent && parent.nodeType === 'resource' && parent.resourceKind.manifestKind === 'Pod') {
return [ new FolderNode(this.kubectl, parent.name, '/', '') ];

// // Example of using the kubectl API to invoke a command
// const lsResult = await this.kubectl.invokeCommand(`exec -it ${parent.name} -- ls -F /`);

// if (!lsResult || lsResult.code !== 0) {
// vscode.window.showErrorMessage(`Can't get resource usage: ${lsResult ? lsResult.stderr : 'unable to run kubectl'}`);
// return;
// }
// const lsCommandOutput = lsResult.stdout;
// const fileNames = lsCommandOutput.split('\n');
// return fileNames.map((fileName) => {
// if (fileName.endsWith('/')) {
// return new FolderNode(this.kubectl, parent.name, '/', fileName)
// } else {
// return new FileNode(fileName)
// }
// });
}
return [];
}
Expand All @@ -107,7 +112,7 @@ class FileSystemNodeContributor {
export async function activate(context: vscode.ExtensionContext) {
const explorer = await k8s.extension.clusterExplorer.v1;
if (!explorer.available) {
vscode.window.showErrorMessage(`Command not available.`);
vscode.window.showErrorMessage(`ClusterExplorer not available.`);
return;
}

Expand All @@ -118,6 +123,19 @@ export async function activate(context: vscode.ExtensionContext) {
}

explorer.api.registerNodeContributor(new FileSystemNodeContributor(kubectl.api));

const disposable = vscode.commands.registerCommand('k8s.pod.container.file.view', viewFile);
context.subscriptions.push(disposable);
}

async function viewFile(target?: any) {
if (target && target.nodeType === 'extension') {
if (target.impl instanceof FileNode) {
(target.impl as FileNode).viewFile();
return;
}
}
vscode.window.showErrorMessage(`View file command only work on node for a file in Pod(Container) filesystem.`);
}

export function deactivate() {
Expand Down

0 comments on commit b94eb8c

Please sign in to comment.