MCPcopy Create free account
hub / github.com/basecamp/hey-cli / saveAllToFile

Method saveAllToFile

internal/auth/store.go:132–173  ·  view source on GitHub ↗
(all map[string]*Credentials)

Source from the content-addressed store, hash-verified

130}
131
132func (s *Store) saveAllToFile(all map[string]*Credentials) error {
133 if err := os.MkdirAll(s.fallbackDir, 0700); err != nil {
134 return err
135 }
136
137 data, err := json.MarshalIndent(all, "", " ") //nolint:gosec // G117: intentional credential marshaling for storage
138 if err != nil {
139 return err
140 }
141
142 tmpFile, err := os.CreateTemp(s.fallbackDir, "credentials-*.json.tmp")
143 if err != nil {
144 return err
145 }
146 tmpPath := tmpFile.Name()
147
148 if _, err := tmpFile.Write(data); err != nil {
149 _ = tmpFile.Close()
150 _ = os.Remove(tmpPath) //nolint:gosec // G703: path from os.CreateTemp
151 return err
152 }
153 if err := tmpFile.Chmod(0600); err != nil {
154 _ = tmpFile.Close()
155 _ = os.Remove(tmpPath) //nolint:gosec // G703: path from os.CreateTemp
156 return err
157 }
158 if err := tmpFile.Close(); err != nil {
159 _ = os.Remove(tmpPath) //nolint:gosec // G703: path from os.CreateTemp
160 return err
161 }
162
163 destPath := s.credentialsPath()
164 if err := os.Rename(tmpPath, destPath); err != nil { //nolint:gosec // G703: paths constructed internally
165 if runtime.GOOS == "windows" {
166 _ = os.Remove(destPath)
167 return os.Rename(tmpPath, destPath) //nolint:gosec // G703: same
168 }
169 _ = os.Remove(tmpPath) //nolint:gosec // G703: path from os.CreateTemp
170 return err
171 }
172 return nil
173}
174
175func (s *Store) loadFromFile(origin string) (*Credentials, error) {
176 all, err := s.loadAllFromFile()

Callers 2

saveToFileMethod · 0.95
deleteFileMethod · 0.95

Calls 1

credentialsPathMethod · 0.95

Tested by

no test coverage detected