mirror of https://github.com/hashicorp/packer
parent
d5c5306a97
commit
3590fae8bd
@ -1,41 +0,0 @@
|
||||
package packer
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ConfigFile returns the default path to the configuration file. On
|
||||
// Unix-like systems this is the ".packerconfig" file in the home directory.
|
||||
// On Windows, this is the "packer.config" file in the application data
|
||||
// directory.
|
||||
func ConfigFile() (string, error) {
|
||||
return configFile()
|
||||
}
|
||||
|
||||
// ConfigDir returns the configuration directory for Packer.
|
||||
func ConfigDir() (string, error) {
|
||||
return configDir()
|
||||
}
|
||||
|
||||
// ConfigTmpDir returns the configuration tmp directory for Packer
|
||||
func ConfigTmpDir() (string, error) {
|
||||
var tmpdir, td, cd string
|
||||
var err error
|
||||
|
||||
cd, _ = ConfigDir()
|
||||
for _, tmpdir = range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} {
|
||||
if tmpdir != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if td, err = ioutil.TempDir(tmpdir, "packer"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
return td, nil
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const EnvPackerTmpDir = "PACKER_TMP_DIR"
|
||||
|
||||
// ConfigFile returns the default path to the configuration file. On
|
||||
// Unix-like systems this is the ".packerconfig" file in the home directory.
|
||||
// On Windows, this is the "packer.config" file in the application data
|
||||
// directory.
|
||||
func ConfigFile() (string, error) {
|
||||
return configFile()
|
||||
}
|
||||
|
||||
// ConfigDir returns the configuration directory for Packer.
|
||||
func ConfigDir() (string, error) {
|
||||
return configDir()
|
||||
}
|
||||
|
||||
// ConfigTmpDir returns a "deterministic" (based on environment or Packer config)
|
||||
// path intended as the root of subsequent temporary items, to minimize scatter.
|
||||
//
|
||||
// The caller must ensure safe tempfile practice via ioutil.TempDir() and friends.
|
||||
func ConfigTmpDir() (string, error) {
|
||||
var tmpdir, td string
|
||||
var err error
|
||||
|
||||
if tmpdir = os.Getenv(EnvPackerTmpDir); tmpdir != "" {
|
||||
td, err = filepath.Abs(tmpdir)
|
||||
} else if tmpdir, err = configDir(); err == nil {
|
||||
td = filepath.Join(tmpdir, "tmp")
|
||||
} else if tmpdir = os.TempDir(); tmpdir != "" {
|
||||
td = filepath.Join(tmpdir, "packer")
|
||||
}
|
||||
|
||||
if _, err = os.Stat(td); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(td, 0700)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return td, nil
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testConfigTmpDir_impl(t *testing.T) string {
|
||||
var dir string
|
||||
|
||||
prefix, _ := ConfigTmpDir()
|
||||
if dir, err := ioutil.TempDir(prefix, ""); err == nil {
|
||||
defer os.RemoveAll(dir)
|
||||
} else {
|
||||
_ := fmt.Errorf("Error making directory: %s", err)
|
||||
}
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
func TestConfigTmpDir(t *testing.T) {
|
||||
testConfigTmpDir_impl(t)
|
||||
}
|
||||
|
||||
func TestConfigTmpDir_noenv_PackerTmpDir(t *testing.T) {
|
||||
oldenv := os.Getenv(EnvPackerTmpDir)
|
||||
defer os.Setenv(EnvPackerTmpDir, oldenv)
|
||||
os.Setenv(EnvPackerTmpDir, "")
|
||||
|
||||
dir1 := testConfigTmpDir_impl(t)
|
||||
|
||||
cd, err := ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("bad ConfigDir")
|
||||
}
|
||||
td := filepath.Join(cd, "tmp")
|
||||
os.Setenv(EnvPackerTmpDir, td)
|
||||
|
||||
dir2 := testConfigTmpDir_impl(t)
|
||||
|
||||
if filepath.Dir(dir1) != filepath.Dir(dir2) {
|
||||
t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigTmpDir_PackerTmpDir(t *testing.T) {
|
||||
oldenv := os.Getenv(EnvPackerTmpDir)
|
||||
defer os.Setenv(EnvPackerTmpDir, oldenv)
|
||||
os.Setenv(EnvPackerTmpDir, ".")
|
||||
|
||||
dir1 := testConfigTmpDir_impl(t)
|
||||
|
||||
abspath, err := filepath.Abs(".")
|
||||
if err != nil {
|
||||
t.Fatalf("bad absolute path")
|
||||
}
|
||||
dir2 := filepath.Join(abspath, "tmp")
|
||||
|
||||
if filepath.Dir(dir1) != filepath.Dir(dir2) {
|
||||
t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
// +build windows
|
||||
|
||||
package packer
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
Loading…
Reference in new issue