Skip to content

Commit 4456efc

Browse files
authored
Merge pull request #3144 from jandubois/fix-start-command
Fix start command: return error early when instance already exists
2 parents cce27c4 + 6642aaa commit 4456efc

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

cmd/limactl/start.go

+29-24
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
149149
if err != nil {
150150
return nil, err
151151
}
152-
if len(tmpl.Bytes) == 0 {
152+
if len(tmpl.Bytes) > 0 {
153+
if createOnly {
154+
if _, err := store.Inspect(tmpl.Name); err == nil {
155+
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
156+
}
157+
}
158+
} else {
153159
if arg == "" {
154160
if tmpl.Name == "" {
155161
tmpl.Name = DefaultInstanceName
@@ -164,6 +170,28 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
164170
if err := identifiers.Validate(tmpl.Name); err != nil {
165171
return nil, fmt.Errorf("argument must be either an instance name, a YAML file path, or a URL, got %q: %w", tmpl.Name, err)
166172
}
173+
inst, err := store.Inspect(tmpl.Name)
174+
if err == nil {
175+
if createOnly {
176+
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
177+
}
178+
logrus.Infof("Using the existing instance %q", tmpl.Name)
179+
yqExprs, err := editflags.YQExpressions(flags, false)
180+
if err != nil {
181+
return nil, err
182+
}
183+
if len(yqExprs) > 0 {
184+
yq := yqutil.Join(yqExprs)
185+
inst, err = applyYQExpressionToExistingInstance(inst, yq)
186+
if err != nil {
187+
return nil, fmt.Errorf("failed to apply yq expression %q to instance %q: %w", yq, tmpl.Name, err)
188+
}
189+
}
190+
return inst, nil
191+
}
192+
if !errors.Is(err, os.ErrNotExist) {
193+
return nil, err
194+
}
167195
if arg != "" && arg != DefaultInstanceName {
168196
logrus.Infof("Creating an instance %q from template://default (Not from template://%s)", tmpl.Name, tmpl.Name)
169197
logrus.Warnf("This form is deprecated. Use `limactl create --name=%s template://default` instead", tmpl.Name)
@@ -175,29 +203,6 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
175203
}
176204
}
177205

178-
inst, err := store.Inspect(tmpl.Name)
179-
if err == nil {
180-
if createOnly {
181-
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
182-
}
183-
logrus.Infof("Using the existing instance %q", tmpl.Name)
184-
yqExprs, err := editflags.YQExpressions(flags, false)
185-
if err != nil {
186-
return nil, err
187-
}
188-
if len(yqExprs) > 0 {
189-
yq := yqutil.Join(yqExprs)
190-
inst, err = applyYQExpressionToExistingInstance(inst, yq)
191-
if err != nil {
192-
return nil, fmt.Errorf("failed to apply yq expression %q to instance %q: %w", yq, tmpl.Name, err)
193-
}
194-
}
195-
return inst, nil
196-
}
197-
if !errors.Is(err, os.ErrNotExist) {
198-
return nil, err
199-
}
200-
201206
yqExprs, err := editflags.YQExpressions(flags, true)
202207
if err != nil {
203208
return nil, err

0 commit comments

Comments
 (0)