/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./add-helm-repo-dialog.scss"; import React from "react"; import type { FileFilter } from "electron"; import { observable, makeObservable } from "mobx"; import { observer } from "mobx-react"; import { Dialog, DialogProps } from "../dialog"; import { Wizard, WizardStep } from "../wizard"; import { Input } from "../input"; import { Checkbox } from "../checkbox"; import { Button } from "../button"; import { systemName, isUrl, isPath } from "../input/input_validators"; import { SubTitle } from "../layout/sub-title"; import { Icon } from "../icon"; import { Notifications } from "../notifications"; import { HelmRepo, HelmRepoManager } from "../../../main/helm/helm-repo-manager"; import { requestOpenFilePickingDialog } from "../../ipc"; interface Props extends Partial { onAddRepo: Function } enum FileType { CaFile = "caFile", KeyFile = "keyFile", CertFile = "certFile", } const dialogState = observable.object({ isOpen: false, }); @observer export class AddHelmRepoDialog extends React.Component { private emptyRepo = { name: "", url: "", username: "", password: "", insecureSkipTlsVerify: false, caFile:"", keyFile: "", certFile: "" }; private static keyExtensions = ["key", "keystore", "jks", "p12", "pfx", "pem"]; private static certExtensions = ["crt", "cer", "ca-bundle", "p7b", "p7c", "p7s", "p12", "pfx", "pem"]; constructor(props: Props) { super(props); makeObservable(this); } static open() { dialogState.isOpen = true; } static close() { dialogState.isOpen = false; } @observable helmRepo : HelmRepo = this.emptyRepo; @observable showOptions = false; close = () => { AddHelmRepoDialog.close(); this.helmRepo = this.emptyRepo; this.showOptions = false; }; setFilepath(type: FileType, value: string) { this.helmRepo[type] = value; } getFilePath(type: FileType) : string { return this.helmRepo[type]; } async selectFileDialog(type: FileType, fileFilter: FileFilter) { const { canceled, filePaths } = await requestOpenFilePickingDialog({ defaultPath: this.getFilePath(type), properties: ["openFile", "showHiddenFiles"], message: `Select file`, buttonLabel: `Use file`, filters: [ fileFilter, { name: "Any", extensions: ["*"] }, ], }); if (!canceled && filePaths.length) { this.setFilepath(type, filePaths[0]); } } async addCustomRepo() { try { await HelmRepoManager.addCustomRepo(this.helmRepo); Notifications.ok(<>Helm repository {this.helmRepo.name} has added); this.props.onAddRepo(); this.close(); } catch (err) { Notifications.error(<>Adding helm branch {this.helmRepo.name} has failed: {String(err)}); } } renderFileInput(placeholder:string, fileType:FileType, fileExtensions:string[]){ return(
this.setFilepath(fileType, v)} /> this.selectFileDialog(fileType, { name: placeholder, extensions: fileExtensions })} tooltip="Browse" />
); } renderOptions() { return ( <> this.helmRepo.insecureSkipTlsVerify = v} /> {this.renderFileInput(`Key file`, FileType.KeyFile, AddHelmRepoDialog.keyExtensions)} {this.renderFileInput(`Ca file`, FileType.CaFile, AddHelmRepoDialog.certExtensions)} {this.renderFileInput(`Certificate file`, FileType.CertFile, AddHelmRepoDialog.certExtensions)} this.helmRepo.username = v} /> this.helmRepo.password = v} /> ); } render() { const { ...dialogProps } = this.props; const header =
Add custom Helm Repo
; return ( this.addCustomRepo()}>
this.helmRepo.name = v} /> this.helmRepo.url = v} /> {this.showOptions && this.renderOptions()}
); } }