Skip to content

Commit

Permalink
Merge pull request #3144 from jandubois/fix-start-command
Browse files Browse the repository at this point in the history
Fix start command: return error early when instance already exists
  • Loading branch information
jandubois authored Jan 23, 2025
2 parents cce27c4 + 6642aaa commit 4456efc
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
if err != nil {
return nil, err
}
if len(tmpl.Bytes) == 0 {
if len(tmpl.Bytes) > 0 {
if createOnly {
if _, err := store.Inspect(tmpl.Name); err == nil {
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
}
}
} else {
if arg == "" {
if tmpl.Name == "" {
tmpl.Name = DefaultInstanceName
Expand All @@ -164,6 +170,28 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
if err := identifiers.Validate(tmpl.Name); err != nil {
return nil, fmt.Errorf("argument must be either an instance name, a YAML file path, or a URL, got %q: %w", tmpl.Name, err)
}
inst, err := store.Inspect(tmpl.Name)
if err == nil {
if createOnly {
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
}
logrus.Infof("Using the existing instance %q", tmpl.Name)
yqExprs, err := editflags.YQExpressions(flags, false)
if err != nil {
return nil, err
}
if len(yqExprs) > 0 {
yq := yqutil.Join(yqExprs)
inst, err = applyYQExpressionToExistingInstance(inst, yq)
if err != nil {
return nil, fmt.Errorf("failed to apply yq expression %q to instance %q: %w", yq, tmpl.Name, err)
}
}
return inst, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if arg != "" && arg != DefaultInstanceName {
logrus.Infof("Creating an instance %q from template://default (Not from template://%s)", tmpl.Name, tmpl.Name)
logrus.Warnf("This form is deprecated. Use `limactl create --name=%s template://default` instead", tmpl.Name)
Expand All @@ -175,29 +203,6 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
}
}

inst, err := store.Inspect(tmpl.Name)
if err == nil {
if createOnly {
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
}
logrus.Infof("Using the existing instance %q", tmpl.Name)
yqExprs, err := editflags.YQExpressions(flags, false)
if err != nil {
return nil, err
}
if len(yqExprs) > 0 {
yq := yqutil.Join(yqExprs)
inst, err = applyYQExpressionToExistingInstance(inst, yq)
if err != nil {
return nil, fmt.Errorf("failed to apply yq expression %q to instance %q: %w", yq, tmpl.Name, err)
}
}
return inst, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}

yqExprs, err := editflags.YQExpressions(flags, true)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4456efc

Please sign in to comment.