1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

initial work

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-04 09:38:57 -05:00
parent bcaef79386
commit 0313a23c6f
2 changed files with 49 additions and 52 deletions

View File

@ -6,6 +6,7 @@ import { Singleton } from "../../common/utils/singleton";
import { customRequestPromise } from "../../common/request"; import { customRequestPromise } from "../../common/request";
import orderBy from "lodash/orderBy"; import orderBy from "lodash/orderBy";
import logger from "../logger"; import logger from "../logger";
import * as util from "util";
export type HelmEnv = Record<string, string> & { export type HelmEnv = Record<string, string> & {
HELM_REPOSITORY_CACHE?: string; HELM_REPOSITORY_CACHE?: string;
@ -58,22 +59,14 @@ export class HelmRepoManager extends Singleton {
} }
protected async parseHelmEnv() { protected async parseHelmEnv() {
const helm = await helmCli.binaryPath(); const stdout = await this.execHelm(["env"]);
const { stdout } = await promiseExec(`"${helm}" env`).catch((error) => {
throw(error.stderr);
});
const lines = stdout.split(/\r?\n/); // split by new line feed
const env: HelmEnv = {};
lines.forEach((line: string) => { return Object.fromEntries(
const [key, value] = line.split("="); stdout.split(/\r?\n/) // split by new line feed
.filter(Boolean) // ignore empty lines
if (key && value) { .map(line => line.split("="))
env[key] = value.replace(/"/g, ""); // strip quotas .map(([key, value]) => [key, value.replaceAll("\"", "")])
} );
});
return env;
} }
public async repositories(): Promise<HelmRepo[]> { public async repositories(): Promise<HelmRepo[]> {
@ -112,52 +105,56 @@ export class HelmRepoManager extends Singleton {
return repositories.find(repo => repo.name == name); return repositories.find(repo => repo.name == name);
} }
/**
* Executes helm with the provided args returning the STDOUT or throwing the STDERR (if error)
* @param args the list of args to call helm with
*/
protected async execHelm(args: string[]): Promise<string> {
const helmPath = util.inspect(helmCli.binaryPath());
const argsStr = args.filter(Boolean).join(" ");
const command = `${helmPath} ${argsStr}`;
try {
return (await promiseExec(command)).stdout;
} catch ({ stderr }) {
throw stderr;
}
}
public async update() { public async update() {
const helm = await helmCli.binaryPath(); return this.execHelm([
const { stdout } = await promiseExec(`"${helm}" repo update`).catch((error) => { "repo",
return { stdout: error.stdout }; "update",
}); ]);
return stdout;
} }
public async addRepo({ name, url }: HelmRepo) { public async addRepo(repoInfo: HelmRepo) {
const { name, url, insecureSkipTlsVerify, username, password, caFile, keyFile, certFile } = repoInfo;
logger.info(`[HELM]: adding repo "${name}" from ${url}`); logger.info(`[HELM]: adding repo "${name}" from ${url}`);
const helm = await helmCli.binaryPath();
const { stdout } = await promiseExec(`"${helm}" repo add ${name} ${url}`).catch((error) => {
throw(error.stderr);
});
return stdout; return this.execHelm([
} "repo",
"add",
public async addСustomRepo(repoAttributes : HelmRepo) { util.inspect(name),
logger.info(`[HELM]: adding repo "${repoAttributes.name}" from ${repoAttributes.url}`); util.inspect(url),
const helm = await helmCli.binaryPath(); insecureSkipTlsVerify && "--insecure-skip-tls-verify",
username && `--username=${util.inspect(username)}`,
const insecureSkipTlsVerify = repoAttributes.insecureSkipTlsVerify ? " --insecure-skip-tls-verify" : ""; password && `--password=${util.inspect(password)}`,
const username = repoAttributes.username ? ` --username "${repoAttributes.username}"` : ""; caFile && `--ca-file=${util.inspect(caFile)}`,
const password = repoAttributes.password ? ` --password "${repoAttributes.password}"` : ""; keyFile && `--key-file=${util.inspect(keyFile)}`,
const caFile = repoAttributes.caFile ? ` --ca-file "${repoAttributes.caFile}"` : ""; certFile && `--cert-file=${util.inspect(keyFile)}`,
const keyFile = repoAttributes.keyFile ? ` --key-file "${repoAttributes.keyFile}"` : ""; ]);
const certFile = repoAttributes.certFile ? ` --cert-file "${repoAttributes.certFile}"` : "";
const addRepoCommand = `"${helm}" repo add ${repoAttributes.name} ${repoAttributes.url}${insecureSkipTlsVerify}${username}${password}${caFile}${keyFile}${certFile}`;
const { stdout } = await promiseExec(addRepoCommand).catch((error) => {
throw(error.stderr);
});
return stdout;
} }
public async removeRepo({ name, url }: HelmRepo): Promise<string> { public async removeRepo({ name, url }: HelmRepo): Promise<string> {
logger.info(`[HELM]: removing repo "${name}" from ${url}`); logger.info(`[HELM]: removing repo "${name}" from ${url}`);
const helm = await helmCli.binaryPath();
const { stdout } = await promiseExec(`"${helm}" repo remove ${name}`).catch((error) => {
throw(error.stderr);
});
return stdout; return this.execHelm([
"repo",
"remove",
util.inspect(name),
]);
} }
} }

View File

@ -79,7 +79,7 @@ export class AddHelmRepoDialog extends React.Component<Props> {
async addCustomRepo() { async addCustomRepo() {
try { try {
await repoManager.addСustomRepo(this.helmRepo); await repoManager.addRepo(this.helmRepo);
Notifications.ok(<>Helm repository <b>{this.helmRepo.name}</b> has added</>); Notifications.ok(<>Helm repository <b>{this.helmRepo.name}</b> has added</>);
this.props.onAddRepo(); this.props.onAddRepo();
this.close(); this.close();