Skip to content

Commit adf801d

Browse files
4xposedDaniel Climent
andauthored
chore: remove support for netlify.json/.yaml/.yml (#348)
Co-authored-by: Daniel Climent <josedaniel@netlify.com>
1 parent f0e2dcc commit adf801d

File tree

2 files changed

+44
-221
lines changed

2 files changed

+44
-221
lines changed

ntoml/netlify_toml.go

Lines changed: 34 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,86 @@
11
package ntoml
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io/ioutil"
76
"os"
87
"path"
98
"path/filepath"
10-
"strings"
119

1210
"github.com/BurntSushi/toml"
1311
"github.com/pkg/errors"
14-
yaml "gopkg.in/yaml.v3"
1512
)
1613

1714
const DefaultFilename = "netlify.toml"
1815

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-
2416
type NetlifyToml struct {
25-
Settings Settings `toml:"settings" json:"settings" yaml:"settings"`
17+
Settings Settings `toml:"settings"`
2618

27-
Redirects []Redirect `toml:"redirects,omitempty" json:"redirects,omitempty" yaml:"redirects,omitempty"`
19+
Redirects []Redirect `toml:"redirects,omitempty"`
2820

2921
// 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"`
3325
}
3426

3527
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"`
3830
}
3931

4032
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"`
4840
}
4941

5042
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"`
5345
}
5446

5547
type DeployContext struct {
5648
BuildConfig `yaml:",inline"`
5749
}
5850

5951
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"`
6759
}
6860

6961
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"`
7365
}
7466

7567
type FoundNoConfigPathError struct {
7668
base string
77-
checked []string
69+
checked string
7870
}
7971

8072
func (f *FoundNoConfigPathError) Error() string {
8173
return fmt.Sprintf("No Netlify configuration file found.")
8274
}
8375

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)
9378

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}
10483
}
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...)
11784
}
11885

11986
func Load() (*NetlifyToml, error) {
@@ -136,29 +103,12 @@ func LoadFrom(paths ...string) (*NetlifyToml, error) {
136103
out := new(NetlifyToml)
137104

138105
for _, p := range paths {
139-
extension := filepath.Ext(p)
140-
141106
if data, ferr := ioutil.ReadFile(p); !os.IsNotExist(ferr) {
142107
if ferr != nil {
143108
return nil, errors.Wrapf(ferr, "Error while reading in file %s", p)
144109
}
145110

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 {
162112
return nil, errors.Wrapf(derr, "Error while decoding file %s", p)
163113
}
164114

ntoml/netlify_toml_test.go

Lines changed: 10 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -46,70 +46,6 @@ func TestLoadingExampleToml(t *testing.T) {
4646
assert.Equal(t, expected, conf)
4747
}
4848

49-
func TestLoadingExampleJSON(t *testing.T) {
50-
tmp := testJSON(t)
51-
defer os.Remove(t.Name())
52-
53-
conf, err := LoadFrom(tmp.Name())
54-
require.NoError(t, err)
55-
56-
expected := &NetlifyToml{
57-
Settings: Settings{
58-
ID: "this-is-a-site",
59-
Path: ".",
60-
},
61-
Redirects: []Redirect{
62-
{Origin: "/other", Destination: "/otherpage.html", Force: true},
63-
},
64-
Build: &BuildConfig{
65-
Command: "echo 'not a thing'",
66-
},
67-
Context: map[string]DeployContext{
68-
"deploy-preview": {BuildConfig{
69-
Command: "hugo version && npm run build-preview",
70-
}},
71-
"branch-deploy": {BuildConfig{
72-
Command: "hugo version && npm run build-branch",
73-
Environment: map[string]string{"HUGO_VERSION": "0.20.5"},
74-
}},
75-
},
76-
}
77-
78-
assert.Equal(t, expected, conf)
79-
}
80-
81-
func TestLoadingExampleYAML(t *testing.T) {
82-
tmp := testYAML(t)
83-
defer os.Remove(t.Name())
84-
85-
conf, err := LoadFrom(tmp.Name())
86-
require.NoError(t, err)
87-
88-
expected := &NetlifyToml{
89-
Settings: Settings{
90-
ID: "this-is-a-site",
91-
Path: ".",
92-
},
93-
Redirects: []Redirect{
94-
{Origin: "/other", Destination: "/otherpage.html", Force: true},
95-
},
96-
Build: &BuildConfig{
97-
Command: "echo 'not a thing'",
98-
},
99-
Context: map[string]DeployContext{
100-
"deploy-preview": {BuildConfig{
101-
Command: "hugo version && npm run build-preview",
102-
}},
103-
"branch-deploy": {BuildConfig{
104-
Command: "hugo version && npm run build-branch",
105-
Environment: map[string]string{"HUGO_VERSION": "0.20.5"},
106-
}},
107-
},
108-
}
109-
110-
assert.Equal(t, expected, conf)
111-
}
112-
11349
func TestSaveTomlFile(t *testing.T) {
11450
conf := &NetlifyToml{
11551
Settings: Settings{ID: "This is something", Path: "/dist"},
@@ -162,84 +98,21 @@ func testToml(t *testing.T) *os.File {
16298
return tmp
16399
}
164100

165-
func testJSON(t *testing.T) *os.File {
166-
tmp, err := ioutil.TempFile("", "netlify-ctl-*.json")
167-
require.NoError(t, err)
168-
169-
data := `
170-
{
171-
"settings": {
172-
"id": "this-is-a-site",
173-
"path": "."
174-
},
175-
"redirects": [
176-
{
177-
"origin": "/other",
178-
"destination": "/otherpage.html",
179-
"force": true
180-
}
181-
],
182-
"build": {
183-
"command": "echo 'not a thing'"
184-
},
185-
"context": {
186-
"deploy-preview": {
187-
"command": "hugo version && npm run build-preview"
188-
},
189-
"branch-deploy": {
190-
"command": "hugo version && npm run build-branch",
191-
"environment": {
192-
"HUGO_VERSION": "0.20.5"
193-
}
194-
}
195-
}
196-
}
197-
`
101+
func TestGetNetlifyConfigPath(t *testing.T) {
102+
// netlify.toml file does not exist
103+
_, err := GetNetlifyConfigPath("")
198104

199-
require.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(data), 0664))
200-
201-
return tmp
202-
}
105+
assert.IsType(t, &FoundNoConfigPathError{}, err)
203106

204-
func testYAML(t *testing.T) *os.File {
205-
tmp, err := ioutil.TempFile("", "netlify-ctl-*.yaml")
107+
// netlify.toml file exists
108+
err = ioutil.WriteFile("netlify.toml", []byte(``), 0664)
206109
require.NoError(t, err)
207110

208-
data := `
209-
settings:
210-
id: "this-is-a-site"
211-
path: "."
212-
213-
redirects:
214-
- origin: "/other"
215-
destination: "/otherpage.html"
216-
force: true
217-
218-
build:
219-
command: "echo 'not a thing'"
220-
221-
context:
222-
deploy-preview:
223-
command: "hugo version && npm run build-preview"
224-
branch-deploy:
225-
command: "hugo version && npm run build-branch"
226-
environment:
227-
HUGO_VERSION: "0.20.5"
228-
`
229-
230-
require.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(data), 0664))
231-
232-
return tmp
233-
}
111+
path, err := GetNetlifyConfigPath("")
234112

235-
func TestFindOnlyOneExistingPath(t *testing.T) {
236-
_, err := findOnlyOneExistingPath("", "does-not-exist")
237-
assert.IsType(t, &FoundNoConfigPathError{}, err)
113+
assert.NoError(t, err)
114+
assert.Equal(t, "netlify.toml", path)
238115

239-
tmp, err := ioutil.TempFile("", "netlify-*.yaml")
240-
require.NoError(t, err)
241-
defer os.Remove(tmp.Name())
242-
path, err := findOnlyOneExistingPath("", tmp.Name())
116+
err = os.Remove("netlify.toml")
243117
assert.NoError(t, err)
244-
assert.Equal(t, tmp.Name(), path)
245118
}

0 commit comments

Comments
 (0)