gitfoss | 76fa798d00614f356f12b47b1582c87780ea9288 | packages/gitfoss-ci-runner/src/utils.cr ∙ GitFOSS
.cr
Crystal
(text/x-crystal)
require "yaml"

require "./command"

# Simple structs to map YAML; we will parse into native types directly.
def load_yaml(path : String) : YAML::Any
  YAML.parse(File.read(path))
end

def map_to_command(command_name : String, node : YAML::Any) : Command
  image = node["image"].as_s
  workdir = node["workdir"]?.try(&.as_s) || ""
  shell = node["shell"]?.try(&.as_s) || "sh"
  env = {} of String => String
  if node["env"]?
    node["env"].as_h.each do |k, v|
      env[k.as_s] = v.as_s
    end
  end

  commands = [] of String
  if node["commands"]?
    node["commands"].as_a.each do |c|
      commands << c.as_s
    end
  end

  cached = [] of String
  if node["cached"]?
    node["cached"].as_a.each do |c|
      cached << c.as_s
    end
  end

  artifacts_to_set = {} of String => String
  if node["stash"]?
    node["stash"].as_h.each do |k, v|
      artifacts_to_set[k.as_s] = v.as_s
    end
  end

  artifacts_to_get = {} of String => String
  if node["unstash"]?
    node["unstash"].as_h.each do |k, v|
      artifacts_to_get[k.as_s] = v.as_s
    end
  end

  Command.new(image,
    command_name,
    env,
    commands,
    cached,
    shell,
    workdir,
    artifacts_to_set,
    artifacts_to_get
  )
end