func Run()

in terraformer/pkg/terraformer/spec.go [40:99]


func Run(_ context.Context, reader io.Reader, environmentParam string, noTokenWrap bool, templateRaw string, w io.Writer) error {
	// Drain input reader
	specificationRaw, err := io.ReadAll(reader)
	if err != nil {
		return fmt.Errorf("unable to read input specification: %w", err)
	}

	// Load YAML to Protobuf
	def, err := loadFromYAML(specificationRaw)
	if err != nil {
		return fmt.Errorf("unable to deserialize specification: %w", err)
	}

	// Validate specification
	if err = validate(def); err != nil {
		return fmt.Errorf("unable to validate specification: %w", err)
	}

	// Serialize as protobuf
	specProto, err := proto.Marshal(def)
	if err != nil {
		return fmt.Errorf("unable to prepare specification hash: %w", err)
	}

	// Calculate spechash
	specHash := sha256.Sum256(specProto)

	// Compile the definition
	m, err := compile(environmentParam, def, base64.StdEncoding.EncodeToString(specHash[:]), noTokenWrap)
	if err != nil {
		return fmt.Errorf("unable to compile specification: %w", err)
	}

	// Prepare template
	t, err := template.New("tf").Parse(templateRaw)
	if err != nil {
		return fmt.Errorf("unable ot compile template: %w", err)
	}

	// Merge with template
	var out bytes.Buffer
	err = t.Execute(&out, m)
	if err != nil {
		return fmt.Errorf("unable to merge template with specification: %w", err)
	}

	// Format output
	formatted, err := hcl.Format(&out)
	if err != nil {
		return fmt.Errorf("unable to format terraform output: %w", err)
	}

	// Write result to writer
	if _, err := w.Write(formatted); err != nil {
		return fmt.Errorf("unable to write output result: %w", err)
	}

	// No error
	return nil
}