github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/cmd/hcd/hcd.go (about)

     1  // Copyright (C) 2013-2018, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
     2  // Use of this source code is governed by GPLv3 found in the LICENSE file
     3  //---------------------------------------------------------------------------------------
     4  // command line interface to running holochain applications
     5  
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	holo "github.com/holochain/holochain-proto"
    11  	"github.com/holochain/holochain-proto/cmd"
    12  	"github.com/holochain/holochain-proto/ui"
    13  	"github.com/urfave/cli"
    14  	"os"
    15  )
    16  
    17  const (
    18  	defaultUIPort = "3141"
    19  )
    20  
    21  var debug bool
    22  var verbose bool
    23  
    24  func setupApp() (app *cli.App) {
    25  	app = cli.NewApp()
    26  	app.Name = "hcd"
    27  	app.Usage = fmt.Sprintf("serve a chain to the web on localhost:<ui-port> (defaults to %s)", defaultUIPort)
    28  	app.ArgsUsage = "holochain-name [ui-port]"
    29  
    30  	app.Version = fmt.Sprintf("0.0.4 (holochain %s)", holo.VersionStr)
    31  
    32  	var root string
    33  	var service *holo.Service
    34  
    35  	app.Flags = []cli.Flag{
    36  		cli.BoolFlag{
    37  			Name:        "debug",
    38  			Usage:       "debugging output",
    39  			Destination: &debug,
    40  		},
    41  		cli.StringFlag{
    42  			Name:        "path",
    43  			Usage:       "path to holochain directory (default: ~/.holochain)",
    44  			Destination: &root,
    45  		},
    46  		cli.BoolFlag{
    47  			Name:        "verbose, V",
    48  			Usage:       "verbose output",
    49  			Destination: &verbose,
    50  		},
    51  	}
    52  
    53  	app.Before = func(c *cli.Context) error {
    54  		// for hcd the -debug flag enables the app level debugging
    55  		if debug {
    56  			os.Setenv("HCLOG_APP_ENABLE", "1")
    57  		}
    58  		if verbose {
    59  			fmt.Printf("hcd version %s \n", app.Version)
    60  		}
    61  		var err error
    62  		root, err = cmd.GetHolochainRoot(root)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		service, err = cmd.GetService(root)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		return nil
    71  	}
    72  
    73  	app.Action = func(c *cli.Context) error {
    74  		args := len(c.Args())
    75  		if args == 1 || args == 2 {
    76  			h, err := cmd.GetHolochain(c.Args().First(), service, "serve")
    77  			if err != nil {
    78  				return err
    79  			}
    80  			if !h.Started() {
    81  				return fmt.Errorf("Can't serve an un-started chain!\n")
    82  			}
    83  
    84  			var port string
    85  			if len(c.Args()) == 1 {
    86  				port = defaultUIPort
    87  			} else {
    88  				port = c.Args()[1]
    89  			}
    90  			err = h.Activate()
    91  			if err != nil {
    92  				return err
    93  			}
    94  
    95  			h.StartBackgroundTasks()
    96  
    97  			fmt.Printf("Serving holochain with DNA hash:%v on port %s\n", h.DNAHash(), port)
    98  
    99  			ws := ui.NewWebServer(h, port)
   100  			ws.Start()
   101  			ws.Wait()
   102  			return err
   103  		} else if args == 0 {
   104  			fmt.Println(service.ListChains())
   105  		} else {
   106  			return fmt.Errorf("Expected single holochain-name argument with optional port argument.\n")
   107  		}
   108  		return nil
   109  	}
   110  	return
   111  }
   112  
   113  func main() {
   114  	app := setupApp()
   115  
   116  	err := app.Run(os.Args)
   117  	if err != nil {
   118  		fmt.Printf("Error: %v\n", err)
   119  		os.Exit(1)
   120  	}
   121  }