-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetlify.go
79 lines (71 loc) · 2 KB
/
netlify.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func (cmd *generateCmd) generateNetlify(ctx context.Context, rd registryData) error {
cmd.ui.Info("\t[netlify] writing service discovery file...")
err := writeJSONFile(filepath.Join(cmd.outputDir, ".well-known/terraform.json"), wellKnownTerraform{
ProvidersV1: "/providers/v1/",
})
if err != nil {
return fmt.Errorf("unable to write service discovery file: %w", err)
}
cmd.ui.Info("\t[netlify] writing provider version files")
for k, v := range rd.ProviderVersions {
err = writeJSONFile(filepath.Join(
cmd.outputDir,
"providers/v1",
strings.ToLower(k.Namespace), strings.ToLower(k.Name),
"versions.json",
), v)
if err != nil {
return err
}
}
cmd.ui.Info("\t[netlify] writing provider version download files...")
for k, v := range rd.Downloads {
err = writeJSONFile(filepath.Join(
cmd.outputDir,
"providers/v1",
strings.ToLower(k.Namespace), strings.ToLower(k.Name),
fmt.Sprintf("%s-%s-%s.json", k.Version, k.OS, k.Arch),
), v)
}
cmd.ui.Info("\t[netlify] writing redirects file...")
err = ioutil.WriteFile(
filepath.Join(
cmd.outputDir,
"_redirects",
),
[]byte(`
# redirect the individual version requests
/providers/v1/:namespace/:name/:version/download/:os/:arch /providers/v1/:namespace/:name/:version-:os-:arch.json 200
# redirect the versions list request
/providers/v1/:namespace/:name/versions /providers/v1/:namespace/:name/versions.json 200
`),
0644,
)
return nil
}
func writeJSONFile(file string, data interface{}) error {
bytes, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return fmt.Errorf("unable to marshal JSON to write to file %q: %w", file, err)
}
dir := filepath.Dir(file)
err = os.MkdirAll(dir, 0644)
if err != nil {
return fmt.Errorf("unable to make directory %q: %w", dir, err)
}
err = ioutil.WriteFile(file, bytes, 0644)
if err != nil {
return fmt.Errorf("unable to write file %q: %w", file, err)
}
return nil
}