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

Move downloadAllLogs logic to injectable

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-08-12 10:33:12 +03:00
parent 5090c323fb
commit 298c8a1145
2 changed files with 37 additions and 18 deletions

View File

@ -13,7 +13,9 @@ import type { LogTabViewModel } from "./logs-view-model";
import { withInjectables } from "@ogre-tools/injectable-react";
import openSaveFileDialogInjectable from "../../../utils/save-file.injectable";
import { DownloadLogsDropdown } from "./download-logs-dropdown";
import callForLogsInjectable, { CallForLogs } from "./call-for-logs.injectable";
import type { ResourceDescriptor } from "../../../../common/k8s-api/kube-api";
import downloadAllLogsInjectable from "./download-all-logs.injectable";
import type { PodLogsQuery } from "../../../../common/k8s-api/endpoints";
export interface LogControlsProps {
model: LogTabViewModel;
@ -21,10 +23,10 @@ export interface LogControlsProps {
interface Dependencies {
openSaveFileDialog: (filename: string, contents: BlobPart | BlobPart[], type: string) => void;
callForLogs: CallForLogs;
downloadAllLogs: (params: ResourceDescriptor, query: PodLogsQuery) => Promise<void>;
}
const NonInjectedLogControls = observer(({ openSaveFileDialog, model, callForLogs }: Dependencies & LogControlsProps) => {
const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAllLogs }: Dependencies & LogControlsProps) => {
const tabData = model.logTabData.get();
const pod = model.pod.get();
@ -45,19 +47,6 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, callForLog
model.reloadLogs();
};
const downloadAllLogs = async () => {
const pod = model.pod.get();
if (pod) {
const logs = await callForLogs(
{ name: pod.getName(), namespace: pod.getNs() },
{ timestamps: showTimestamps, previous }
)
openSaveFileDialog(`${pod.getName()}.log`, logs, "text/plain");
}
};
const downloadLogs = () => {
return new Promise((resolve) => {
const fileName = pod.getName();
@ -97,7 +86,10 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, callForLog
<DownloadLogsDropdown
downloadVisibleLogs={downloadLogs}
downloadAllLogs={downloadAllLogs}
downloadAllLogs={() => downloadAllLogs(
{ name: pod.getName(), namespace: pod.getNs() },
{ timestamps: showTimestamps, previous }
)}
/>
</div>
</div>
@ -107,7 +99,7 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, callForLog
export const LogControls = withInjectables<Dependencies, LogControlsProps>(NonInjectedLogControls, {
getProps: (di, props) => ({
openSaveFileDialog: di.inject(openSaveFileDialogInjectable),
callForLogs: di.inject(callForLogsInjectable),
downloadAllLogs: di.inject(downloadAllLogsInjectable),
...props,
}),
});

View File

@ -0,0 +1,27 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { PodContainer, Pod, PodLogsQuery } from "../../../../common/k8s-api/endpoints";
import type { ResourceDescriptor } from "../../../../common/k8s-api/kube-api";
import openSaveFileDialogInjectable from "../../../utils/save-file.injectable";
import callForLogsInjectable from "./call-for-logs.injectable";
export interface PodLogsTabData {
selectedPod: Pod;
selectedContainer: PodContainer;
}
const downloadAllLogsInjectable = getInjectable({
id: "download-all-logs",
instantiate: (di) => {
const callForLogs = di.inject(callForLogsInjectable);
const openSaveFileDialog = di.inject(openSaveFileDialogInjectable)
return async (params: ResourceDescriptor, query: PodLogsQuery) => {
const logs = await callForLogs(params, query)
openSaveFileDialog(`${params.name}.log`, logs, "text/plain");
}
},
});
export default downloadAllLogsInjectable;