forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
60 lines (49 loc) · 1.46 KB
/
config.go
File metadata and controls
60 lines (49 loc) · 1.46 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
package pgpkg
import (
"fmt"
"github.com/BurntSushi/toml"
"io"
)
// Load the settings
type configType struct {
Package string
Schema string `toml:",omitempty"` // for backward compatibility only
Schemas []string
Extensions []string
Uses []string
Migrations []string
}
// Read a configuration TOML file and update the package accordingly.
// If the package is already configured, it's an error.
func parseConfig(reader io.Reader) (*configType, error) {
var config configType
if _, err := toml.NewDecoder(reader).Decode(&config); err != nil {
return nil, fmt.Errorf("unable to read package config: %w", err)
}
// Convert single-schema name to slice, for backward compatibility
if config.Schema != "" && config.Schemas == nil {
config.Schemas = []string{config.Schema}
}
for _, schemaName := range config.Schemas {
if !schemaPattern.MatchString(schemaName) {
return nil, fmt.Errorf("illegal schema name in pgpkg.toml: %s", schemaName)
}
}
if err := CheckPackageName(config.Package); err != nil {
return nil, err
}
for _, uses := range config.Uses {
if err := CheckPackageName(uses); err != nil {
return nil, err
}
}
return &config, nil
}
func (c *configType) writeConfig(w io.Writer) error {
// "schema" is deprecated, so don't export it, even if it's set.
// To implement this, we make a copy of the config and then update it.
config := *c
config.Schema = ""
encoder := toml.NewEncoder(w)
return encoder.Encode(config)
}