Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: allow to start colima in the foreground #789

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" +
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -126,6 +130,7 @@ var startCmdArgs struct {
Editor string
ActivateRuntime bool
DNSHosts []string
Foreground bool
}
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down