github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/config.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  
     7  	"github.com/hashicorp/hcl"
     8  )
     9  
    10  // Config is the structure of the configuration for the Otto CLI.
    11  //
    12  // This is not the configuration for Otto itself. That is in the
    13  // "config" package.
    14  type Config struct {
    15  	DisableCheckpoint          bool `hcl:"disable_checkpoint"`
    16  	DisableCheckpointSignature bool `hcl:"disable_checkpoint_signature"`
    17  }
    18  
    19  // BuiltinConfig is the built-in defaults for the configuration. These
    20  // can be overridden by user configurations.
    21  var BuiltinConfig Config
    22  
    23  // ConfigFile returns the default path to the configuration file.
    24  //
    25  // On Unix-like systems this is the ".ottorc" file in the home directory.
    26  // On Windows, this is the "otto.rc" file in the application data
    27  // directory.
    28  func ConfigFile() (string, error) {
    29  	return configFile()
    30  }
    31  
    32  // ConfigDir returns the configuration directory for Otto.
    33  func ConfigDir() (string, error) {
    34  	return configDir()
    35  }
    36  
    37  // LoadConfig loads the CLI configuration from ".ottorc" files.
    38  func LoadConfig(path string) (*Config, error) {
    39  	// Read the HCL file and prepare for parsing
    40  	d, err := ioutil.ReadFile(path)
    41  	if err != nil {
    42  		return nil, fmt.Errorf(
    43  			"Error reading %s: %s", path, err)
    44  	}
    45  
    46  	// Parse it
    47  	obj, err := hcl.Parse(string(d))
    48  	if err != nil {
    49  		return nil, fmt.Errorf(
    50  			"Error parsing %s: %s", path, err)
    51  	}
    52  
    53  	// Build up the result
    54  	var result Config
    55  	if err := hcl.DecodeObject(&result, obj); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return &result, nil
    60  }