Skip to content

Commit

Permalink
fix inconsistency error in limactl create command intance name exists
Browse files Browse the repository at this point in the history
Signed-off-by: olalekan odukoya <[email protected]>
  • Loading branch information
olamilekan000 committed Jan 16, 2025
1 parent c08b326 commit 0b3c6cb
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
if err != nil {
return nil, err
}

if len(tmpl.Bytes) == 0 {
if arg == "" {
if tmpl.Name == "" {
Expand All @@ -161,42 +162,45 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
}
tmpl.Name = arg
}

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)
}

// Read the default template for creating a new instance
tmpl.Bytes, err = templatestore.Read(templatestore.Default)
if err != nil {
return nil, 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
}

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

0 comments on commit 0b3c6cb

Please sign in to comment.