github.com/orteth01/up@v0.2.0/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  // Init function.
    25  var Init func() (*up.Config, *up.Project, error)
    26  
    27  func init() {
    28  	log.SetHandler(cli.Default)
    29  
    30  	Cmd.Example(`up`, "Deploy the project to the development stage.")
    31  	Cmd.Example(`up deploy production`, "Deploy the project to the production stage.")
    32  	Cmd.Example(`up url`, "Show the development endpoint url.")
    33  	Cmd.Example(`up logs -f`, "Tail project logs.")
    34  	Cmd.Example(`up logs 'error or fatal'`, "Show error or fatal level logs.")
    35  	Cmd.Example(`up run build`, "Run build command manually.")
    36  	Cmd.Example(`up help logs`, "Show help and examples for a sub-command.")
    37  
    38  	region := Cmd.Flag("region", "Override the region.").Short('r').String()
    39  	workdir := Cmd.Flag("chdir", "Change working directory.").Default(".").Short('C').String()
    40  	verbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
    41  
    42  	Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
    43  		os.Chdir(*workdir)
    44  
    45  		if *verbose {
    46  			log.SetHandler(delta.Default)
    47  			log.SetLevel(log.DebugLevel)
    48  		}
    49  
    50  		Init = func() (*up.Config, *up.Project, error) {
    51  			c, err := up.ReadConfig("up.json")
    52  			if err != nil {
    53  				return nil, nil, errors.Wrap(err, "reading config")
    54  			}
    55  
    56  			if *region != "" {
    57  				c.Regions = []string{*region}
    58  			}
    59  
    60  			events := make(event.Events)
    61  			p := up.New(c, events).WithPlatform(lambda.New(c, events))
    62  
    63  			if *verbose {
    64  				go reporter.Discard(events)
    65  			} else {
    66  				go reporter.Text(events)
    67  			}
    68  
    69  			return c, p, nil
    70  		}
    71  
    72  		return nil
    73  	})
    74  }