Skip to content

Commit

Permalink
cli: allow to start colima in the foreground (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
regulskimichal authored Sep 25, 2023
1 parent d946b57 commit 1c0c838
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
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

0 comments on commit 1c0c838

Please sign in to comment.