1
1
package ntoml
2
2
3
3
import (
4
- "encoding/json"
5
4
"fmt"
6
5
"io/ioutil"
7
6
"os"
8
7
"path"
9
8
"path/filepath"
10
- "strings"
11
9
12
10
"github.com/BurntSushi/toml"
13
11
"github.com/pkg/errors"
14
- yaml "gopkg.in/yaml.v3"
15
12
)
16
13
17
14
const DefaultFilename = "netlify.toml"
18
15
19
- // cf. https://github.com/netlify/build/blob/3c9cf4dda7a39994a3f0f1a544242d386b2bc2dd/packages/%40netlify-config/path.js#L16
20
- var netlifyConfigFileNames = []string {
21
- "netlify.toml" , "netlify.yml" , "netlify.yaml" , "netlify.json" ,
22
- }
23
-
24
16
type NetlifyToml struct {
25
- Settings Settings `toml:"settings" json:"settings" yaml:"settings" `
17
+ Settings Settings `toml:"settings"`
26
18
27
- Redirects []Redirect `toml:"redirects,omitempty" json:"redirects,omitempty" yaml:"redirects,omitempty" `
19
+ Redirects []Redirect `toml:"redirects,omitempty"`
28
20
29
21
// this is the default context
30
- Build * BuildConfig `toml:"build" json:"build" yaml:"build" `
31
- Plugins []Plugin `toml:"plugins" json:"plugins" yaml:"plugins" `
32
- Context map [string ]DeployContext `toml:"context,omitempty" json:"context,omitempty" yaml:"context,omitempty" `
22
+ Build * BuildConfig `toml:"build"`
23
+ Plugins []Plugin `toml:"plugins"`
24
+ Context map [string ]DeployContext `toml:"context,omitempty"`
33
25
}
34
26
35
27
type Settings struct {
36
- ID string `toml:"id" json:"id" yaml:"id" `
37
- Path string `toml:"path" json:"path" yaml:"path" `
28
+ ID string `toml:"id"`
29
+ Path string `toml:"path"`
38
30
}
39
31
40
32
type BuildConfig struct {
41
- Command string `toml:"command" json:"command" yaml:"command" `
42
- Base string `toml:"base" json:"base" yaml:"base" `
43
- Publish string `toml:"publish" json:"publish" yaml:"publish" `
44
- Ignore string `toml:"ignore" json:"ignore" yaml:"ignore" `
45
- Environment map [string ]string `toml:"environment" json:"environment" yaml:"environment" `
46
- Functions string `toml:"functions" json:"functions" yaml:"functions" `
47
- EdgeHandlers string `toml:"edge_handlers" json:"edge_handlers" yaml:"edge_handlers" `
33
+ Command string `toml:"command"`
34
+ Base string `toml:"base"`
35
+ Publish string `toml:"publish"`
36
+ Ignore string `toml:"ignore"`
37
+ Environment map [string ]string `toml:"environment"`
38
+ Functions string `toml:"functions"`
39
+ EdgeHandlers string `toml:"edge_handlers"`
48
40
}
49
41
50
42
type Plugin struct {
51
- Package string `toml:"package" json:"package" yaml:"package" `
52
- PinnedVersion string `toml:"pinned_version,omitempty" json:"pinned_version,omitempty" yaml:"pinned_version,omitempty" `
43
+ Package string `toml:"package"`
44
+ PinnedVersion string `toml:"pinned_version,omitempty"`
53
45
}
54
46
55
47
type DeployContext struct {
56
48
BuildConfig `yaml:",inline"`
57
49
}
58
50
59
51
type Redirect struct {
60
- Origin string `toml:"origin" json:"origin" yaml:"origin" `
61
- Destination string `toml:"destination" json:"destination" yaml:"destination" `
62
- Parmeters map [string ]string `toml:"parameters" json:"parameters" yaml:"parameters" `
63
- Status int `toml:"status" json:"status" yaml:"status" `
64
- Force bool `toml:"force" json:"force" yaml:"force" `
65
- Conditions * RedirectCondition `toml:"conditions" json:"conditions" yaml:"conditions" `
66
- Headers map [string ]string `toml:"headers" json:"headers" yaml:"headers" `
52
+ Origin string `toml:"origin"`
53
+ Destination string `toml:"destination"`
54
+ Parmeters map [string ]string `toml:"parameters"`
55
+ Status int `toml:"status"`
56
+ Force bool `toml:"force"`
57
+ Conditions * RedirectCondition `toml:"conditions"`
58
+ Headers map [string ]string `toml:"headers"`
67
59
}
68
60
69
61
type RedirectCondition struct {
70
- Language []string `toml:"language" json:"language" yaml:"language" `
71
- Country []string `toml:"country" json:"country" yaml:"country" `
72
- Role []string `toml:"role" json:"role" yaml:"role" `
62
+ Language []string `toml:"language"`
63
+ Country []string `toml:"country"`
64
+ Role []string `toml:"role"`
73
65
}
74
66
75
67
type FoundNoConfigPathError struct {
76
68
base string
77
- checked [] string
69
+ checked string
78
70
}
79
71
80
72
func (f * FoundNoConfigPathError ) Error () string {
81
73
return fmt .Sprintf ("No Netlify configuration file found." )
82
74
}
83
75
84
- type FoundMoreThanOneConfigPathError struct {
85
- base string
86
- checked []string
87
- found []string
88
- }
89
-
90
- func (f * FoundMoreThanOneConfigPathError ) Error () string {
91
- return fmt .Sprintf ("Multiple potential Netlify configuration files in \" %s\" : %s" , f .base , strings .Join (f .found , ", " ))
92
- }
76
+ func GetNetlifyConfigPath (base string ) (path string , err error ) {
77
+ filePath := filepath .Join (base , DefaultFilename )
93
78
94
- func findOnlyOneExistingPath (base string , paths ... string ) (path string , err error ) {
95
- foundPaths := make ([]string , 0 , len (paths ))
96
- for _ , possiblePath := range paths {
97
- p := filepath .Join (base , possiblePath )
98
- if fi , err := os .Stat (p ); err == nil && ! fi .IsDir () {
99
- foundPaths = append (foundPaths , p )
100
- }
101
- }
102
- if len (foundPaths ) == 0 {
103
- return "" , & FoundNoConfigPathError {base : base , checked : paths }
79
+ if fi , err := os .Stat (filePath ); err == nil && ! fi .IsDir () {
80
+ return filePath , nil
81
+ } else {
82
+ return "" , & FoundNoConfigPathError {base : base , checked : DefaultFilename }
104
83
}
105
- if len (foundPaths ) > 1 {
106
- foundFilenames := make ([]string , 0 , len (foundPaths ))
107
- for _ , foundPath := range foundPaths {
108
- foundFilenames = append (foundFilenames , filepath .Base (foundPath ))
109
- }
110
- return "" , & FoundMoreThanOneConfigPathError {base : base , checked : paths , found : foundFilenames }
111
- }
112
- return foundPaths [0 ], nil
113
- }
114
-
115
- func GetNetlifyConfigPath (base string ) (path string , err error ) {
116
- return findOnlyOneExistingPath (base , netlifyConfigFileNames ... )
117
84
}
118
85
119
86
func Load () (* NetlifyToml , error ) {
@@ -136,29 +103,12 @@ func LoadFrom(paths ...string) (*NetlifyToml, error) {
136
103
out := new (NetlifyToml )
137
104
138
105
for _ , p := range paths {
139
- extension := filepath .Ext (p )
140
-
141
106
if data , ferr := ioutil .ReadFile (p ); ! os .IsNotExist (ferr ) {
142
107
if ferr != nil {
143
108
return nil , errors .Wrapf (ferr , "Error while reading in file %s" , p )
144
109
}
145
110
146
- var derr error
147
-
148
- switch extension {
149
- case ".toml" :
150
- derr = toml .Unmarshal (data , out )
151
- case ".json" :
152
- derr = json .Unmarshal (data , out )
153
- case ".yaml" :
154
- fallthrough
155
- case ".yml" :
156
- derr = yaml .Unmarshal (data , out )
157
- default :
158
- return nil , errors .New (fmt .Sprintf ("Invalid config extension %s of path %s" , extension , p ))
159
- }
160
-
161
- if derr != nil {
111
+ if derr := toml .Unmarshal (data , out ); derr != nil {
162
112
return nil , errors .Wrapf (derr , "Error while decoding file %s" , p )
163
113
}
164
114
0 commit comments