mirror of
https://github.com/containers/podman.git
synced 2025-11-29 17:48:05 +08:00
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:
@@ -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,7 +85,28 @@ 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
|
||||||
|
var service string
|
||||||
|
|
||||||
|
if err := config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
||||||
|
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 {
|
for _, name := range names {
|
||||||
if _, ok := cfg.Connection.Connections[name]; ok {
|
if _, ok := cfg.Connection.Connections[name]; ok {
|
||||||
delete(cfg.Connection.Connections, name)
|
delete(cfg.Connection.Connections, name)
|
||||||
@@ -96,12 +118,23 @@ func RemoveConnections(names ...string) error {
|
|||||||
cfg.Connection.Default = ""
|
cfg.Connection.Default = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for service := range cfg.Connection.Connections {
|
|
||||||
cfg.Connection.Default = service
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
break
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeSSHURL creates a URL from the given input
|
// makeSSHURL creates a URL from the given input
|
||||||
|
|||||||
Reference in New Issue
Block a user