Skip to content
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

Allow to install packed extensions from URL or local file #1456

Merged
merged 23 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6624287
Option to install an extension from filesystem/url #1227 -- part 1 (UI)
ixrock Nov 19, 2020
7d28a43
DropFileInput: common component to handle droped files (replaced also…
ixrock Nov 19, 2020
724ee86
fix: install via url-string on input.submit
ixrock Nov 19, 2020
cc28424
ui tweaks & minor fixes
ixrock Nov 19, 2020
3df6c07
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 19, 2020
97bcb49
more ui/ux tweaks & fixes
ixrock Nov 19, 2020
51f686c
layout fixes
ixrock Nov 19, 2020
f505dab
component renaming: `copy-to-click` => `copy-to-clipboard` => `clipbo…
ixrock Nov 19, 2020
78dcd5d
reworks -- part 1
ixrock Nov 20, 2020
9284611
fix downloading file, added common/utils/downloadFile
ixrock Nov 23, 2020
5093262
confirm before install, unpack tar first steps
ixrock Nov 23, 2020
88950b0
installation flow, extracting .tgz
ixrock Nov 23, 2020
de93812
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 23, 2020
150c3dc
clean up, fix lint issues
ixrock Nov 23, 2020
8b6616a
update .azure-pipelines.yml
ixrock Nov 23, 2020
5fc0c63
fixes & refactoring
ixrock Nov 24, 2020
e460b3f
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 24, 2020
12c3422
fix lint harder :/
ixrock Nov 24, 2020
d050d6c
fix validation
ixrock Nov 24, 2020
1c17420
fix validation harder
ixrock Nov 24, 2020
55c284f
responding to comments, fixed package validation
ixrock Nov 24, 2020
639bc1a
common/utils/tar.ts: reject with Error-type
ixrock Nov 24, 2020
4c3a8e2
fix: unit-tests
ixrock Nov 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"bundledHelmVersion": "3.3.4"
},
"engines": {
"node": ">=12.0 <13.0"
"node": ">=12 <13"
},
"lingui": {
"locales": [
Expand Down Expand Up @@ -214,7 +214,7 @@
"@types/node": "^12.12.45",
"@types/proper-lockfile": "^4.1.1",
"@types/react-beautiful-dnd": "^13.0.0",
"@types/tar": "^4.0.3",
"@types/tar": "^4.0.4",
"array-move": "^3.0.0",
"await-lock": "^2.1.0",
"chalk": "^4.1.0",
Expand Down Expand Up @@ -251,7 +251,7 @@
"serializr": "^2.0.3",
"shell-env": "^3.0.0",
"spdy": "^4.0.2",
"tar": "^6.0.2",
"tar": "^6.0.5",
"tcp-port-used": "^1.0.1",
"tempy": "^0.5.0",
"uuid": "^8.1.0",
Expand Down Expand Up @@ -314,7 +314,6 @@
"@types/sharp": "^0.26.0",
"@types/shelljs": "^0.8.8",
"@types/spdy": "^3.4.4",
"@types/tar": "^4.0.3",
"@types/tcp-port-used": "^1.0.0",
"@types/tempy": "^0.3.0",
"@types/terser-webpack-plugin": "^3.0.0",
Expand Down
35 changes: 35 additions & 0 deletions src/common/utils/downloadFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import request from "request";

export interface DownloadFileOptions {
url: string;
gzip?: boolean;
}

export interface DownloadFileTicket {
url: string;
promise: Promise<Buffer>;
cancel(): void;
Nokel81 marked this conversation as resolved.
Show resolved Hide resolved
}

export function downloadFile({ url, gzip = true }: DownloadFileOptions): DownloadFileTicket {
const fileChunks: Buffer[] = [];
const req = request(url, { gzip });
const promise: Promise<Buffer> = new Promise((resolve, reject) => {
req.on("data", (chunk: Buffer) => {
fileChunks.push(chunk);
});
req.once("error", err => {
reject({ url, err });
});
req.once("complete", () => {
resolve(Buffer.concat(fileChunks));
});
});
return {
url,
promise,
cancel() {
req.abort();
}
};
}
5 changes: 5 additions & 0 deletions src/common/utils/escapeRegExp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Helper to sanitize / escape special chars for passing to RegExp-constructor

export function escapeRegExp(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
3 changes: 3 additions & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export * from "./saveToAppFiles";
export * from "./singleton";
export * from "./openExternal";
export * from "./rectify-array";
export * from "./downloadFile";
export * from "./escapeRegExp";
ixrock marked this conversation as resolved.
Show resolved Hide resolved
export * from "./tar";
55 changes: 55 additions & 0 deletions src/common/utils/tar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Helper for working with tarball files (.tar, .tgz)
// Docs: https://github.com/npm/node-tar
import tar, { ExtractOptions, FileStat } from "tar";
import path from "path";

export interface ReadFileFromTarOpts {
tarPath: string;
filePath: string;
parseJson?: boolean;
}

export function readFileFromTar<R = Buffer>({ tarPath, filePath, parseJson }: ReadFileFromTarOpts): Promise<R> {
return new Promise(async (resolve, reject) => {
const fileChunks: Buffer[] = [];

await tar.list({
file: tarPath,
filter: path => path === filePath,
onentry(entry: FileStat) {
entry.on("data", chunk => {
fileChunks.push(chunk);
});
entry.once("error", err => {
reject(new Error(`reading file has failed ${entry.path}: ${err}`));
});
entry.once("end", () => {
const data = Buffer.concat(fileChunks);
const result = parseJson ? JSON.parse(data.toString("utf8")) : data;
resolve(result);
Comment on lines +28 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this is wrong. If R is unchanged but parseJson: true then the types are wrong. I am surprised that typescript doesn't catch this.

});
},
});

if (!fileChunks.length) {
reject(new Error("Not found"));
}
});
}

export async function listTarEntries(filePath: string): Promise<string[]> {
const entries: string[] = [];
await tar.list({
file: filePath,
onentry: (entry: FileStat) => entries.push(entry.path as any as string),
});
return entries;
}

export function extractTar(filePath: string, opts: ExtractOptions & { sync?: boolean } = {}) {
return tar.extract({
file: filePath,
cwd: path.dirname(filePath),
...opts,
});
}
2 changes: 2 additions & 0 deletions src/common/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ export const apiKubePrefix = "/api-kube"; // k8s cluster apis
// Links
export const issuesTrackerUrl = "https://github.com/lensapp/lens/issues";
export const slackUrl = "https://join.slack.com/t/k8slens/shared_invite/enQtOTc5NjAyNjYyOTk4LWU1NDQ0ZGFkOWJkNTRhYTc2YjVmZDdkM2FkNGM5MjhiYTRhMDU2NDQ1MzIyMDA4ZGZlNmExOTc0N2JmY2M3ZGI";
export const docsUrl = "https://docs.k8slens.dev/";
export const supportUrl = "https://docs.k8slens.dev/latest/support/";
2 changes: 1 addition & 1 deletion src/extensions/extension-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface InstalledExtension {
}

const logModule = "[EXTENSION-DISCOVERY]";
const manifestFilename = "package.json";
export const manifestFilename = "package.json";

/**
* Returns true if the lstat is for a directory-like file (e.g. isDirectory or symbolic link)
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/lens-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface LensExtensionManifest {
description?: string;
main?: string; // path to %ext/dist/main.js
renderer?: string; // path to %ext/dist/renderer.js
lens?: object; // fixme: add more required fields for validation
}

export class LensExtension {
Expand Down Expand Up @@ -96,3 +97,7 @@ export class LensExtension {
// mock
}
}

export function sanitizeExtensionName(name: string) {
return name.replace("@", "").replace("/", "--");
}
4 changes: 2 additions & 2 deletions src/extensions/registries/__tests__/page-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe("getPageUrl", () => {
expect(getExtensionPageUrl({ extensionId: ext.name, pageId: "/test" })).toBe("/extension/foo-bar/test");
});

it("removes @", () => {
expect(getExtensionPageUrl({ extensionId: "@foo/bar" })).toBe("/extension/foo-bar");
it("removes @ and replace `/` to `--`", () => {
expect(getExtensionPageUrl({ extensionId: "@foo/bar" })).toBe("/extension/foo--bar");
});

it("adds / prefix", () => {
Expand Down
6 changes: 1 addition & 5 deletions src/extensions/registries/page-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path";
import { action } from "mobx";
import { compile } from "path-to-regexp";
import { BaseRegistry } from "./base-registry";
import { LensExtension } from "../lens-extension";
import { LensExtension, sanitizeExtensionName } from "../lens-extension";
import logger from "../../main/logger";
import { recitfy } from "../../common/utils";

Expand Down Expand Up @@ -45,10 +45,6 @@ export interface PageComponents {
Page: React.ComponentType<any>;
}

export function sanitizeExtensionName(name: string) {
return name.replace("@", "").replace("/", "-");
}

export function getExtensionPageUrl<P extends object>({ extensionId, pageId = "", params }: PageMenuTarget<P>): string {
const extensionBaseUrl = compile(`/extension/:name`)({
name: sanitizeExtensionName(extensionId), // compile only with extension-id first and define base path
Expand Down
7 changes: 4 additions & 3 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app, BrowserWindow, dialog, ipcMain, IpcMainEvent, Menu, MenuItem, MenuItemConstructorOptions, webContents, shell } from "electron";
import { autorun } from "mobx";
import { WindowManager } from "./window-manager";
import { appName, isMac, isWindows, isTestEnv } from "../common/vars";
import { appName, isMac, isWindows, isTestEnv, docsUrl, supportUrl } from "../common/vars";
import { addClusterURL } from "../renderer/components/+add-cluster/add-cluster.route";
import { preferencesURL } from "../renderer/components/+preferences/preferences.route";
import { whatsNewURL } from "../renderer/components/+whats-new/whats-new.route";
Expand All @@ -24,6 +24,7 @@ export function showAbout(browserWindow: BrowserWindow) {
`${appName}: ${app.getVersion()}`,
`Electron: ${process.versions.electron}`,
`Chrome: ${process.versions.chrome}`,
`Node: ${process.versions.node}`,
`Copyright 2020 Mirantis, Inc.`,
];
dialog.showMessageBoxSync(browserWindow, {
Expand Down Expand Up @@ -215,13 +216,13 @@ export function buildMenu(windowManager: WindowManager) {
{
label: "Documentation",
click: async () => {
shell.openExternal('https://docs.k8slens.dev/');
shell.openExternal(docsUrl);
},
},
{
label: "Support",
click: async () => {
shell.openExternal('https://docs.k8slens.dev/latest/support/');
shell.openExternal(supportUrl);
},
},
...ignoreOnMac([
Expand Down
8 changes: 0 additions & 8 deletions src/renderer/components/+add-cluster/add-cluster.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
.AddCluster {
.droppable {
box-shadow: 0 0 0 5px inset $primary;

> * {
pointer-events: none;
}
}

.hint {
margin-top: -$padding;
color: $textColorSecondary;
Expand Down
Loading