diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/DescribeDocument.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/DescribeDocument.kt new file mode 100644 index 000000000..c15710303 --- /dev/null +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/DescribeDocument.kt @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.kubernetes.editor.describe + +abstract class DescribeDocument { + + protected val paragraphs = mutableListOf() + + fun add(paragraph: Paragraph): DescribeDocument { + paragraphs.add(paragraph) + return this + } + + fun add(title: String, value: String): DescribeDocument { + paragraphs.add(Paragraph(title, value)); + return this + } + + abstract fun toText(): String + + class Paragraph(val title: String, val value: String? = null) { + + val children = mutableListOf() + + fun add(paragraph: Paragraph): Paragraph { + children.add(paragraph) + return this + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocument.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocument.kt new file mode 100644 index 000000000..ec2b0363a --- /dev/null +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocument.kt @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.kubernetes.editor.describe + +import org.yaml.snakeyaml.DumperOptions +import org.yaml.snakeyaml.Yaml + + +class YAMLDescribeDocument: DescribeDocument() { + + private val yaml = let { + val options = DumperOptions().apply { + defaultFlowStyle = DumperOptions.FlowStyle.BLOCK + } + Yaml(options) + } + + override fun toText(): String { + return yaml.dump(toMap(paragraphs)) + } + + /* + * Turning objects into map because I couldn't convince snakeyaml to format like I wanted by using native configurations. + */ + private fun toMap(paragraphs: List): Map { + return paragraphs.associate { paragraph -> + if (paragraph.children.isEmpty()) { + paragraph.title to paragraph.value + } else { + paragraph.title to toMap(paragraph.children) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContexts.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContexts.kt index b793acf84..d9add854c 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContexts.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContexts.kt @@ -109,8 +109,12 @@ open class AllContexts( get() { lock.write { if (_all.isEmpty()) { - val all = createContexts(client.get(), client.get()?.config) + try { + val all = createContexts(client.get(), client.get()?.config) _all.addAll(all) + } catch (e: Exception) { + // + } } return _all } diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocumentTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocumentTest.kt new file mode 100644 index 000000000..e2e675613 --- /dev/null +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocumentTest.kt @@ -0,0 +1,69 @@ +package com.redhat.devtools.intellij.kubernetes.editor.describe + +import com.redhat.devtools.intellij.kubernetes.editor.describe.DescribeDocument.* +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class YAMLDescribeDocumentTest { + + @Test + fun `toText should return empty String if document has no paragraphs`() { + // given + val document = YAMLDescribeDocument() + // when + val text = document.toText() + // then + assertThat(text).isEqualTo("") + } + + @Test + fun `toText should return title and value if document has 1 paragraph`() { + // given + val document = YAMLDescribeDocument() + document.add("jedi", "luke") + // when + val text = document.toText() + // then + assertThat(text).isEqualTo("jedi: luke\n") + } + + @Test + fun `toText should return title, subtitle and values if document has 1 paragraph with a nested paragraph`() { + // given + val document = YAMLDescribeDocument() + val jedis = Paragraph("jedis") + .add(Paragraph("padawan", "luke")) + .add(Paragraph("master", "yoda")) + document.add(jedis) + // when + val text = document.toText() + // then + assertThat(text).isEqualTo( + "jedis:\n" + + " padawan: luke\n" + + " master: yoda\n" + ) + } + + @Test + fun `toText should return title, subtitle and a title if document has a paragraph, a nested one and a paragraph again`() { + // given + val document = YAMLDescribeDocument() + val jedis = Paragraph("jedis") + .add(Paragraph("padawan", "luke")) + val siths = Paragraph("sith") + .add(Paragraph("master", "darth vader")) + document + .add(jedis) + .add(siths) + // when + val text = document.toText() + // then + assertThat(text).isEqualTo( + "jedis:\n" + + " padawan: luke\n" + + "sith:\n" + + " master: darth vader\n" + ) + } +} \ No newline at end of file