github.com/lukahartwig/terraform@v0.11.4-0.20180302171601-664391c254ea/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  		// FIXME: homeDir gets called from globalPluginDirs during init, before
    36  		// the logging is setup.  We should move meta initializtion outside of
    37  		// init, but in the meantime we just need to silence this output.
    38  		//log.Printf("[DEBUG] Detected home directory from env var: %s", home)
    39  
    40  		return home, nil
    41  	}
    42  
    43  	// If that fails, try the shell
    44  	var stdout bytes.Buffer
    45  	cmd := exec.Command("sh", "-c", "eval echo ~$USER")
    46  	cmd.Stdout = &stdout
    47  	if err := cmd.Run(); err != nil {
    48  		return "", err
    49  	}
    50  
    51  	result := strings.TrimSpace(stdout.String())
    52  	if result == "" {
    53  		return "", errors.New("blank output")
    54  	}
    55  
    56  	return result, nil
    57  }