Skip to content

Commit

Permalink
refactor markdown table generation (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
usernamehw committed Sep 13, 2021
1 parent 6e27284 commit 4fa5767
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 82 deletions.
19 changes: 19 additions & 0 deletions src/utils/markdownStringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MarkdownString } from 'vscode';

/**
* Create specific table header (2 columns, left aligned)
* for the tree view item hover.
*/
export function generateMarkdownTableHeader(markdown: MarkdownString) {
markdown.appendMarkdown('Property | Value\n');
markdown.appendMarkdown(':--- | :---\n');
}
/**
* Create markdown table row (only if value is not equal `undefined`)
*/
export function generateMarkdownTableRow(propertyName: string, propertyValue: string | boolean | undefined, markdown: MarkdownString) {
if (propertyValue === undefined) {
return;
}
markdown.appendMarkdown(`${propertyName} | ${propertyValue}\n`);
}
18 changes: 6 additions & 12 deletions src/views/clusterTreeViewDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ResourceTypes } from '../kubernetes/kubernetesTypes';
import { TreeViewDataProvider } from './treeViewDataProvider';
import { TreeViewItem } from './treeViewItem';
import { TreeViewItemContext } from './treeViewItemContext';
import { generateMarkdownTableHeader, generateMarkdownTableRow } from '../utils/markdownStringUtils';

let _extensionContext: ExtensionContext;

Expand Down Expand Up @@ -93,18 +94,11 @@ export class ClusterTreeViewItem extends TreeViewItem {
*/
getMarkdown(cluster: Cluster, showJsonConfig: boolean = false): MarkdownString {
const markdown: MarkdownString = new MarkdownString();
markdown.appendMarkdown(`Property | Value\n`);
markdown.appendMarkdown(`--- | ---\n`);
markdown.appendMarkdown(`Name | ${cluster.name}\n`);
markdown.appendMarkdown(`Server | ${cluster.cluster.server}\n`);

if (cluster.cluster['certificate-authority']) {
markdown.appendMarkdown(`Certificate authority | ${cluster.cluster['certificate-authority']}`);
}

if (cluster.cluster['certificate-authority-data']) {
markdown.appendMarkdown(`Certificate authority data | ${cluster.cluster['certificate-authority-data']}`);
}
generateMarkdownTableHeader(markdown);
generateMarkdownTableRow('Name', cluster.name, markdown);
generateMarkdownTableRow('Server', cluster.cluster.server, markdown);
generateMarkdownTableRow('Certificate authority', cluster.cluster['certificate-authority'], markdown);
generateMarkdownTableRow('Certificate authority data', cluster.cluster['certificate-authority-data'], markdown);

if (showJsonConfig) {
markdown.appendCodeblock(JSON.stringify(cluster, null, ' '), 'json');
Expand Down
51 changes: 17 additions & 34 deletions src/views/deploymentTreeViewItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MarkdownString } from 'vscode';
import { HelmRelease } from '../kubernetes/helmRelease';
import { Kustomize } from '../kubernetes/kustomize';
import { generateMarkdownTableHeader, generateMarkdownTableRow } from '../utils/markdownStringUtils';
import { TreeViewItem } from './treeViewItem';

/**
Expand All @@ -20,44 +21,26 @@ export class DeploymentTreeViewItem extends TreeViewItem {
showJsonConfig: boolean = false): MarkdownString {

const markdown: MarkdownString = new MarkdownString();
markdown.appendMarkdown(`Property | Value\n`);
markdown.appendMarkdown(`--- | ---\n`);
markdown.appendMarkdown(`Api version | ${deployment.apiVersion}\n`);
markdown.appendMarkdown(`Kind | ${deployment.kind}\n`);

if (deployment.metadata.name) {
markdown.appendMarkdown(`Name | ${deployment.metadata.name}\n`);
}

if (deployment.metadata.namespace) {
markdown.appendMarkdown(`Namespace | ${deployment.metadata.namespace}\n`);
}

markdown.appendMarkdown(`Interval | ${deployment.spec.interval}\n`);
if (deployment.spec.timeout) {
markdown.appendMarkdown(`Timeout | ${deployment.spec.timeout}\n`);
}
generateMarkdownTableHeader(markdown);
generateMarkdownTableRow('Api version', deployment.apiVersion, markdown);
generateMarkdownTableRow('Kind', deployment.kind, markdown);
generateMarkdownTableRow('Name', deployment.metadata.name, markdown);
generateMarkdownTableRow('Namespace', deployment.metadata.namespace, markdown);
generateMarkdownTableRow('Interval', deployment.spec.interval, markdown);
generateMarkdownTableRow('Timeout', deployment.spec.timeout, markdown);

if (deployment.kind === 'Kustomization') {
markdown.appendMarkdown(`Prune | ${deployment.spec.prune}\n`);
markdown.appendMarkdown(`Source ref kind | ${deployment.spec.sourceRef.kind}\n`);
markdown.appendMarkdown(`Source ref name | ${deployment.spec.sourceRef.name}\n`);

if (deployment.spec.force !== undefined) {
markdown.appendMarkdown(`Force | ${deployment.spec.force}\n`);
}

if (deployment.spec.path) {
markdown.appendMarkdown(`Path | ${deployment.spec.path}\n`);
}
generateMarkdownTableRow('Prune', deployment.spec.prune, markdown);
generateMarkdownTableRow('Source ref kind', deployment.spec.sourceRef.kind, markdown);
generateMarkdownTableRow('Source ref name', deployment.spec.sourceRef.name, markdown);
generateMarkdownTableRow('Force', deployment.spec.force, markdown);
generateMarkdownTableRow('Path', deployment.spec.path, markdown);
}
else if (deployment.kind === 'HelmRelease') {
markdown.appendMarkdown(`Chart name | ${deployment.spec.chart.spec.chart}\n`);
markdown.appendMarkdown(`Chart source ref kind | ${deployment.spec.chart.spec.sourceRef.kind}\n`);
markdown.appendMarkdown(`Chart source ref name | ${deployment.spec.chart.spec.sourceRef.name}\n`);
if (deployment.spec.chart.spec.version) {
markdown.appendMarkdown(`Chart version | ${deployment.spec.chart.spec.version}\n`);
}
generateMarkdownTableRow('Chart name', deployment.spec.chart.spec.chart, markdown);
generateMarkdownTableRow('Chart source ref kind', deployment.spec.chart.spec.sourceRef.kind, markdown);
generateMarkdownTableRow('Chart source ref name', deployment.spec.chart.spec.sourceRef.name, markdown);
generateMarkdownTableRow('Chart version', deployment.spec.chart.spec.version, markdown);
}

if (showJsonConfig) {
Expand Down
52 changes: 16 additions & 36 deletions src/views/sourceTreeViewItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MarkdownString } from 'vscode';
import { Bucket } from '../kubernetes/bucket';
import { GitRepository } from '../kubernetes/gitRepository';
import { HelmRepository } from '../kubernetes/helmRepository';
import { generateMarkdownTableHeader, generateMarkdownTableRow } from '../utils/markdownStringUtils';
import { TreeViewItem } from './treeViewItem';

/**
Expand All @@ -21,48 +22,27 @@ import { TreeViewItem } from './treeViewItem';
showJsonConfig: boolean = false): MarkdownString {

const markdown: MarkdownString = new MarkdownString();
markdown.appendMarkdown(`Property | Value\n`);
markdown.appendMarkdown(`--- | ---\n`);
markdown.appendMarkdown(`Api version | ${source.apiVersion}\n`);
markdown.appendMarkdown(`Kind | ${source.kind}\n`);

if (source.metadata.name) {
markdown.appendMarkdown(`Name | ${source.metadata.name}\n`);
}

if (source.metadata.namespace) {
markdown.appendMarkdown(`Namespace | ${source.metadata.namespace}\n`);
}

markdown.appendMarkdown(`Interval | ${source.spec.interval}\n`);
if (source.spec.timeout) {
markdown.appendMarkdown(`Timeout | ${source.spec.timeout}\n`);
}
generateMarkdownTableHeader(markdown);
generateMarkdownTableRow('Api version', source.apiVersion, markdown);
generateMarkdownTableRow('Kind', source.kind, markdown);
generateMarkdownTableRow('Name', source.metadata.name, markdown);
generateMarkdownTableRow('Namespace', source.metadata.namespace, markdown);
generateMarkdownTableRow('Interval', source.spec.interval, markdown);
generateMarkdownTableRow('Timeout', source.spec.timeout, markdown);

if (source.kind === 'GitRepository') {
markdown.appendMarkdown(`URL | ${source.spec.url}\n`);
if (source.spec.ref) {
if (source.spec.ref.branch) {
markdown.appendMarkdown(`Branch | ${source.spec.ref.branch}\n`);
}

if (source.spec.ref.commit) {
markdown.appendMarkdown(`Commit | ${source.spec.ref.commit}\n`);
}
}
generateMarkdownTableRow('URL', source.spec.url, markdown);
generateMarkdownTableRow('Branch', source.spec?.ref?.branch, markdown);
generateMarkdownTableRow('Commit', source.spec?.ref?.commit, markdown);
}
else if (source.kind === 'HelmRepository') {
markdown.appendMarkdown(`URL | ${source.spec.url}\n`);
generateMarkdownTableRow('URL', source.spec.url, markdown);
}
else if (source.kind === 'Bucket') {
markdown.appendMarkdown(`Name | ${source.spec.bucketName}\n`);
markdown.appendMarkdown(`Endpoint | ${source.spec.endpoint}\n`);
if (source.spec.provider) {
markdown.appendMarkdown(`Provider | ${source.spec.provider}\n`);
}
if (source.spec.insecure !== undefined) {
markdown.appendMarkdown(`Insecure | ${source.spec.insecure}\n`);
}
generateMarkdownTableRow('Name', source.spec.bucketName, markdown);
generateMarkdownTableRow('Endpoint', source.spec.endpoint, markdown);
generateMarkdownTableRow('Provider', source.spec.provider, markdown);
generateMarkdownTableRow('Insecure', source.spec.insecure, markdown);
}

if (showJsonConfig) {
Expand Down

0 comments on commit 4fa5767

Please sign in to comment.