customflags.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "flag"
  19. "fmt"
  20. "os"
  21. "os/user"
  22. "path"
  23. "strings"
  24. )
  25. // Custom type which is registered in the flags library which cli uses for
  26. // argument parsing. This allows us to expand Value to an absolute path when
  27. // the argument is parsed
  28. type DirectoryString struct {
  29. Value string
  30. }
  31. func (self *DirectoryString) String() string {
  32. return self.Value
  33. }
  34. func (self *DirectoryString) Set(value string) error {
  35. self.Value = expandPath(value)
  36. return nil
  37. }
  38. // Custom cli.Flag type which expand the received string to an absolute path.
  39. // e.g. ~/.ethereum -> /home/username/.ethereum
  40. type DirectoryFlag struct {
  41. Name string
  42. Value DirectoryString
  43. Usage string
  44. EnvVar string
  45. }
  46. func (self DirectoryFlag) String() string {
  47. fmtString := "%s %v\t%v"
  48. if len(self.Value.Value) > 0 {
  49. fmtString = "%s \"%v\"\t%v"
  50. }
  51. return withEnvHint(self.EnvVar, fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage))
  52. }
  53. func eachName(longName string, fn func(string)) {
  54. parts := strings.Split(longName, ",")
  55. for _, name := range parts {
  56. name = strings.Trim(name, " ")
  57. fn(name)
  58. }
  59. }
  60. // called by cli library, grabs variable from environment (if in env)
  61. // and adds variable to flag set for parsing.
  62. func (self DirectoryFlag) Apply(set *flag.FlagSet) {
  63. if self.EnvVar != "" {
  64. for _, envVar := range strings.Split(self.EnvVar, ",") {
  65. envVar = strings.TrimSpace(envVar)
  66. if envVal := os.Getenv(envVar); envVal != "" {
  67. self.Value.Value = envVal
  68. break
  69. }
  70. }
  71. }
  72. eachName(self.Name, func(name string) {
  73. set.Var(&self.Value, self.Name, self.Usage)
  74. })
  75. }
  76. func prefixFor(name string) (prefix string) {
  77. if len(name) == 1 {
  78. prefix = "-"
  79. } else {
  80. prefix = "--"
  81. }
  82. return
  83. }
  84. func prefixedNames(fullName string) (prefixed string) {
  85. parts := strings.Split(fullName, ",")
  86. for i, name := range parts {
  87. name = strings.Trim(name, " ")
  88. prefixed += prefixFor(name) + name
  89. if i < len(parts)-1 {
  90. prefixed += ", "
  91. }
  92. }
  93. return
  94. }
  95. func withEnvHint(envVar, str string) string {
  96. envText := ""
  97. if envVar != "" {
  98. envText = fmt.Sprintf(" [$%s]", strings.Join(strings.Split(envVar, ","), ", $"))
  99. }
  100. return str + envText
  101. }
  102. func (self DirectoryFlag) GetName() string {
  103. return self.Name
  104. }
  105. func (self *DirectoryFlag) Set(value string) {
  106. self.Value.Value = value
  107. }
  108. // Expands a file path
  109. // 1. replace tilde with users home dir
  110. // 2. expands embedded environment variables
  111. // 3. cleans the path, e.g. /a/b/../c -> /a/c
  112. // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
  113. func expandPath(p string) string {
  114. if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
  115. if home := homeDir(); home != "" {
  116. p = home + p[1:]
  117. }
  118. }
  119. return path.Clean(os.ExpandEnv(p))
  120. }
  121. func homeDir() string {
  122. if home := os.Getenv("HOME"); home != "" {
  123. return home
  124. }
  125. if usr, err := user.Current(); err == nil {
  126. return usr.HomeDir
  127. }
  128. return ""
  129. }