Simplify RemoveConnections

Takes the code inside the closure in the function `RemoveConnections`
and makes it a separate function to increase readability.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti
2024-09-12 14:50:53 -04:00
parent 9febd2c27a
commit c709be3a29

View File

@@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"github.com/containers/common/pkg/config" "github.com/containers/common/pkg/config"
"github.com/containers/podman/v5/pkg/machine/define"
) )
const LocalhostIP = "127.0.0.1" const LocalhostIP = "127.0.0.1"
@@ -84,24 +85,56 @@ func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error {
} }
func RemoveConnections(names ...string) error { func RemoveConnections(names ...string) error {
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error { var dest config.Destination
for _, name := range names { var service string
if _, ok := cfg.Connection.Connections[name]; ok {
delete(cfg.Connection.Connections, name)
} else {
return fmt.Errorf("unable to find connection named %q", name)
}
if cfg.Connection.Default == name { if err := config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
cfg.Connection.Default = "" err := setNewDefaultConnection(cfg, &dest, &service, names...)
} if err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// setNewDefaultConnection iterates through the available system connections and
// sets the first available connection as the new default
func setNewDefaultConnection(cfg *config.ConnectionsFile, dest *config.Destination, service *string, names ...string) error {
// delete the connection associated with the names and if that connection is
// the default, reset the default connection
for _, name := range names {
if _, ok := cfg.Connection.Connections[name]; ok {
delete(cfg.Connection.Connections, name)
} else {
return fmt.Errorf("unable to find connection named %q", name)
} }
for service := range cfg.Connection.Connections {
cfg.Connection.Default = service if cfg.Connection.Default == name {
break cfg.Connection.Default = ""
} }
}
// If there is a podman-machine-default system connection, immediately set that as the new default
if c, ok := cfg.Connection.Connections[define.DefaultMachineName]; ok {
cfg.Connection.Default = define.DefaultMachineName
*dest = c
*service = define.DefaultMachineName
return nil return nil
}) }
// set the new default system connection to the first in the map
for con, d := range cfg.Connection.Connections {
cfg.Connection.Default = con
*dest = d
*service = con
break
}
return nil
} }
// makeSSHURL creates a URL from the given input // makeSSHURL creates a URL from the given input