forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_path.go
58 lines (47 loc) · 1.37 KB
/
binary_path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package terraform
import (
"os"
"path/filepath"
"time"
"github.com/cloudfoundry/bosh-bootloader/terraform/binary_dist"
"github.com/spf13/afero"
)
const tfBinDataAssetName = "terraform"
const bblTfBinaryName = "bbl-terraform"
func BinaryPath() (string, error) {
return BinaryPathInjected(afero.Afero{afero.NewOsFs()})
}
type tfBinaryPathFs interface {
GetTempDir(string) string
Exists(string) (bool, error)
Stat(string) (os.FileInfo, error)
WriteFile(string, []byte, os.FileMode) error
Chtimes(name string, atime time.Time, mtime time.Time) error
}
func BinaryPathInjected(fs tfBinaryPathFs) (string, error) {
path := filepath.Join(fs.GetTempDir(""), bblTfBinaryName)
exists, err := fs.Exists(path)
if err != nil {
return "", err
}
if !exists {
return path, installTfBinary(path, fs)
}
foundBinaryFileInfo, err := fs.Stat(path)
if err != nil {
return "", err
}
foundBinaryModTime := foundBinaryFileInfo.ModTime()
distModTime := binary_dist.MustAssetInfo(tfBinDataAssetName).ModTime()
if !distModTime.After(foundBinaryModTime) {
return path, nil
}
return path, installTfBinary(path, fs)
}
func installTfBinary(path string, fs tfBinaryPathFs) error {
err := fs.WriteFile(path, binary_dist.MustAsset(tfBinDataAssetName), os.ModePerm)
if err != nil {
return err
}
return fs.Chtimes(path, time.Now(), binary_dist.MustAssetInfo(tfBinDataAssetName).ModTime())
}