go-ethereum/cmd/utils/customflags.go

134 lines
2.9 KiB
Go
Raw Normal View History

package utils
2015-04-09 11:26:26 +03:00
import (
2015-04-09 11:26:26 +03:00
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
)
// 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 {
2015-04-09 11:26:26 +03:00
Value string
}
func (self DirectoryString) String() string {
2015-04-09 11:26:26 +03:00
return self.Value
}
func (self DirectoryString) Set(value string) error {
2015-04-09 11:26:26 +03:00
self.Value = expandPath(value)
return nil
}
// Custom cli.Flag type which expand the received string to an absolute path.
// e.g. ~/.ethereum -> /home/username/.ethereum
type DirectoryFlag struct {
2015-04-09 11:26:26 +03:00
cli.GenericFlag
Name string
Value DirectoryString
Usage string
EnvVar string
}
func (self DirectoryFlag) String() string {
2015-04-09 11:26:26 +03:00
var fmtString string
fmtString = "%s %v\t%v"
2015-04-09 11:26:26 +03:00
if len(self.Value.Value) > 0 {
fmtString = "%s \"%v\"\t%v"
} else {
fmtString = "%s %v\t%v"
}
2015-04-09 11:26:26 +03:00
return withEnvHint(self.EnvVar, fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage))
}
func eachName(longName string, fn func(string)) {
2015-04-09 11:26:26 +03:00
parts := strings.Split(longName, ",")
for _, name := range parts {
name = strings.Trim(name, " ")
fn(name)
}
}
2015-04-09 11:26:26 +03:00
// 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) {
2015-04-09 11:26:26 +03:00
if self.EnvVar != "" {
for _, envVar := range strings.Split(self.EnvVar, ",") {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" {
self.Value.Value = envVal
break
}
}
}
eachName(self.Name, func(name string) {
set.Var(self.Value, self.Name, "a: "+self.Usage)
})
}
func prefixFor(name string) (prefix string) {
2015-04-09 11:26:26 +03:00
if len(name) == 1 {
prefix = "-"
} else {
prefix = "--"
}
2015-04-09 11:26:26 +03:00
return
}
func prefixedNames(fullName string) (prefixed string) {
2015-04-09 11:26:26 +03:00
parts := strings.Split(fullName, ",")
for i, name := range parts {
name = strings.Trim(name, " ")
prefixed += prefixFor(name) + name
if i < len(parts)-1 {
prefixed += ", "
}
}
return
}
func withEnvHint(envVar, str string) string {
2015-04-09 11:26:26 +03:00
envText := ""
if envVar != "" {
envText = fmt.Sprintf(" [$%s]", strings.Join(strings.Split(envVar, ","), ", $"))
}
return str + envText
}
func (self DirectoryFlag) getName() string {
2015-04-09 11:26:26 +03:00
return self.Name
}
func (self *DirectoryFlag) Set(value string) {
2015-04-09 11:26:26 +03:00
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 {
2015-04-09 11:26:26 +03:00
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if user, err := user.Current(); err == nil {
if err == nil {
p = strings.Replace(p, "~", user.HomeDir, 1)
}
}
}
return filepath.Clean(os.ExpandEnv(p))
}