-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
176 lines (163 loc) · 4.55 KB
/
Copy pathproject.go
File metadata and controls
176 lines (163 loc) · 4.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
// discoverProject walks from dir toward the filesystem root and returns
// the nearest regular file named .clau.toml, or "" if there is none.
func discoverProject(dir string) string {
for {
p := filepath.Join(dir, ".clau.toml")
if fi, err := os.Stat(p); err == nil && fi.Mode().IsRegular() {
return p
}
parent := filepath.Dir(dir)
if parent == dir {
return ""
}
dir = parent
}
}
// trustPath returns the trust store location. Like configPath, the
// XDG fallback (~/.local/state) is used on every platform.
func trustPath() string {
base := os.Getenv("XDG_STATE_HOME")
if base == "" {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
base = filepath.Join(home, ".local", "state")
}
return filepath.Join(base, "clau", "trust.toml")
}
type trustFile struct {
Trusted map[string]string `toml:"trusted"`
}
// loadTrust reads the trust store. A missing file is an empty store;
// corrupt=true means the file existed but did not parse (treated as
// empty — never a crash).
func loadTrust(path string) (map[string]string, bool) {
var tf trustFile
if _, err := toml.DecodeFile(path, &tf); err != nil {
if errors.Is(err, os.ErrNotExist) {
return map[string]string{}, false
}
return map[string]string{}, true
}
if tf.Trusted == nil {
tf.Trusted = map[string]string{}
}
return tf.Trusted, false
}
func saveTrust(path string, store map[string]string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return toml.NewEncoder(f).Encode(trustFile{Trusted: store})
}
// hashBytes returns the lowercase hex SHA-256 digest of data.
func hashBytes(data []byte) string {
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}
func hashFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
return hashBytes(data), nil
}
type ProjectStatus struct {
Path string // discovered .clau.toml; "" if none in play
Trusted bool // content hash matches the trust store
Changed bool // in the store, but content differs
Applied bool // layer merged into the returned Config
}
func sameFile(a, b string) bool {
fa, err := os.Stat(a)
if err != nil {
return false
}
fb, err := os.Stat(b)
if err != nil {
return false
}
return os.SameFile(fa, fb)
}
// loadEffectiveConfig loads the global config and, when a trusted
// .clau.toml is discovered walking up from cwd, layers it on top.
// enforce=true makes an untrusted or changed project file a hard error
// (launch paths); enforce=false returns the global-only view plus
// status (list, doctor). CLAU_NO_PROJECT (non-empty) skips discovery.
func loadEffectiveConfig(cwd string, enforce bool) (Config, ProjectStatus, error) {
global, err := loadConfig(configPath())
if err != nil {
return Config{}, ProjectStatus{}, err
}
if os.Getenv("CLAU_NO_PROJECT") != "" {
return global, ProjectStatus{}, nil
}
proj := discoverProject(cwd)
if proj == "" || sameFile(proj, configPath()) {
return global, ProjectStatus{}, nil
}
st := ProjectStatus{Path: proj}
data, err := os.ReadFile(proj)
if err != nil {
return Config{}, st, fmt.Errorf("project config %s: %v", proj, err)
}
hash := hashBytes(data)
store, _ := loadTrust(trustPath())
switch stored, known := store[proj]; {
case known && stored == hash:
st.Trusted = true
case known:
st.Changed = true
}
if !st.Trusted {
if enforce {
reason := "is not trusted"
if st.Changed {
reason = "changed since it was trusted"
}
return Config{}, st, fmt.Errorf(
"project config %s %s; review with `clau trust --show`, then `clau trust` to allow (CLAU_NO_PROJECT=1 skips project configs)",
proj, reason)
}
return global, st, nil
}
layered, err := applyConfigData(global, proj, data)
if err != nil {
return Config{}, st, err
}
st.Applied = true
return layered, st, nil
}
// projectDeclarations reports which model keys and profile names the
// project file itself declares, for provenance display. The file has
// already been validated by the time this is called.
func projectDeclarations(path string) (map[string]bool, map[string]bool, error) {
var raw rawConfig
if _, err := toml.DecodeFile(path, &raw); err != nil {
return nil, nil, err
}
models, profiles := map[string]bool{}, map[string]bool{}
for k := range raw.Models {
models[k] = true
}
for k := range raw.Profiles {
profiles[k] = true
}
return models, profiles, nil
}