github.com/opentofu/opentofu@v1.7.1/internal/encryption/config/config_load.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package config
     7  
     8  import (
     9  	"strings"
    10  
    11  	"github.com/hashicorp/hcl/v2"
    12  	"github.com/hashicorp/hcl/v2/hclsyntax"
    13  	"github.com/hashicorp/hcl/v2/json"
    14  )
    15  
    16  // LoadConfigFromString loads a configuration from a string. The sourceName is used to identify the source of the
    17  // configuration in error messages.
    18  // This method serves as an example for how someone using this library might want to load a configuration.
    19  // if they were not using gohcl directly.
    20  // However! Right now, this method should only be used in tests, as OpenTofu should be using gohcl to parse the configuration.
    21  func LoadConfigFromString(sourceName string, rawInput string) (*EncryptionConfig, hcl.Diagnostics) {
    22  	var diags hcl.Diagnostics
    23  	var file *hcl.File
    24  
    25  	if strings.TrimSpace(rawInput)[0] == '{' {
    26  		file, diags = json.Parse([]byte(rawInput), sourceName)
    27  	} else {
    28  		file, diags = hclsyntax.ParseConfig([]byte(rawInput), sourceName, hcl.Pos{Byte: 0, Line: 1, Column: 1})
    29  	}
    30  
    31  	cfg, cfgDiags := DecodeConfig(file.Body, hcl.Range{Filename: sourceName})
    32  	diags = append(diags, cfgDiags...)
    33  
    34  	return cfg, diags
    35  }