From 1c0c838819461655b70bea9da5cf58da2ae44354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Regulski?= Date: Mon, 25 Sep 2023 09:15:04 +0200 Subject: [PATCH] cli: allow to start colima in the foreground (#789) --- cmd/start.go | 33 +++++++++++++++++++++++++++++++-- docs/FAQ.md | 11 +++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/start.go b/cmd/start.go index 512b9779b..9a4daa9fc 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -3,10 +3,13 @@ package cmd import ( "fmt" "os" + "os/signal" "path/filepath" "strings" + "syscall" "time" + "github.com/abiosoft/colima/app" "github.com/abiosoft/colima/cli" "github.com/abiosoft/colima/cmd/root" "github.com/abiosoft/colima/config" @@ -32,6 +35,7 @@ Run 'colima template' to set the default configurations or 'colima start --edit' `, Example: " colima start\n" + " colima start --edit\n" + + " colima start --foreground\n" + " colima start --runtime containerd\n" + " colima start --kubernetes\n" + " colima start --runtime containerd --kubernetes\n" + @@ -50,7 +54,7 @@ Run 'colima template' to set the default configurations or 'colima start --edit' log.Warnln("already running, ignoring") return nil } - return app.Start(conf) + return start(app, conf) } // edit flag is specified @@ -80,7 +84,7 @@ Run 'colima template' to set the default configurations or 'colima start --edit' time.Sleep(time.Second * 3) } - return app.Start(conf) + return start(app, conf) }, PreRunE: func(cmd *cobra.Command, args []string) error { // combine args and current config file(if any) @@ -126,6 +130,7 @@ var startCmdArgs struct { Editor string ActivateRuntime bool DNSHosts []string + Foreground bool } } @@ -145,6 +150,7 @@ func init() { startCmd.Flags().IntVarP(&startCmdArgs.Memory, "memory", "m", defaultMemory, "memory in GiB") startCmd.Flags().IntVarP(&startCmdArgs.Disk, "disk", "d", defaultDisk, "disk size in GiB") startCmd.Flags().StringVarP(&startCmdArgs.Arch, "arch", "a", defaultArch, "architecture (aarch64, x86_64)") + startCmd.Flags().BoolVarP(&startCmdArgs.Flags.Foreground, "foreground", "f", false, "Keep colima in the foreground") // network if util.MacOS() { @@ -443,3 +449,26 @@ func editConfigFile() error { }() return configmanager.SaveFromFile(tmpFile) } + +func start(app app.App, conf config.Config) error { + if err := app.Start(conf); err != nil { + return err + } + if startCmdArgs.Flags.Foreground { + return awaitForInterruption(app) + } + return nil +} + +func awaitForInterruption(app app.App) error { + signalChannel := make(chan os.Signal, 1) + signal.Notify(signalChannel, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + log.Println("keeping Colima in the foreground, press ctrl+c to exit...") + sig := <-signalChannel + log.Infof("interrupted by: %v", sig) + if err := app.Stop(false); err != nil { + log.Errorf("error stopping: %v", err) + return err + } + return nil +} diff --git a/docs/FAQ.md b/docs/FAQ.md index 7ab736108..42294d084 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -3,6 +3,7 @@ - [FAQs](#faqs) - [How does Colima compare to Lima?](#how-does-colima-compare-to-lima) - [Are M1 macs supported?](#are-m1-macs-supported) + - [Does Colima support autostart](#does-colima-support-autostart) - [Can config file be used instead of cli flags?](#can-config-file-be-used-instead-of-cli-flags) - [Editing the config](#editing-the-config) - [Setting the default config](#setting-the-default-config) @@ -48,6 +49,16 @@ Colima supports and works on both Intel and M1 macs. Feedbacks would be appreciated. +## Does Colima support autostart? + +Since v0.5.6 Colima supports foreground mode. It allows to create OS dependent services. + +If Colima has been installed using brew, the easiest way to autostart Colima is to use brew services. + +```sh +brew services start colima +``` + ## Can config file be used instead of cli flags? Yes, from v0.4.0, Colima support YAML configuration file.