Class YamlParser

java.lang.Object
dev.zucca_ops.kustomtrace.parser.YamlParser

public class YamlParser extends Object
Utility class for parsing YAML files, particularly Kubernetes manifests and Kustomization files, using the SnakeYAML library.
  • Constructor Details

    • YamlParser

      public YamlParser()
  • Method Details

    • parseFile

      public static List<Map<String,Object>> parseFile(Path path) throws FileNotFoundException, InvalidContentException
      Parses a YAML file, potentially containing multiple YAML documents (separated by "---"). Each document is expected to be a YAML map (key-value structure).
      Parameters:
      path - The Path to the YAML file to be parsed.
      Returns:
      A list of maps, where each map represents a YAML document in the file. Returns an empty list if the file is empty or contains only null documents.
      Throws:
      FileNotFoundException - If the file specified by the path does not exist, is a directory, or cannot be read.
      InvalidContentException - If any document in the YAML file is not a map (e.g., it's a list or a scalar at the root).
    • parseKustomizationFile

      public static Map<String,Object> parseKustomizationFile(Path path) throws InvalidContentException, FileNotFoundException
      Parses a Kustomization file, which is expected to be a single YAML document representing a map.
      Parameters:
      path - The Path to the Kustomization file.
      Returns:
      A map representing the parsed Kustomization content.
      Throws:
      FileNotFoundException - If the file does not exist or cannot be read.
      InvalidContentException - If the file is empty, contains multiple YAML documents, or if the document is not a map.
    • getKind

      public static String getKind(Map<String,Object> document)
      Safely extracts the 'kind' attribute as a String from a parsed YAML document.
      Parameters:
      document - A Map representing the parsed YAML document.
      Returns:
      The String value of the 'kind' field if present and a String; otherwise, returns null.