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

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/charmbracelet/lipgloss"
     7  	"github.com/charmbracelet/lipgloss/table"
     8  	"github.com/charmbracelet/log"
     9  	"github.com/docker/docker/pkg/stdcopy"
    10  	"github.com/omnigres/cli/orb"
    11  	"github.com/omnigres/cli/src"
    12  	"github.com/spf13/cobra"
    13  	"io"
    14  	"os"
    15  	"path/filepath"
    16  )
    17  
    18  var runCmd = &cobra.Command{
    19  	Use:   "run [orb]",
    20  	Short: "Run a one-off cluster",
    21  	Long: `Run an Omnigres cluster in foreground.
    22  
    23      It's going to operate until it shut down. No run file will be created.
    24     `,
    25  	Run: func(cmd *cobra.Command, args []string) {
    26  		var err error
    27  		var orbs []string
    28  		if len(args) == 0 {
    29  			var path string
    30  			path, err = getOrbPath(false)
    31  
    32  			orbs = []string{filepath.Base(path)}
    33  		}
    34  
    35  		if len(args) == 1 {
    36  			inputPath := args[0]
    37  			srcdir, err := src.GetSourceDirectory(inputPath)
    38  			if err != nil {
    39  				log.Fatal(err)
    40  			}
    41  			defer srcdir.Close()
    42  
    43  			if src.IsGitHubGistURL(inputPath) {
    44  				orbs = []string{"gist"}
    45  			} else {
    46  				workspace = srcdir.Path()
    47  				var path string
    48  				path, err = getOrbPath(false)
    49  
    50  				orbs = []string{filepath.Base(path)}
    51  			}
    52  
    53  		}
    54  
    55  		var cluster orb.OrbCluster
    56  		cluster, err = getOrbCluster()
    57  
    58  		if err != nil {
    59  			log.Fatal(err)
    60  		}
    61  
    62  		cluster.Config().Image.Name = runImage
    63  
    64  		ctx := context.Background()
    65  
    66  		options := orb.OrbClusterStartOptions{
    67  			Runfile:    false,
    68  			AutoRemove: true,
    69  			Listeners: []orb.OrbStartEventListener{{
    70  				Ready: func(cluster orb.OrbCluster) {
    71  
    72  					err := assembleOrbs(
    73  						ctx,
    74  						cluster,
    75  						true,
    76  						orbs,
    77  						func(orbName string) string { return orbName },
    78  					)
    79  
    80  					if err != nil {
    81  						log.Fatal(err)
    82  					}
    83  
    84  					if err != nil {
    85  						log.Fatal(err)
    86  					}
    87  
    88  					var endpoints []orb.Endpoint
    89  					endpoints, err = cluster.Endpoints(ctx)
    90  					if err != nil {
    91  						log.Fatal(err)
    92  					}
    93  
    94  					rows := make([][]string, 0)
    95  
    96  					for _, endpoint := range endpoints {
    97  						rows = append(rows, []string{endpoint.Database, endpoint.Protocol, endpoint.String()})
    98  					}
    99  
   100  					t := table.New().
   101  						Border(lipgloss.RoundedBorder()).
   102  						BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
   103  						BorderColumn(false).
   104  						Width(80).
   105  						Headers("Orb", "Protocol", "URL").
   106  						Rows(rows...)
   107  
   108  					fmt.Println(t)
   109  
   110  				},
   111  			}},
   112  		}
   113  		options.Attachment.ShouldAttach = true
   114  		options.Attachment.Listeners =
   115  			[]orb.OrbRunEventListener{
   116  				{
   117  					OutputHandler: func(cluster orb.OrbCluster, reader io.Reader) {
   118  						go func() { _, _ = stdcopy.StdCopy(os.Stdout, os.Stderr, reader) }()
   119  					},
   120  				},
   121  			}
   122  		err = cluster.StartWithCurrentUser(ctx, options)
   123  
   124  		if err != nil {
   125  			log.Fatal(err)
   126  		}
   127  	},
   128  }
   129  
   130  var runImage string
   131  
   132  func init() {
   133  	rootCmd.AddCommand(runCmd)
   134  	runCmd.Flags().StringVarP(&runImage, "image", "i", orb.NewConfig().Image.Name, "The Omnigres image to use")
   135  }