github.com/prysmaticlabs/prysm@v1.4.4/tools/specs-checker/download.go (about) 1 package main 2 3 import ( 4 _ "embed" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "os" 9 "path" 10 "regexp" 11 12 "github.com/urfave/cli/v2" 13 ) 14 15 const baseUrl = "https://raw.githubusercontent.com/ethereum/eth2.0-specs/dev" 16 17 // Regex to find Python's code snippets in markdown. 18 var reg2 = regexp.MustCompile(`(?msU)^\x60\x60\x60python\n+def\s(.*)^\x60\x60\x60`) 19 20 func download(cliCtx *cli.Context) error { 21 fmt.Print("Downloading specs:\n") 22 baseDir := cliCtx.String(dirFlag.Name) 23 for dirName, fileNames := range specDirs { 24 if err := prepareDir(path.Join(baseDir, dirName)); err != nil { 25 return err 26 } 27 for _, fileName := range fileNames { 28 outFilePath := path.Join(baseDir, dirName, fileName) 29 specDocUrl := fmt.Sprintf("%s/%s", baseUrl, fmt.Sprintf("%s/%s", dirName, fileName)) 30 fmt.Printf("- %s\n", specDocUrl) 31 if err := getAndSaveFile(specDocUrl, outFilePath); err != nil { 32 return err 33 } 34 } 35 } 36 37 return nil 38 } 39 40 func getAndSaveFile(specDocUrl, outFilePath string) error { 41 // Create output file. 42 f, err := os.Create(outFilePath) 43 if err != nil { 44 return fmt.Errorf("cannot create output file: %w", err) 45 } 46 defer func() { 47 if err := f.Close(); err != nil { 48 fmt.Printf("cannot close output file: %v", err) 49 } 50 }() 51 52 // Download spec doc. 53 resp, err := http.Get(specDocUrl) 54 if err != nil { 55 return err 56 } 57 defer func() { 58 if err := resp.Body.Close(); err != nil { 59 fmt.Printf("cannot close spec doc file: %v", err) 60 } 61 }() 62 63 // Transform and save spec docs. 64 specDoc, err := ioutil.ReadAll(resp.Body) 65 if err != nil { 66 return err 67 } 68 specDocString := string(specDoc) 69 for _, snippet := range reg2.FindAllString(specDocString, -1) { 70 if _, err = f.WriteString(snippet + "\n"); err != nil { 71 return err 72 } 73 } 74 75 return nil 76 } 77 78 func prepareDir(dirPath string) error { 79 return os.MkdirAll(dirPath, os.ModePerm) 80 }