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