Skip to content

Commit

Permalink
Merge pull request #3120 from olamilekan000/fix-inconsistency-in-lima…
Browse files Browse the repository at this point in the history
…cttl-create-command-if-name-already-exists

fix inconsistent error in limactl create command when instance name and template is specified
  • Loading branch information
jandubois authored Jan 19, 2025
2 parents 597ffed + d6a6d7f commit 9b8d2a7
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,6 @@ 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 @@ -197,6 +175,29 @@ 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 9b8d2a7

Please sign in to comment.