forked from helmfile/chartify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempdir.go
102 lines (81 loc) · 1.78 KB
/
tempdir.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package chartify
import (
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"os"
"path/filepath"
"strings"
"github.com/davecgh/go-spew/spew"
)
func makeTempDir(release, chart string, opts *ChartifyOpts) string {
var err error
var id string
if opts.ID != "" {
id = strings.ReplaceAll(opts.ID, "/", string(filepath.Separator))
} else {
id, err = GenerateID(release, chart, opts)
if err != nil {
panic(err)
}
}
workDir := os.Getenv(EnvVarTempDir)
if workDir == "" {
workDir, err = os.MkdirTemp(os.TempDir(), "chartify")
if err != nil {
panic(err)
}
} else if !filepath.IsAbs(workDir) {
workDir, err = filepath.Abs(workDir)
if err != nil {
panic(err)
}
}
d := filepath.Join(workDir, id)
if os.Getenv(EnvVarDebug) != "" {
bs, _ := json.Marshal(map[string]interface{}{
"Release": release,
"Chart": chart,
"Options": opts,
})
if len(bs) > 0 {
_ = os.WriteFile(d+".json", bs, 0666)
}
}
info, err := os.Stat(d)
if err != nil && !errors.Is(err, os.ErrNotExist) {
panic(err)
} else if info == nil {
if err := os.MkdirAll(d, 0777); err != nil {
panic(err)
}
}
return d
}
func GenerateID(release, chart string, opts *ChartifyOpts) (string, error) {
var id []string
if opts.Namespace != "" {
id = append(id, opts.Namespace)
}
id = append(id, release)
hash, err := HashObject([]interface{}{release, chart, opts})
if err != nil {
return "", err
}
id = append(id, hash)
return strings.Join(id, "-"), nil
}
func HashObject(obj interface{}) (string, error) {
hash := fnv.New32a()
hash.Reset()
printer := spew.ConfigState{
Indent: " ",
SortKeys: true,
DisableMethods: true,
SpewKeys: true,
}
_, _ = printer.Fprintf(hash, "%#v", obj)
sum := fmt.Sprint(hash.Sum32())
return SafeEncodeString(sum), nil
}