Skip to content

Commit 32eee0a

Browse files
authored
Merge pull request #16 from netlify/add-netlify-toml
create a toml package
2 parents 3a6b779 + 38e0b9b commit 32eee0a

File tree

4 files changed

+210
-2
lines changed

4 files changed

+210
-2
lines changed

glide.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import:
2121
version: v1.0.0
2222
- package: github.com/streadway/amqp
2323
- package: gopkg.in/mgo.v2
24+
- package: github.com/BurntSushi/toml
25+
version: v0.3.0
2426
testImport:
2527
- package: github.com/nats-io/gnatsd
2628
version: v0.9.6

ntoml/netlify_toml.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package ntoml
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
"strings"
9+
10+
"github.com/BurntSushi/toml"
11+
"github.com/pkg/errors"
12+
)
13+
14+
const DefaultFilename = "netlify.toml"
15+
16+
type NetlifyToml struct {
17+
Settings Settings `toml:"settings"`
18+
19+
Redirects []Redirect `toml:"redirects, omitempty"`
20+
21+
// this is the default context
22+
Build *BuildConfig `toml:"build"`
23+
Context map[string]DeployContext `toml:"context, omitempty"`
24+
}
25+
26+
type Settings struct {
27+
ID string `toml:"id"`
28+
Path string `toml:"path"`
29+
}
30+
31+
type BuildConfig struct {
32+
Command string `toml:"command"`
33+
Base string `toml:"base"`
34+
Publish string `toml:"publish"`
35+
Environment map[string]string `toml:"environment"`
36+
}
37+
38+
type DeployContext struct {
39+
BuildConfig
40+
}
41+
42+
type Redirect struct {
43+
Origin string `toml:"origin"`
44+
Destination string `toml:"destination"`
45+
Parmeters map[string]string `toml:"parameters"`
46+
Status int `toml:"status"`
47+
Force bool `toml:"force"`
48+
Conditions *RedirectCondition `toml:"conditions"`
49+
Headers map[string]string `toml:"headers"`
50+
}
51+
52+
type RedirectCondition struct {
53+
Language []string `toml:"language"`
54+
Country []string `toml:"country"`
55+
Role []string `toml:"role"`
56+
}
57+
58+
func Load() (*NetlifyToml, error) {
59+
wd, err := os.Getwd()
60+
if err != nil {
61+
return nil, err
62+
}
63+
return LoadFrom(path.Join(wd, DefaultFilename))
64+
}
65+
66+
func LoadFrom(paths ...string) (*NetlifyToml, error) {
67+
if len(paths) == 0 {
68+
return nil, errors.New("No paths specified")
69+
}
70+
71+
out := new(NetlifyToml)
72+
73+
for _, p := range paths {
74+
if data, ferr := ioutil.ReadFile(p); !os.IsNotExist(ferr) {
75+
if ferr != nil {
76+
return nil, errors.Wrapf(ferr, "Error while reading in file %s.", p)
77+
}
78+
79+
if _, derr := toml.Decode(string(data), out); derr != nil {
80+
return nil, errors.Wrapf(derr, "Error while decoding file %s", p)
81+
}
82+
83+
return out, nil
84+
}
85+
}
86+
return nil, fmt.Errorf("Failed to find toml file in %s", strings.Join(paths, ","))
87+
}
88+
89+
func Save(conf *NetlifyToml) error {
90+
wd, err := os.Getwd()
91+
if err != nil {
92+
return err
93+
}
94+
return SaveTo(conf, path.Join(wd, DefaultFilename))
95+
}
96+
97+
func SaveTo(conf *NetlifyToml, path string) error {
98+
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
99+
if err != nil {
100+
return errors.Wrapf(err, "Failed to open file %s", path)
101+
}
102+
103+
defer f.Close()
104+
105+
if err := toml.NewEncoder(f).Encode(conf); err != nil {
106+
return errors.Wrap(err, "Failed to encode the toml file")
107+
}
108+
109+
return nil
110+
}

ntoml/netlify_toml_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ntoml
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestLoadingExample(t *testing.T) {
13+
tmp := testToml(t)
14+
defer os.Remove(t.Name())
15+
16+
conf, err := LoadFrom(tmp.Name())
17+
require.NoError(t, err)
18+
19+
expected := &NetlifyToml{
20+
Settings: Settings{
21+
ID: "this-is-a-site",
22+
Path: ".",
23+
},
24+
Redirects: []Redirect{
25+
{Origin: "/other", Destination: "/otherpage.html", Force: true},
26+
},
27+
Build: &BuildConfig{
28+
Command: "echo 'not a thing'",
29+
},
30+
Context: map[string]DeployContext{
31+
"deploy-preview": {BuildConfig{
32+
Command: "hugo version && npm run build-preview",
33+
}},
34+
"branch-deploy": {BuildConfig{
35+
Command: "hugo version && npm run build-branch",
36+
Environment: map[string]string{"HUGO_VERSION": "0.20.5"},
37+
}},
38+
},
39+
}
40+
41+
assert.Equal(t, expected, conf)
42+
}
43+
44+
func TestSaveTomlFile(t *testing.T) {
45+
conf := &NetlifyToml{
46+
Settings: Settings{ID: "This is something", Path: "/dist"},
47+
}
48+
49+
tmp, err := ioutil.TempFile("", "netlify-ctl")
50+
require.NoError(t, err)
51+
52+
require.NoError(t, SaveTo(conf, tmp.Name()))
53+
54+
data, err := ioutil.ReadFile(tmp.Name())
55+
require.NoError(t, err)
56+
57+
expected := "[settings]\n" +
58+
" id = \"This is something\"\n" +
59+
" path = \"/dist\"\n"
60+
61+
assert.Equal(t, expected, string(data))
62+
}
63+
64+
func testToml(t *testing.T) *os.File {
65+
tmp, err := ioutil.TempFile("", "netlify-ctl")
66+
require.NoError(t, err)
67+
68+
data := `
69+
[Settings]
70+
id = "this-is-a-site"
71+
path = "."
72+
73+
[build]
74+
command = "echo 'not a thing'"
75+
76+
[[redirects]]
77+
origin = "/other"
78+
force = true
79+
destination = "/otherpage.html"
80+
81+
[context.deploy-preview]
82+
command = "hugo version && npm run build-preview"
83+
84+
85+
[context.branch-deploy]
86+
command = "hugo version && npm run build-branch"
87+
88+
[context.branch-deploy.environment]
89+
HUGO_VERSION = "0.20.5"
90+
`
91+
require.NoError(t, ioutil.WriteFile(tmp.Name(), []byte(data), 0664))
92+
93+
return tmp
94+
}

0 commit comments

Comments
 (0)