go-ethereum/cmd/utils/customflags.go

156 lines
3.7 KiB
Go
Raw Normal View History

2015-07-07 03:54:22 +03:00
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 03:54:22 +03:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
2015-07-07 03:54:22 +03:00
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"
2015-04-09 11:26:26 +03:00
"strings"
"gopkg.in/urfave/cli.v1"
)
// 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
}
2015-04-09 21:06:27 +03:00
func (self *DirectoryString) String() string {
2015-04-09 11:26:26 +03:00
return self.Value
}
2015-04-09 21:06:27 +03:00
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) {
2015-04-09 21:06:27 +03:00
set.Var(&self.Value, self.Name, self.Usage)
2015-04-09 11:26:26 +03:00
})
}
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 home := homeDir(); home != "" {
p = home + p[1:]
2015-04-09 11:26:26 +03:00
}
}
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 ""
}