github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/siad/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/NebulousLabs/Sia/build"
    10  )
    11  
    12  var (
    13  	// globalConfig is used by the cobra package to fill out the configuration
    14  	// variables.
    15  	globalConfig Config
    16  )
    17  
    18  // exit codes
    19  // inspired by sysexits.h
    20  const (
    21  	exitCodeGeneral = 1  // Not in sysexits.h, but is standard practice.
    22  	exitCodeUsage   = 64 // EX_USAGE in sysexits.h
    23  )
    24  
    25  // The Config struct contains all configurable variables for siad. It is
    26  // compatible with gcfg.
    27  type Config struct {
    28  	Siad struct {
    29  		APIaddr  string
    30  		RPCaddr  string
    31  		HostAddr string
    32  
    33  		Modules           string
    34  		NoBootstrap       bool
    35  		RequiredUserAgent string
    36  
    37  		Profile    bool
    38  		ProfileDir string
    39  		SiaDir     string
    40  	}
    41  }
    42  
    43  // die prints its arguments to stderr, then exits the program with the default
    44  // error code.
    45  func die(args ...interface{}) {
    46  	fmt.Fprintln(os.Stderr, args...)
    47  	os.Exit(exitCodeGeneral)
    48  }
    49  
    50  // versionCmd is a cobra command that prints the version of siad.
    51  func versionCmd(*cobra.Command, []string) {
    52  	switch build.Release {
    53  	case "dev":
    54  		fmt.Println("Sia Daemon v" + build.Version + "-dev")
    55  	case "standard":
    56  		fmt.Println("Sia Daemon v" + build.Version)
    57  	case "testing":
    58  		fmt.Println("Sia Daemon v" + build.Version + "-testing")
    59  	default:
    60  		fmt.Println("Sia Daemon v" + build.Version + "-???")
    61  	}
    62  }
    63  
    64  // modulesCmd is a cobra command that prints help info about modules.
    65  func modulesCmd(*cobra.Command, []string) {
    66  	fmt.Println(`Use the -M or --modules flag to only run specific modules. Modules are
    67  independent components of Sia. This flag should only be used by developers or
    68  people who want to reduce overhead from unused modules. Modules are specified by
    69  their first letter. If the -M or --modules flag is not specified the default
    70  modules are run. The default modules are:
    71  	gateway, consensus set, host, miner, renter, transaction pool, wallet
    72  This is equivalent to:
    73  	siad -M cghmrtw
    74  Below is a list of all the modules available.
    75  
    76  Gateway (g):
    77  	The gateway maintains a peer to peer connection to the network and
    78  	enables other modules to perform RPC calls on peers.
    79  	The gateway is required by all other modules.
    80  	Example:
    81  		siad -M g
    82  Consensus Set (c):
    83  	The consensus set manages everything related to consensus and keeps the
    84  	blockchain in sync with the rest of the network.
    85  	The consensus set requires the gateway.
    86  	Example:
    87  		siad -M gc
    88  Transaction Pool (t):
    89  	The transaction pool manages unconfirmed transactions.
    90  	The transaction pool requires the consensus set.
    91  	Example:
    92  		siad -M gct
    93  Wallet (w):
    94  	The wallet stores and manages siacoins and siafunds.
    95  	The wallet requires the consensus set and transaction pool.
    96  	Example:
    97  		siad -M gctw
    98  Renter (r):
    99  	The renter manages the user's files on the network.
   100  	The renter requires the consensus set, transaction pool, and wallet.
   101  	Example:
   102  		siad -M gctwr
   103  Host (h):
   104  	The host provides storage from local disks to the network. The host
   105  	negotiates file contracts with remote renters to earn money for storing
   106  	other users' files.
   107  	The host requires the consensus set, transaction pool, and wallet.
   108  	Example:
   109  		siad -M gctwh
   110  Miner (m):
   111  	The miner provides a basic CPU mining implementation as well as an API
   112  	for external miners to use.
   113  	The miner requires the consensus set, transaction pool, and wallet.
   114  	Example:
   115  		siad -M gctwm
   116  Explorer (e):
   117  	The explorer provides statistics about the blockchain and can be
   118  	queried for information about specific transactions or other objects on
   119  	the blockchain.
   120  	The explorer requires the consenus set.
   121  	Example:
   122  		siad -M gce`)
   123  }
   124  
   125  // main establishes a set of commands and flags using the cobra package.
   126  func main() {
   127  	root := &cobra.Command{
   128  		Use:   os.Args[0],
   129  		Short: "Sia Daemon v" + build.Version,
   130  		Long:  "Sia Daemon v" + build.Version,
   131  		Run:   startDaemonCmd,
   132  	}
   133  
   134  	root.AddCommand(&cobra.Command{
   135  		Use:   "version",
   136  		Short: "Print version information",
   137  		Long:  "Print version information about the Sia Daemon",
   138  		Run:   versionCmd,
   139  	})
   140  
   141  	root.AddCommand(&cobra.Command{
   142  		Use:   "modules",
   143  		Short: "List available modules for use with -M, --modules flag",
   144  		Long:  "List available modules for use with -M, --modules flag and their uses",
   145  		Run:   modulesCmd,
   146  	})
   147  
   148  	// Set default values, which have the lowest priority.
   149  	root.Flags().StringVarP(&globalConfig.Siad.RequiredUserAgent, "agent", "A", "Sia-Agent", "required substring for the user agent")
   150  	root.Flags().StringVarP(&globalConfig.Siad.HostAddr, "host-addr", "H", ":9982", "which port the host listens on")
   151  	root.Flags().StringVarP(&globalConfig.Siad.ProfileDir, "profile-directory", "P", "profiles", "location of the profiling directory")
   152  	root.Flags().StringVarP(&globalConfig.Siad.APIaddr, "api-addr", "a", "localhost:9980", "which host:port the API server listens on")
   153  	root.Flags().StringVarP(&globalConfig.Siad.SiaDir, "sia-directory", "d", "", "location of the sia directory")
   154  	root.Flags().BoolVarP(&globalConfig.Siad.NoBootstrap, "no-bootstrap", "n", false, "disable bootstrapping on this run")
   155  	root.Flags().BoolVarP(&globalConfig.Siad.Profile, "profile", "p", false, "enable profiling")
   156  	root.Flags().StringVarP(&globalConfig.Siad.RPCaddr, "rpc-addr", "r", ":9981", "which port the gateway listens on")
   157  	root.Flags().StringVarP(&globalConfig.Siad.Modules, "modules", "M", "cghmrtw", "enabled modules, see 'siad modules' for more info")
   158  
   159  	// Deprecate shorthand flags that aren't commonly used.
   160  	// COMPATv0.5.2
   161  	// TODO: remove shorthands for these flags by supplying a blank shorthand in flag construction above.
   162  	root.Flags().MarkShorthandDeprecated("agent", "please use --agent instead")
   163  	root.Flags().MarkShorthandDeprecated("host-addr", "please use --host-addr instead")
   164  	root.Flags().MarkShorthandDeprecated("profile-directory", "please use --profile-directory instead")
   165  	root.Flags().MarkShorthandDeprecated("api-addr", "please use --api-addr instead")
   166  	root.Flags().MarkShorthandDeprecated("no-bootstrap", "please use --no-bootstrap instead")
   167  	root.Flags().MarkShorthandDeprecated("profile", "please use --profile instead")
   168  	root.Flags().MarkShorthandDeprecated("rpc-addr", "please use --rpc-addr instead")
   169  
   170  	// Parse cmdline flags, overwriting both the default values and the config
   171  	// file values.
   172  	if err := root.Execute(); err != nil {
   173  		// Since no commands return errors (all commands set Command.Run instead of
   174  		// Command.RunE), Command.Execute() should only return an error on an
   175  		// invalid command or flag. Therefore Command.Usage() was called (assuming
   176  		// Command.SilenceUsage is false) and we should exit with exitCodeUsage.
   177  		os.Exit(exitCodeUsage)
   178  	}
   179  }