Skip to content

Commit

Permalink
Moving logic to of instead of function
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjanr committed Jun 1, 2023
1 parent 6ac1f0a commit 0f2db47
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pkg/cluster/internal/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ func Cluster(logger log.Logger, p providers.Provider, opts *ClusterOptions) erro
}

// Check if the cluster name already exists
if err := alreadyExists(p, opts.Config.Name, opts.ForceDelete); err != nil {
return err
if err := alreadyExists(p, opts.Config.Name); err != nil {
if opts.ForceDelete {
// Delete current cluster container
_ = delete.Cluster(nil, p, opts.Config.Name, "")
} else {
return errors.Errorf("A cluster with the name %q already exists \n"+
"Please use a different cluster name or delete the current container with --force flag", opts.Config.Name)
}
}

// warn if cluster name might typically be too long
Expand Down Expand Up @@ -195,19 +201,14 @@ func Cluster(logger log.Logger, p providers.Provider, opts *ClusterOptions) erro
}

// alreadyExists returns an error if the cluster name already exists
func alreadyExists(p providers.Provider, name string, forceDelete bool) error {
// or if we had an error checking
func alreadyExists(p providers.Provider, name string) error {
n, err := p.ListNodes(name)
if err != nil {
return err
}
if len(n) > 0 {
if forceDelete {
// Delete current cluster container
_ = delete.Cluster(nil, p, name, "")
} else {
return errors.Errorf("node(s) already exist for a cluster with the name %q \n"+
"Please use a different cluster name or delete the current container with --force flag", name)
}
if len(n) != 0 {
return errors.Errorf("node(s) already exist for a cluster with the name %q", name)
}
return nil
}
Expand Down

0 comments on commit 0f2db47

Please sign in to comment.