defaults.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. //go:generate go run defaults.go default.json defs.go
  17. package main //build !none
  18. import (
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "os/exec"
  24. "path/filepath"
  25. "strings"
  26. )
  27. func fatal(str string, v ...interface{}) {
  28. fmt.Fprintf(os.Stderr, str, v...)
  29. os.Exit(1)
  30. }
  31. type setting struct {
  32. Value int64 `json:"v"`
  33. Comment string `json:"d"`
  34. }
  35. func main() {
  36. if len(os.Args) < 3 {
  37. fatal("usage %s <input> <output>\n", os.Args[0])
  38. }
  39. content, err := ioutil.ReadFile(os.Args[1])
  40. if err != nil {
  41. fatal("error reading file %v\n", err)
  42. }
  43. m := make(map[string]setting)
  44. json.Unmarshal(content, &m)
  45. filepath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "params", os.Args[2])
  46. output, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, os.ModePerm /*0777*/)
  47. if err != nil {
  48. fatal("error opening file for writing %v\n", err)
  49. }
  50. output.WriteString(`// DO NOT EDIT!!!
  51. // AUTOGENERATED FROM generators/defaults.go
  52. package params
  53. import "math/big"
  54. var (
  55. `)
  56. for name, setting := range m {
  57. output.WriteString(fmt.Sprintf("%s=big.NewInt(%d) // %s\n", strings.Title(name), setting.Value, setting.Comment))
  58. }
  59. output.WriteString(")\n")
  60. output.Close()
  61. cmd := exec.Command("gofmt", "-w", filepath)
  62. if err := cmd.Run(); err != nil {
  63. fatal("gofmt failed: %v\n", err)
  64. }
  65. }