MCPcopy Create free account
hub / github.com/docker/cli / Save

Method Save

cli/config/configfile/file.go:197–244  ·  view source on GitHub ↗

Save encodes and writes out all the authorization information

()

Source from the content-addressed store, hash-verified

195
196// Save encodes and writes out all the authorization information
197func (c *ConfigFile) Save() (retErr error) {
198 if c.Filename == "" {
199 return errors.New("can't save config with empty filename")
200 }
201
202 dir := filepath.Dir(c.Filename)
203 if err := os.MkdirAll(dir, 0o700); err != nil {
204 return err
205 }
206 temp, err := os.CreateTemp(dir, filepath.Base(c.Filename))
207 if err != nil {
208 return err
209 }
210 defer func() {
211 // ignore error as the file may already be closed when we reach this.
212 _ = temp.Close()
213 if retErr != nil {
214 if err := os.Remove(temp.Name()); err != nil {
215 logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file")
216 }
217 }
218 }()
219
220 err = c.SaveToWriter(temp)
221 if err != nil {
222 return err
223 }
224
225 if err := temp.Close(); err != nil {
226 return fmt.Errorf("error closing temp file: %w", err)
227 }
228
229 // Handle situation where the configfile is a symlink, and allow for dangling symlinks
230 cfgFile := c.Filename
231 if f, err := filepath.EvalSymlinks(cfgFile); err == nil {
232 cfgFile = f
233 } else if os.IsNotExist(err) {
234 // extract the path from the error if the configfile does not exist or is a dangling symlink
235 var pathError *os.PathError
236 if errors.As(err, &pathError) {
237 cfgFile = pathError.Path
238 }
239 }
240
241 // Try copying the current config file (if any) ownership and permissions
242 copyFilePermissions(cfgFile, temp.Name())
243 return os.Rename(temp.Name(), cfgFile)
244}
245
246// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and
247// then checking this against any environment variables provided to the container

Callers

nothing calls this directly

Calls 5

SaveToWriterMethod · 0.95
copyFilePermissionsFunction · 0.70
RemoveMethod · 0.65
CloseMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected