@@ -149,7 +149,13 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
149
149
if err != nil {
150
150
return nil , err
151
151
}
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 {
153
159
if arg == "" {
154
160
if tmpl .Name == "" {
155
161
tmpl .Name = DefaultInstanceName
@@ -164,6 +170,28 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
164
170
if err := identifiers .Validate (tmpl .Name ); err != nil {
165
171
return nil , fmt .Errorf ("argument must be either an instance name, a YAML file path, or a URL, got %q: %w" , tmpl .Name , err )
166
172
}
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
+ }
167
195
if arg != "" && arg != DefaultInstanceName {
168
196
logrus .Infof ("Creating an instance %q from template://default (Not from template://%s)" , tmpl .Name , tmpl .Name )
169
197
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) (*
175
203
}
176
204
}
177
205
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
-
201
206
yqExprs , err := editflags .YQExpressions (flags , true )
202
207
if err != nil {
203
208
return nil , err
0 commit comments