(filename string, force bool)
| 1661 | } |
| 1662 | |
| 1663 | func (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 | |
| 1701 | func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error { |
| 1702 | format := strings.ToLower(v.getConfigType()) |
no test coverage detected