mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
4d5d229f0f
Former-commit-id: bbd5b46e7f64f762350d6fb496492207e70d7130 [formerly 43a37f7139b7d1d90f0c27a7406b63bdf390ad96] Former-commit-id: bb7a2ff0a7619f8de0bd38cd2c9eb0de7c189edb
112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
// Custom type which is registered in the flags library which cli uses for
|
|
// argument parsing. This allows us to expand Value to an absolute path when
|
|
// the argument is parsed
|
|
type DirectoryString struct {
|
|
Value string
|
|
}
|
|
|
|
func (self *DirectoryString) String() string {
|
|
return self.Value
|
|
}
|
|
|
|
func (self *DirectoryString) Set(value string) error {
|
|
self.Value = expandPath(value)
|
|
return nil
|
|
}
|
|
|
|
func prefixFor(name string) (prefix string) {
|
|
if len(name) == 1 {
|
|
prefix = "-"
|
|
} else {
|
|
prefix = "--"
|
|
}
|
|
return prefix
|
|
}
|
|
|
|
func prefixedNames(fullName string) (prefixed string) {
|
|
parts := strings.Split(fullName, ",")
|
|
for i, name := range parts {
|
|
name = strings.Trim(name, " ")
|
|
prefixed += prefixFor(name) + name
|
|
if i < len(parts)-1 {
|
|
prefixed += ", "
|
|
}
|
|
}
|
|
return prefixed
|
|
}
|
|
|
|
// Custom cli.Flag type which expand the received string to an absolute path.
|
|
// e.g. ~/.ethereum -> /home/username/.ethereum
|
|
type DirectoryFlag struct {
|
|
Name string
|
|
Value DirectoryString
|
|
Usage string
|
|
}
|
|
|
|
func (self DirectoryFlag) String() string {
|
|
fmtString := "%s %v\t%v"
|
|
if len(self.Value.Value) > 0 {
|
|
fmtString = "%s \"%v\"\t%v"
|
|
}
|
|
return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
|
|
}
|
|
|
|
func eachName(longName string, fn func(string)) {
|
|
parts := strings.Split(longName, ",")
|
|
for _, name := range parts {
|
|
name = strings.Trim(name, " ")
|
|
fn(name)
|
|
}
|
|
}
|
|
|
|
// called by cli library, grabs variable from environment (if in env)
|
|
// and adds variable to flag set for parsing.
|
|
func (self DirectoryFlag) Apply(set *flag.FlagSet) {
|
|
eachName(self.Name, func(name string) {
|
|
set.Var(&self.Value, self.Name, self.Usage)
|
|
})
|
|
}
|
|
|
|
func (self DirectoryFlag) GetName() string {
|
|
return self.Name
|
|
}
|
|
|
|
func (self *DirectoryFlag) Set(value string) {
|
|
self.Value.Value = value
|
|
}
|
|
|
|
// Expands a file path
|
|
// 1. replace tilde with users home dir
|
|
// 2. expands embedded environment variables
|
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
|
func expandPath(p string) string {
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
|
if home := homeDir(); home != "" {
|
|
p = home + p[1:]
|
|
}
|
|
}
|
|
return path.Clean(os.ExpandEnv(p))
|
|
}
|
|
|
|
func homeDir() string {
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
return home
|
|
}
|
|
if usr, err := user.Current(); err == nil {
|
|
return usr.HomeDir
|
|
}
|
|
return ""
|
|
}
|