Fix duplicated connection name when update connection profile

This commit is contained in:
tiny-craft 2023-07-02 20:11:53 +08:00
parent f6a4ab8de0
commit d573cab0c0
1 changed files with 35 additions and 23 deletions

View File

@ -174,38 +174,41 @@ func (c *ConnectionsStorage) UpdateConnection(name string, param types.Connectio
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
var updated bool
conns := c.getConnections() conns := c.getConnections()
var updated bool
var retrieve func(types.Connections, string, types.ConnectionConfig) error
retrieve = func(conns types.Connections, name string, param types.ConnectionConfig) error {
for i, conn := range conns { for i, conn := range conns {
if conn.Name == name { if conn.Type != "group" {
if name != param.Name && conn.Name == param.Name {
return errors.New("duplicated connection name")
} else if conn.Name == name && !updated {
conns[i] = types.Connection{ conns[i] = types.Connection{
ConnectionConfig: param, ConnectionConfig: param,
} }
updated = true updated = true
} else if conn.Type == "group" {
for j, conn2 := range conn.Connections {
if conn2.Name == name {
conns[i].Connections[j] = types.Connection{
ConnectionConfig: param,
} }
updated = true } else {
break err := retrieve(conn.Connections, name, param)
if err != nil {
return err
} }
} }
} }
return nil
}
if updated { err := retrieve(conns, name, param)
break if err != nil {
return err
} }
} if !updated {
if updated {
return c.saveConnections(conns)
}
return errors.New("connection not found") return errors.New("connection not found")
} }
return c.saveConnections(conns)
}
// RemoveConnection remove special connection // RemoveConnection remove special connection
func (c *ConnectionsStorage) RemoveConnection(name string) error { func (c *ConnectionsStorage) RemoveConnection(name string) error {
c.mutex.Lock() c.mutex.Lock()
@ -263,17 +266,26 @@ func (c *ConnectionsStorage) RenameGroup(name, newName string) error {
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
groupIndex := -1
conns := c.getConnections() conns := c.getConnections()
for i, conn := range conns { for i, conn := range conns {
if conn.Type == "group" && conn.Name == name { if conn.Type == "group" {
conns[i].Name = newName if conn.Name == newName {
return c.saveConnections(conns) return errors.New("duplicated group name")
} else if conn.Name == name {
groupIndex = i
}
} }
} }
if groupIndex == -1 {
return errors.New("group not found") return errors.New("group not found")
} }
conns[groupIndex].Name = newName
return c.saveConnections(conns)
}
// RemoveGroup remove special group, include all connections under it // RemoveGroup remove special group, include all connections under it
func (c *ConnectionsStorage) RemoveGroup(group string, includeConnection bool) error { func (c *ConnectionsStorage) RemoveGroup(group string, includeConnection bool) error {
c.mutex.Lock() c.mutex.Lock()