33package main
44
55import (
6+ "archive/zip"
67 "bytes"
78 "compress/gzip"
89 ctx "context"
910 "encoding/json"
1011 "flag"
1112 "fmt"
13+ "io"
1214 "io/ioutil"
15+ "net/http"
1316 "os"
1417 "path/filepath"
1518 "sort"
@@ -32,7 +35,7 @@ import (
3235
3336func main () {
3437 flag .Usage = func () {
35- const usageFormat = "Usage: %s <languages> <schema-file > <root-dir> "
38+ const usageFormat = "Usage: %s <languages> <schema-folder > <version> (<schema urls>) "
3639 _ , err := fmt .Fprintf (flag .CommandLine .Output (), usageFormat , os .Args [0 ])
3740 contract .IgnoreError (err )
3841 flag .PrintDefaults ()
@@ -52,9 +55,18 @@ func main() {
5255 genDir := filepath .Join ("." , "provider" , "cmd" , "pulumi-gen-aws-native" )
5356
5457 if languages == "discovery" {
58+ if len (args ) < 4 {
59+ flag .Usage ()
60+ return
61+ }
62+ jsonSchemaUrls := strings .Split (args [3 ], "," )
63+
5564 if err := writeSupportedResourceTypes (genDir ); err != nil {
5665 panic (err )
5766 }
67+ if err := downloadCloudFormationSchemas (jsonSchemaUrls , filepath .Join ("." , schemaFolder )); err != nil {
68+ panic (err )
69+ }
5870 return
5971 }
6072
@@ -181,6 +193,51 @@ func writeSupportedResourceTypes(outDir string) error {
181193 return emitFile (outDir , supportedResourcesFile , []byte (val ))
182194}
183195
196+ func downloadCloudFormationSchemas (urls []string , outDir string ) error {
197+ for _ , url := range urls {
198+ resp , err := http .Get (url )
199+ if err != nil {
200+ return err
201+ }
202+ defer resp .Body .Close ()
203+
204+ body , err := ioutil .ReadAll (resp .Body )
205+ if err != nil {
206+ return err
207+ }
208+
209+ zipReader , err := zip .NewReader (bytes .NewReader (body ), int64 (len (body )))
210+ if err != nil {
211+ return err
212+ }
213+
214+ // Read all the files from zip archive
215+ for _ , f := range zipReader .File {
216+ outPath := filepath .Join (outDir , f .Name )
217+
218+ outFile , err := os .OpenFile (outPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , f .Mode ())
219+ if err != nil {
220+ return err
221+ }
222+
223+ rc , err := f .Open ()
224+ if err != nil {
225+ return err
226+ }
227+
228+ _ , err = io .Copy (outFile , rc )
229+ if err != nil {
230+ return err
231+ }
232+
233+ outFile .Close ()
234+ rc .Close ()
235+ }
236+ }
237+
238+ return nil
239+ }
240+
184241func generate (ppkg * pschema.Package , language string ) (map [string ][]byte , error ) {
185242 toolDescription := "the Pulumi SDK Generator"
186243 extraFiles := map [string ][]byte {}
0 commit comments