customflags.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package utils
  17. import (
  18. "encoding"
  19. "errors"
  20. "flag"
  21. "math/big"
  22. "os"
  23. "os/user"
  24. "path"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. "gopkg.in/urfave/cli.v1"
  28. )
  29. // Custom type which is registered in the flags library which cli uses for
  30. // argument parsing. This allows us to expand Value to an absolute path when
  31. // the argument is parsed
  32. type DirectoryString string
  33. func (s *DirectoryString) String() string {
  34. return string(*s)
  35. }
  36. func (s *DirectoryString) Set(value string) error {
  37. *s = DirectoryString(expandPath(value))
  38. return nil
  39. }
  40. // Custom cli.Flag type which expand the received string to an absolute path.
  41. // e.g. ~/.ethereum -> /home/username/.ethereum
  42. type DirectoryFlag struct {
  43. Name string
  44. Value DirectoryString
  45. Usage string
  46. EnvVar string
  47. }
  48. func (f DirectoryFlag) String() string {
  49. return cli.FlagStringer(f)
  50. }
  51. // called by cli library, grabs variable from environment (if in env)
  52. // and adds variable to flag set for parsing.
  53. func (f DirectoryFlag) Apply(set *flag.FlagSet) {
  54. eachName(f.Name, func(name string) {
  55. set.Var(&f.Value, f.Name, f.Usage)
  56. })
  57. }
  58. func (f DirectoryFlag) GetName() string {
  59. return f.Name
  60. }
  61. func (f *DirectoryFlag) Set(value string) {
  62. f.Value.Set(value)
  63. }
  64. func eachName(longName string, fn func(string)) {
  65. parts := strings.Split(longName, ",")
  66. for _, name := range parts {
  67. name = strings.Trim(name, " ")
  68. fn(name)
  69. }
  70. }
  71. type TextMarshaler interface {
  72. encoding.TextMarshaler
  73. encoding.TextUnmarshaler
  74. }
  75. // textMarshalerVal turns a TextMarshaler into a flag.Value
  76. type textMarshalerVal struct {
  77. v TextMarshaler
  78. }
  79. func (v textMarshalerVal) String() string {
  80. if v.v == nil {
  81. return ""
  82. }
  83. text, _ := v.v.MarshalText()
  84. return string(text)
  85. }
  86. func (v textMarshalerVal) Set(s string) error {
  87. return v.v.UnmarshalText([]byte(s))
  88. }
  89. // TextMarshalerFlag wraps a TextMarshaler value.
  90. type TextMarshalerFlag struct {
  91. Name string
  92. Value TextMarshaler
  93. Usage string
  94. EnvVar string
  95. }
  96. func (f TextMarshalerFlag) GetName() string {
  97. return f.Name
  98. }
  99. func (f TextMarshalerFlag) String() string {
  100. return cli.FlagStringer(f)
  101. }
  102. func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
  103. eachName(f.Name, func(name string) {
  104. set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
  105. })
  106. }
  107. // GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
  108. func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
  109. val := ctx.GlobalGeneric(name)
  110. if val == nil {
  111. return nil
  112. }
  113. return val.(textMarshalerVal).v
  114. }
  115. // BigFlag is a command line flag that accepts 256 bit big integers in decimal or
  116. // hexadecimal syntax.
  117. type BigFlag struct {
  118. Name string
  119. Value *big.Int
  120. Usage string
  121. EnvVar string
  122. }
  123. // bigValue turns *big.Int into a flag.Value
  124. type bigValue big.Int
  125. func (b *bigValue) String() string {
  126. if b == nil {
  127. return ""
  128. }
  129. return (*big.Int)(b).String()
  130. }
  131. func (b *bigValue) Set(s string) error {
  132. int, ok := math.ParseBig256(s)
  133. if !ok {
  134. return errors.New("invalid integer syntax")
  135. }
  136. *b = (bigValue)(*int)
  137. return nil
  138. }
  139. func (f BigFlag) GetName() string {
  140. return f.Name
  141. }
  142. func (f BigFlag) String() string {
  143. return cli.FlagStringer(f)
  144. }
  145. func (f BigFlag) Apply(set *flag.FlagSet) {
  146. eachName(f.Name, func(name string) {
  147. set.Var((*bigValue)(f.Value), f.Name, f.Usage)
  148. })
  149. }
  150. // GlobalBig returns the value of a BigFlag from the global flag set.
  151. func GlobalBig(ctx *cli.Context, name string) *big.Int {
  152. val := ctx.GlobalGeneric(name)
  153. if val == nil {
  154. return nil
  155. }
  156. return (*big.Int)(val.(*bigValue))
  157. }
  158. // Expands a file path
  159. // 1. replace tilde with users home dir
  160. // 2. expands embedded environment variables
  161. // 3. cleans the path, e.g. /a/b/../c -> /a/c
  162. // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
  163. func expandPath(p string) string {
  164. if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
  165. if home := HomeDir(); home != "" {
  166. p = home + p[1:]
  167. }
  168. }
  169. return path.Clean(os.ExpandEnv(p))
  170. }
  171. func HomeDir() string {
  172. if home := os.Getenv("HOME"); home != "" {
  173. return home
  174. }
  175. if usr, err := user.Current(); err == nil {
  176. return usr.HomeDir
  177. }
  178. return ""
  179. }