本文整理匯總了Golang中github.com/hashicorp/hcl.Parse函數的典型用法代碼示例。如果您正苦於以下問題:Golang Parse函數的具體用法?Golang Parse怎麽用?Golang Parse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Parse函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: loadVarFile
func loadVarFile(rawPath string) (map[string]string, error) {
path, err := homedir.Expand(rawPath)
if err != nil {
return nil, fmt.Errorf(
"Error expanding path: %s", err)
}
// Read the HCL file and prepare for parsing
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf(
"Error reading %s: %s", path, err)
}
// Parse it
obj, err := hcl.Parse(string(d))
if err != nil {
return nil, fmt.Errorf(
"Error parsing %s: %s", path, err)
}
var result map[string]string
if err := hcl.DecodeObject(&result, obj); err != nil {
return nil, err
}
return result, nil
}
示例2: LoadSSHHelperConfig
// LoadSSHHelperConfig loads ssh-helper's configuration from the file and populates the corresponding
// in-memory structure.
//
// Vault address is a required parameter.
// Mount point defaults to "ssh".
func LoadSSHHelperConfig(path string) (*SSHHelperConfig, error) {
var config SSHHelperConfig
contents, err := ioutil.ReadFile(path)
if !os.IsNotExist(err) {
obj, err := hcl.Parse(string(contents))
if err != nil {
return nil, err
}
if err := hcl.DecodeObject(&config, obj); err != nil {
return nil, err
}
} else {
return nil, err
}
if config.VaultAddr == "" {
return nil, fmt.Errorf("config missing vault_addr")
}
if config.SSHMountPoint == "" {
config.SSHMountPoint = SSHHelperDefaultMountPoint
}
return &config, nil
}
示例3: Parse
// Parse parses the detector config from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*Config, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
obj, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
var result Config
// Parse the detects
if o := obj.Get("detect", false); o != nil {
if err := parseDetect(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'import': %s", err)
}
}
return &result, nil
}
示例4: ParseConfig
// ParseConfig parses the config from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func ParseConfig(r io.Reader) (*Config, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
root, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Top-level item should be a list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing: root should be an object")
}
var config Config
if err := parseConfig(&config, list); err != nil {
return nil, fmt.Errorf("error parsing 'config': %v", err)
}
return &config, nil
}
示例5: Parse
// Parse parses the job spec from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*structs.Job, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
root, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Top-level item should be a list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing: root should be an object")
}
var job structs.Job
// Parse the job out
matches := list.Filter("job")
if len(matches.Items) == 0 {
return nil, fmt.Errorf("'job' stanza not found")
}
if err := parseJob(&job, matches); err != nil {
return nil, fmt.Errorf("error parsing 'job': %s", err)
}
return &job, nil
}
示例6: loadKVFile
func loadKVFile(rawPath string) (map[string]interface{}, error) {
path, err := homedir.Expand(rawPath)
if err != nil {
return nil, fmt.Errorf(
"Error expanding path: %s", err)
}
// Read the HCL file and prepare for parsing
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf(
"Error reading %s: %s", path, err)
}
// Parse it
obj, err := hcl.Parse(string(d))
if err != nil {
return nil, fmt.Errorf(
"Error parsing %s: %s", path, err)
}
var result map[string]interface{}
if err := hcl.DecodeObject(&result, obj); err != nil {
return nil, fmt.Errorf(
"Error decoding Terraform vars file: %s\n\n"+
"The vars file should be in the format of `key = \"value\"`.\n"+
"Decoding errors are usually caused by an invalid format.",
err)
}
return result, nil
}
示例7: Parse
// Parse parses the job spec from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*structs.Job, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
obj, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
var job structs.Job
// Parse the job out
jobO := obj.Get("job", false)
if jobO == nil {
return nil, fmt.Errorf("'job' stanza not found")
}
if err := parseJob(&job, jobO); err != nil {
return nil, fmt.Errorf("error parsing 'job': %s", err)
}
return &job, nil
}
示例8: LoadConfig
// LoadConfig reads the configuration from the given path. If path is
// empty, then the default path will be used, or the environment variable
// if set.
func LoadConfig(path string) (*Config, error) {
if path == "" {
path = DefaultConfigPath
}
if v := os.Getenv(ConfigPathEnv); v != "" {
path = v
}
path, err := homedir.Expand(path)
if err != nil {
return nil, fmt.Errorf("Error expanding config path: %s", err)
}
var config Config
contents, err := ioutil.ReadFile(path)
if !os.IsNotExist(err) {
if err != nil {
return nil, err
}
obj, err := hcl.Parse(string(contents))
if err != nil {
return nil, err
}
if err := hcl.DecodeObject(&config, obj); err != nil {
return nil, err
}
}
return &config, nil
}
示例9: ParseSSHHelperConfig
// ParseSSHHelperConfig parses the given contents as a string for the SSHHelper
// configuration.
func ParseSSHHelperConfig(contents string) (*SSHHelperConfig, error) {
root, err := hcl.Parse(string(contents))
if err != nil {
return nil, fmt.Errorf("ssh_helper: error parsing config: %s", err)
}
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("ssh_helper: error parsing config: file doesn't contain a root object")
}
valid := []string{
"vault_addr",
"ssh_mount_point",
"ca_cert",
"ca_path",
"allowed_cidr_list",
"allowed_roles",
"tls_skip_verify",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, multierror.Prefix(err, "ssh_helper:")
}
var c SSHHelperConfig
c.SSHMountPoint = SSHHelperDefaultMountPoint
if err := hcl.DecodeObject(&c, list); err != nil {
return nil, multierror.Prefix(err, "ssh_helper:")
}
if c.VaultAddr == "" {
return nil, fmt.Errorf("ssh_helper: missing config 'vault_addr'")
}
return &c, nil
}
示例10: ParseClusterFromFile
// ParseClusterFromFile reads a cluster from file
func ParseClusterFromFile(path string) (*Cluster, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, maskAny(err)
}
// Parse the input
root, err := hcl.Parse(string(data))
if err != nil {
return nil, maskAny(err)
}
// Top-level item should be a list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, errgo.New("error parsing: root should be an object")
}
matches := list.Filter("cluster")
if len(matches.Items) == 0 {
return nil, errgo.New("'cluster' stanza not found")
}
// Parse hcl into Cluster
cluster := &Cluster{}
if err := cluster.parse(matches); err != nil {
return nil, maskAny(err)
}
cluster.setDefaults()
// Validate the cluster
if err := cluster.validate(); err != nil {
return nil, maskAny(err)
}
return cluster, nil
}
示例11: main
func main() {
for i, arg := range os.Args {
if i == 0 {
continue
}
search := arg
if info, err := os.Stat(arg); err == nil && info.IsDir() {
search = fmt.Sprintf("%s/*.tf", arg)
}
files, err := filepath.Glob(search)
if err != nil {
colorstring.Printf("[red]Error finding files: %s", err)
}
for _, filename := range files {
fmt.Printf("Checking %s ... ", filename)
file, err := ioutil.ReadFile(filename)
if err != nil {
colorstring.Printf("[red]Error reading file: %s\n", err)
break
}
_, err = hcl.Parse(string(file))
if err != nil {
colorstring.Printf("[red]Error parsing file: %s\n", err)
break
}
colorstring.Printf("[green]OK!\n")
}
}
}
示例12: ParseConfig
// ParseConfig parses the given configuration as a string.
func ParseConfig(contents string) (*DefaultConfig, error) {
root, err := hcl.Parse(contents)
if err != nil {
return nil, err
}
// Top-level item should be the object list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("Failed to parse config: does not contain a root object")
}
valid := []string{
"token_helper",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, err
}
var c DefaultConfig
if err := hcl.DecodeObject(&c, list); err != nil {
return nil, err
}
return &c, nil
}
示例13: Parse
// Parse parses the detector config from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*Config, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
root, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Top-level item should be the object list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
}
var result Config
// Parse the detects
if o := list.Filter("detect"); len(o.Items) > 0 {
if err := parseDetect(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'import': %s", err)
}
}
return &result, nil
}
示例14: LoadConfigFile
// LoadConfigFile loads the configuration from the given file.
func LoadConfigFile(path string) (*Config, error) {
// Read the file
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
// Parse!
obj, err := hcl.Parse(string(d))
if err != nil {
return nil, err
}
// Start building the result
var result Config
if err := hcl.DecodeObject(&result, obj); err != nil {
return nil, err
}
if objs := obj.Get("listener", false); objs != nil {
result.Listeners, err = loadListeners(objs)
if err != nil {
return nil, err
}
}
if objs := obj.Get("backend", false); objs != nil {
result.Backend, err = loadBackend(objs)
if err != nil {
return nil, err
}
}
return &result, nil
}
示例15: parseJob
// ParseJob takes input from a given reader and parses it into a Job.
func parseJob(input []byte, jf *jobFunctions) (*Job, error) {
// Create a template, add the function map, and parse the text.
tmpl, err := template.New("job").Funcs(jf.Functions()).Parse(string(input))
if err != nil {
return nil, maskAny(err)
}
// Run the template to verify the output.
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, jf.Options())
if err != nil {
return nil, maskAny(err)
}
// Parse the input
root, err := hcl.Parse(buffer.String())
if err != nil {
return nil, maskAny(err)
}
// Top-level item should be a list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, errgo.New("error parsing: root should be an object")
}
// Parse hcl into Job
job := &Job{}
matches := list.Filter("job")
if len(matches.Items) == 0 {
return nil, maskAny(errgo.WithCausef(nil, ValidationError, "'job' stanza not found"))
}
if err := job.parse(matches); err != nil {
return nil, maskAny(err)
}
// Link internal structures
job.prelink()
// Set defaults
job.setDefaults(jf.cluster)
// Replace variables
if err := job.replaceVariables(); err != nil {
return nil, maskAny(err)
}
// Sort internal structures and make final links
job.link()
// Optimize job for cluster
job.optimizeFor(jf.cluster)
// Validate the job
if err := job.Validate(); err != nil {
return nil, maskAny(err)
}
return job, nil
}