github.com/kevholditch/terraform@v0.9.7-0.20170613192930-9706042ddd51/config_unix.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"errors"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"strings"
    12  )
    13  
    14  func configFile() (string, error) {
    15  	dir, err := homeDir()
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  
    20  	return filepath.Join(dir, ".terraformrc"), nil
    21  }
    22  
    23  func configDir() (string, error) {
    24  	dir, err := homeDir()
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  
    29  	return filepath.Join(dir, ".terraform.d"), nil
    30  }
    31  
    32  func homeDir() (string, error) {
    33  	// First prefer the HOME environmental variable
    34  	if home := os.Getenv("HOME"); home != "" {
    35  
    36  		// FIXME: homeDir gets called from globalPluginDirs during init, before
    37  		// the logging is setup.  We should move meta initializtion outside of
    38  		// init, but in the meantime we just need to silence this output.
    39  		//log.Printf("[DEBUG] Detected home directory from env var: %s", home)
    40  
    41  		return home, nil
    42  	}
    43  
    44  	// If that fails, try the shell
    45  	var stdout bytes.Buffer
    46  	cmd := exec.Command("sh", "-c", "eval echo ~$USER")
    47  	cmd.Stdout = &stdout
    48  	if err := cmd.Run(); err != nil {
    49  		return "", err
    50  	}
    51  
    52  	result := strings.TrimSpace(stdout.String())
    53  	if result == "" {
    54  		return "", errors.New("blank output")
    55  	}
    56  
    57  	return result, nil
    58  }