-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlocal.go
More file actions
122 lines (104 loc) · 2.55 KB
/
local.go
File metadata and controls
122 lines (104 loc) · 2.55 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package provider
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/sst/sst/v3/internal/util"
"github.com/sst/sst/v3/pkg/global"
)
type LocalHome struct {
}
func NewLocalHome() *LocalHome {
return &LocalHome{}
}
func (l *LocalHome) Bootstrap() error {
return nil
}
func (l *LocalHome) cleanup(key, app, stage string) error {
return nil
}
func (l *LocalHome) getData(key, app, stage string) (io.Reader, error) {
p := l.pathForData(key, app, stage)
result, err := os.Open(p)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return result, nil
}
func (l *LocalHome) putData(key, app, stage string, data io.Reader) error {
if key == "summary" {
return nil
}
p := l.pathForData(key, app, stage)
err := os.MkdirAll(filepath.Dir(p), 0755)
if err != nil {
return err
}
file, err := os.Create(p)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, data)
if err != nil {
return err
}
return nil
}
func (l *LocalHome) removeData(key, app, stage string) error {
p := l.pathForData(key, app, stage)
return os.Remove(p)
}
func (c *LocalHome) removePassphrase(app, stage string) error {
return c.removeData("passphrase", app, stage)
}
// these should go into secrets manager once it's out of beta
func (c *LocalHome) setPassphrase(app, stage string, passphrase string) error {
return c.putData("passphrase", app, stage, bytes.NewReader([]byte(passphrase)))
}
func (c *LocalHome) getPassphrase(app, stage string) (string, error) {
data, err := c.getData("passphrase", app, stage)
if err != nil {
return "", err
}
if data == nil {
return "", nil
}
read, err := io.ReadAll(data)
if err != nil {
return "", err
}
return string(read), nil
}
func (l *LocalHome) pathForData(key, app, stage string) string {
return filepath.Join(global.ConfigDir(), "state", key, app, fmt.Sprintf("%v.json", stage))
}
func (a *LocalHome) listStages(app string) ([]string, error) {
path := filepath.Join(global.ConfigDir(), "state", "app", app)
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
var stages []string
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".json") {
stageName := strings.TrimSuffix(entry.Name(), ".json")
if hasResources(a, app, stageName) {
stages = append(stages, stageName)
}
}
}
return stages, nil
}
func (c *LocalHome) info() (util.KeyValuePairs[string], error) {
return util.KeyValuePairs[string]{
{Key: "Provider", Value: "Local"},
{Key: "Path", Value: global.ConfigDir()},
}, nil
}