github.com/omnigres/cli@v0.1.4/cmd/start.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/charmbracelet/log"
     7  	"github.com/omnigres/cli/orb"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var startCmd = &cobra.Command{
    12  	Use:   "start",
    13  	Short: "Start cluster",
    14  	Run: func(cmd *cobra.Command, args []string) {
    15  		var cluster orb.OrbCluster
    16  		var err error
    17  		cluster, err = getOrbCluster()
    18  		if err != nil {
    19  			log.Fatal(err)
    20  		}
    21  
    22  		ctx := context.Background()
    23  
    24  		readyCh := make(chan orb.OrbCluster, 1)
    25  		err = cluster.StartWithCurrentUser(ctx, orb.OrbClusterStartOptions{Runfile: true, Listeners: []orb.OrbStartEventListener{
    26  			{Ready: func(cluster orb.OrbCluster) {
    27  				readyCh <- cluster
    28  			}}}})
    29  		if err != nil {
    30  			log.Fatal(err)
    31  		}
    32  		err = cluster.Config().Save()
    33  		if err != nil {
    34  			log.Fatal(err)
    35  		}
    36  
    37  		<-readyCh
    38  
    39  		log.Info("Omnigres Orb cluster started.")
    40  
    41  		var endpoints []orb.Endpoint
    42  		endpoints, err = cluster.Endpoints(ctx)
    43  		if err != nil {
    44  			log.Fatal(err)
    45  		}
    46  
    47  		for _, endpoint := range endpoints {
    48  			fmt.Printf("%s (%s): %s\n", endpoint.Database, endpoint.Protocol, endpoint.String())
    49  		}
    50  
    51  	},
    52  }
    53  
    54  func init() {
    55  	rootCmd.AddCommand(startCmd)
    56  }