/
local.go
74 lines (61 loc) · 1.46 KB
/
local.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package install
import (
"crypto/sha256"
"encoding/hex"
"io"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/gideaworx/terraform-exporter/runner"
)
func (i *Command) localInstall() error {
path, err := filepath.Abs(i.PluginName)
if err != nil {
return err
}
pluginDir := filepath.Join(i.pluginHomeDir, filepath.Base(path))
if err := os.MkdirAll(pluginDir, 0777); err != nil {
return err
}
targetFile, err := os.OpenFile(filepath.Join(pluginDir, "export-plugin"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
if err != nil {
return err
}
defer targetFile.Close()
hasher := sha256.New()
writer := io.MultiWriter(targetFile, hasher)
contents, err := os.Open(path)
if err != nil {
return err
}
defer contents.Close()
if _, err = io.Copy(writer, contents); err != nil {
return err
}
targetFile.Close()
integrity := &runner.PluginIntegrity{
Checksum: hex.EncodeToString(hasher.Sum(nil)),
Algorithm: "sha256",
}
info, err := runner.LoadPluginInfo(filepath.Base(path), integrity)
if err != nil {
return err
}
bom := runner.BillOfMaterials{
Name: filepath.Base(path),
Type: runner.Native,
Source: runner.PluginSource{
Type: "local-file",
Name: path,
},
Integrity: integrity,
Version: info.Version,
Provides: info.Provides,
}
bomFile, err := os.Create(filepath.Join(pluginDir, "export-plugin.bom"))
if err != nil {
return err
}
defer bomFile.Close()
return toml.NewEncoder(bomFile).Encode(bom)
}