From eaf7e10958a03f8ddec4d3a11b48ad3e24d0eb24 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann Date: Fri, 22 Oct 2021 18:53:37 -0400 Subject: [PATCH] code reorg Signed-off-by: Jim Ehrismann --- src/renderer/port-forward/index.ts | 1 + .../port-forward/port-forward-utils.ts | 54 +++++++++++++++++++ .../port-forward/port-forward.store.ts | 29 +--------- 3 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 src/renderer/port-forward/port-forward-utils.ts diff --git a/src/renderer/port-forward/index.ts b/src/renderer/port-forward/index.ts index 5be9fb5aed..83a548f933 100644 --- a/src/renderer/port-forward/index.ts +++ b/src/renderer/port-forward/index.ts @@ -22,3 +22,4 @@ export * from "./port-forward.store"; export * from "./port-forward-item"; export * from "./port-forward-dialog"; +export * from "./port-forward-utils"; diff --git a/src/renderer/port-forward/port-forward-utils.ts b/src/renderer/port-forward/port-forward-utils.ts new file mode 100644 index 0000000000..7ca1c6bfa8 --- /dev/null +++ b/src/renderer/port-forward/port-forward-utils.ts @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +import { openExternal } from "../utils"; +import { Notifications } from "../components/notifications"; +import type { ForwardedPort } from "./port-forward-item"; +import logger from "../../common/logger"; + +export function portForwardAddress(portForward: ForwardedPort) { + return `${portForward.protocol ?? "http"}://localhost:${portForward.forwardPort}`; + } + + export function openPortForward(portForward: ForwardedPort) { + const browseTo = portForwardAddress(portForward); + + openExternal(browseTo) + .catch(error => { + logger.error(`failed to open in browser: ${error}`, { + clusterId: portForward.clusterId, + port: portForward.port, + kind: portForward.kind, + namespace: portForward.namespace, + name: portForward.name, + }); + Notifications.error(`Failed to open ${browseTo} in browser`); + } + ); + + } + + export function predictProtocol(name: string) { + return name === "https" ? "https" : "http"; + } + + \ No newline at end of file diff --git a/src/renderer/port-forward/port-forward.store.ts b/src/renderer/port-forward/port-forward.store.ts index 9c90dcf6a0..4fc9c81540 100644 --- a/src/renderer/port-forward/port-forward.store.ts +++ b/src/renderer/port-forward/port-forward.store.ts @@ -22,11 +22,10 @@ import { makeObservable, observable, reaction } from "mobx"; import { ItemStore } from "../../common/item.store"; -import { autoBind, createStorage, disposer, getHostedClusterId, openExternal } from "../utils"; +import { autoBind, createStorage, disposer, getHostedClusterId } from "../utils"; import { ForwardedPort, PortForwardItem } from "./port-forward-item"; import { apiBase } from "../api"; import { waitUntilFree } from "tcp-port-used"; -import { Notifications } from "../components/notifications"; import logger from "../../common/logger"; export class PortForwardStore extends ItemStore { @@ -178,30 +177,4 @@ export async function getPortForwards(): Promise { } } -export function portForwardAddress(portForward: ForwardedPort) { - return `${portForward.protocol ?? "http"}://localhost:${portForward.forwardPort}`; -} - -export function openPortForward(portForward: ForwardedPort) { - const browseTo = portForwardAddress(portForward); - - openExternal(browseTo) - .catch(error => { - logger.error(`failed to open in browser: ${error}`, { - clusterId: portForward.clusterId, - port: portForward.port, - kind: portForward.kind, - namespace: portForward.namespace, - name: portForward.name, - }); - Notifications.error(`Failed to open ${browseTo} in browser`); - } - ); - -} - -export function predictProtocol(name: string) { - return name === "https" ? "https" : "http"; -} - export const portForwardStore = new PortForwardStore();