github.com/hhsnopek/up@v0.1.1/internal/cli/root/root.go (about)

     1  package root
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/apex/log"
     7  	"github.com/apex/log/handlers/cli"
     8  	"github.com/apex/log/handlers/delta"
     9  	"github.com/pkg/errors"
    10  	"github.com/tj/kingpin"
    11  
    12  	"github.com/apex/up"
    13  	"github.com/apex/up/platform/event"
    14  	"github.com/apex/up/platform/lambda"
    15  	"github.com/apex/up/reporter"
    16  )
    17  
    18  // Cmd is the root command.
    19  var Cmd = kingpin.New("up", "")
    20  
    21  // Command registers a command.
    22  var Command = Cmd.Command
    23  
    24  // Project is the project, populated
    25  // before running any commands.
    26  var Project *up.Project
    27  
    28  // Config is the project config, populate
    29  // before running any commands.
    30  var Config *up.Config
    31  
    32  func init() {
    33  	log.SetHandler(cli.Default)
    34  
    35  	Cmd.Example(`up`, "Deploy the project to the development stage.")
    36  	Cmd.Example(`up deploy production`, "Deploy the project to the production stage.")
    37  	Cmd.Example(`up url`, "Show the development endpoint url.")
    38  	Cmd.Example(`up logs -f`, "Tail project logs.")
    39  	Cmd.Example(`up logs 'error or fatal'`, "Show error or fatal level logs.")
    40  	Cmd.Example(`up run build`, "Run build command manually.")
    41  	Cmd.Example(`up help logs`, "Show help and examples for a sub-command.")
    42  
    43  	region := Cmd.Flag("region", "Override the region.").Short('r').String()
    44  	workdir := Cmd.Flag("chdir", "Change working directory.").Default(".").Short('C').String()
    45  	verbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
    46  
    47  	Cmd.PreAction(func(_ *kingpin.ParseContext) error {
    48  		os.Chdir(*workdir)
    49  
    50  		if *verbose {
    51  			log.SetHandler(delta.Default)
    52  			log.SetLevel(log.DebugLevel)
    53  		}
    54  
    55  		c, err := up.ReadConfig("up.json")
    56  		if err != nil {
    57  			return errors.Wrap(err, "reading config")
    58  		}
    59  
    60  		if *region != "" {
    61  			c.Regions = []string{*region}
    62  		}
    63  
    64  		events := make(event.Events)
    65  		Config = c
    66  		Project = up.New(c, events).WithPlatform(lambda.New(c, events))
    67  
    68  		if *verbose {
    69  			go reporter.Discard(events)
    70  		} else {
    71  			go reporter.Text(events)
    72  		}
    73  
    74  		return nil
    75  	})
    76  }