MCPcopy
hub / github.com/spf13/viper / writeConfig

Method writeConfig

viper.go:1663–1699  ·  view source on GitHub ↗
(filename string, force bool)

Source from the content-addressed store, hash-verified

1661}
1662
1663func (v *Viper) writeConfig(filename string, force bool) error {
1664 v.logger.Info("attempting to write configuration to file")
1665
1666 var configType string
1667
1668 ext := filepath.Ext(filename)
1669 if ext != "" && ext != filepath.Base(filename) {
1670 configType = ext[1:]
1671 } else {
1672 configType = v.configType
1673 }
1674 if configType == "" {
1675 return fmt.Errorf("config type could not be determined for %s", filename)
1676 }
1677
1678 if !slices.Contains(SupportedExts, configType) {
1679 return UnsupportedConfigError(configType)
1680 }
1681 if v.config == nil {
1682 v.config = make(map[string]any)
1683 }
1684 flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
1685 if !force {
1686 flags |= os.O_EXCL
1687 }
1688 f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
1689 if err != nil {
1690 return err
1691 }
1692 defer f.Close()
1693
1694 if err := v.marshalWriter(f, configType); err != nil {
1695 return err
1696 }
1697
1698 return f.Sync()
1699}
1700
1701func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
1702 format := strings.ToLower(v.getConfigType())

Callers 3

WriteConfigMethod · 0.95
WriteConfigAsMethod · 0.95
SafeWriteConfigAsMethod · 0.95

Calls 2

marshalWriterMethod · 0.95
UnsupportedConfigErrorTypeAlias · 0.85

Tested by

no test coverage detected