chore: handle edge cases for restart command (#605)

* chore: misc refactor for restart command

* cli: restart: handle error for uncreated instances
This commit is contained in:
Abiola Ibrahim
2023-02-02 06:43:28 +01:00
committed by GitHub
parent 7aba815354
commit e6edc4d0cb
5 changed files with 32 additions and 8 deletions
+13 -3
View File
@@ -1,8 +1,11 @@
package cmd
import (
"time"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config/configmanager"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/spf13/cobra"
)
@@ -20,13 +23,20 @@ The state of the VM is persisted at stop. A start afterwards
should return it back to its previous state.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// validate if the instance was previously created
if _, err := limautil.Instance(); err != nil {
return err
}
app := newApp()
stopErr := app.Stop(restartCmdArgs.force)
if stopErr != nil {
return stopErr
if err := app.Stop(restartCmdArgs.force); err != nil {
return err
}
// delay a bit before starting
time.Sleep(time.Second * 3)
config, err := configmanager.Load()
if err != nil {
return err
+10 -2
View File
@@ -20,9 +20,17 @@ var rootCmd = &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
switch cmd.Name() {
// special case handling for commands directly interacting with the VM
// start, stop, delete, status, version, ssh-config
case "start", "stop", "delete", "status", "version", "ssh-config":
// start, stop, restart, delete, status, version, ssh-config
case "start",
"stop",
"restart",
"delete",
"status",
"version",
"ssh-config":
// if an arg is passed, assume it to be the profile (provided --profile is unset)
// i.e. colima start docker == colima start --profile=docker
if len(args) > 0 && !cmd.Flag("profile").Changed {
+1 -1
View File
@@ -77,7 +77,7 @@ Run 'colima template' to set the default configurations or 'colima start --edit'
return fmt.Errorf("error stopping :%w", err)
}
// pause before startup to prevent race condition
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
}
return app.Start(conf)
+1 -1
View File
@@ -92,7 +92,7 @@ func launchEditor(editor string, file string) error {
return fmt.Errorf("no editor found in $PATH, kindly set $EDITOR environment variable and try again")
}
// vscode needs the wait flag, add it if the user did not.
// some editors need the wait flag, let us add it if the user has not.
switch editor {
case "code", "code-insiders", "code-oss", "codium", "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code":
editor = strconv.Quote(editor) + " --wait --new-window"
+7 -1
View File
@@ -73,8 +73,10 @@ type InstanceInfo struct {
Runtime string `json:"runtime,omitempty"`
}
// Running checks if the instance is running.
func (i InstanceInfo) Running() bool { return i.Status == limaStatusRunning }
// Config returns the current Colima config
func (i InstanceInfo) Config() (config.Config, error) {
return configmanager.LoadFrom(ColimaStateFile(i.Name))
}
@@ -200,10 +202,14 @@ func getInstance(profileID string) (InstanceInfo, error) {
if err := cmd.Run(); err != nil {
return i, fmt.Errorf("error retrieving instance: %w", err)
}
if buf.Len() == 0 {
return i, fmt.Errorf("instance '%s' does not exist", config.Profile(profileID).DisplayName)
}
if err := json.Unmarshal(buf.Bytes(), &i); err != nil {
return i, fmt.Errorf("error retrieving instance: %w", err)
}
return i, nil
}