github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/cmd/cli/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"os/signal"
     7  
     8  	"github.com/defang-io/defang/src/cmd/cli/command"
     9  	"github.com/defang-io/defang/src/pkg/term"
    10  )
    11  
    12  func main() {
    13  	// Handle Ctrl+C so we can exit gracefully
    14  	ctx, cancel := context.WithCancel(context.Background())
    15  	sigs := make(chan os.Signal, 1)
    16  	signal.Notify(sigs, os.Interrupt)
    17  
    18  	go func() {
    19  		<-sigs
    20  		signal.Stop(sigs)
    21  		term.Debug("Received interrupt signal; cancelling...")
    22  		command.Track("User Interrupted")
    23  		cancel()
    24  	}()
    25  
    26  	command.SetupCommands(version)
    27  	err := command.Execute(ctx)
    28  	command.FlushAllTracking() // TODO: track errors/panics
    29  
    30  	if err != nil {
    31  		// If the error is a command.ExitCode, use its value as the exit code
    32  		ec, ok := err.(command.ExitCode)
    33  		if !ok {
    34  			ec = 1 // should not happen since we always return ExitCode
    35  		}
    36  		os.Exit(int(ec))
    37  	}
    38  }