github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/flags.go (about)

     1  /*
     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  */
    17  /**
    18   * @authors
    19   * 	Jeffrey Wilcke <i@jev.io>
    20   */
    21  package main
    22  
    23  import (
    24  	"crypto/ecdsa"
    25  	"flag"
    26  	"fmt"
    27  	"log"
    28  	"os"
    29  	"os/user"
    30  	"path"
    31  	"path/filepath"
    32  	"runtime"
    33  
    34  	"bitbucket.org/kardianos/osext"
    35  	"github.com/jonasnick/go-ethereum/crypto"
    36  	"github.com/jonasnick/go-ethereum/logger"
    37  	"github.com/jonasnick/go-ethereum/p2p/nat"
    38  	"github.com/jonasnick/go-ethereum/vm"
    39  )
    40  
    41  var (
    42  	Identifier      string
    43  	KeyRing         string
    44  	KeyStore        string
    45  	StartRpc        bool
    46  	StartWebSockets bool
    47  	RpcPort         int
    48  	WsPort          int
    49  	OutboundPort    string
    50  	ShowGenesis     bool
    51  	AddPeer         string
    52  	MaxPeer         int
    53  	GenAddr         bool
    54  	BootNodes       string
    55  	NodeKey         *ecdsa.PrivateKey
    56  	NAT             nat.Interface
    57  	SecretFile      string
    58  	ExportDir       string
    59  	NonInteractive  bool
    60  	Datadir         string
    61  	LogFile         string
    62  	ConfigFile      string
    63  	DebugFile       string
    64  	LogLevel        int
    65  	VmType          int
    66  )
    67  
    68  // flags specific to gui client
    69  var AssetPath string
    70  
    71  //TODO: If we re-use the one defined in cmd.go the binary osx image crashes. If somebody finds out why we can dry this up.
    72  func defaultAssetPath() string {
    73  	var assetPath string
    74  	// If the current working directory is the go-ethereum dir
    75  	// assume a debug build and use the source directory as
    76  	// asset directory.
    77  	pwd, _ := os.Getwd()
    78  	if pwd == path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist") {
    79  		assetPath = path.Join(pwd, "assets")
    80  	} else {
    81  		switch runtime.GOOS {
    82  		case "darwin":
    83  			// Get Binary Directory
    84  			exedir, _ := osext.ExecutableFolder()
    85  			assetPath = filepath.Join(exedir, "../Resources")
    86  		case "linux":
    87  			assetPath = "/usr/share/mist"
    88  		case "windows":
    89  			assetPath = "./assets"
    90  		default:
    91  			assetPath = "."
    92  		}
    93  	}
    94  	return assetPath
    95  }
    96  func defaultDataDir() string {
    97  	usr, _ := user.Current()
    98  	return path.Join(usr.HomeDir, ".ethereum")
    99  }
   100  
   101  var defaultConfigFile = path.Join(defaultDataDir(), "conf.ini")
   102  
   103  func Init() {
   104  	// TODO: move common flag processing to cmd/utils
   105  	flag.Usage = func() {
   106  		fmt.Fprintf(os.Stderr, "%s [options] [filename]:\noptions precedence: default < config file < environment variables < command line\n", os.Args[0])
   107  		flag.PrintDefaults()
   108  	}
   109  
   110  	flag.IntVar(&VmType, "vm", 0, "Virtual Machine type: 0-1: standard, debug")
   111  	flag.StringVar(&Identifier, "id", "", "Custom client identifier")
   112  	flag.StringVar(&KeyRing, "keyring", "", "identifier for keyring to use")
   113  	flag.StringVar(&KeyStore, "keystore", "db", "system to store keyrings: db|file (db)")
   114  	flag.IntVar(&RpcPort, "rpcport", 8545, "port to start json-rpc server on")
   115  	flag.IntVar(&WsPort, "wsport", 40404, "port to start websocket rpc server on")
   116  	flag.BoolVar(&StartRpc, "rpc", true, "start rpc server")
   117  	flag.BoolVar(&StartWebSockets, "ws", false, "start websocket server")
   118  	flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)")
   119  	flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
   120  	flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
   121  	flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
   122  	flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)")
   123  	flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use")
   124  	flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
   125  	flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)")
   126  	flag.IntVar(&LogLevel, "loglevel", int(logger.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)")
   127  
   128  	flag.StringVar(&AssetPath, "asset_path", defaultAssetPath(), "absolute path to GUI assets directory")
   129  
   130  	// Network stuff
   131  	var (
   132  		nodeKeyFile = flag.String("nodekey", "", "network private key file")
   133  		nodeKeyHex  = flag.String("nodekeyhex", "", "network private key (for testing)")
   134  		natstr      = flag.String("nat", "any", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
   135  	)
   136  	flag.StringVar(&OutboundPort, "port", "30303", "listening port")
   137  	flag.StringVar(&BootNodes, "bootnodes", "", "space-separated node URLs for discovery bootstrap")
   138  	flag.IntVar(&MaxPeer, "maxpeer", 30, "maximum desired peers")
   139  
   140  	flag.Parse()
   141  
   142  	var err error
   143  	if NAT, err = nat.Parse(*natstr); err != nil {
   144  		log.Fatalf("-nat: %v", err)
   145  	}
   146  	switch {
   147  	case *nodeKeyFile != "" && *nodeKeyHex != "":
   148  		log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive")
   149  	case *nodeKeyFile != "":
   150  		if NodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
   151  			log.Fatalf("-nodekey: %v", err)
   152  		}
   153  	case *nodeKeyHex != "":
   154  		if NodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
   155  			log.Fatalf("-nodekeyhex: %v", err)
   156  		}
   157  	}
   158  
   159  	if VmType >= int(vm.MaxVmTy) {
   160  		log.Fatal("Invalid VM type ", VmType)
   161  	}
   162  }