SetAlias assigns the configured alias to all server entries matching the target IP.
()
| 17 | |
| 18 | // SetAlias assigns the configured alias to all server entries matching the target IP. |
| 19 | func (ac *AliasController) SetAlias() bool { |
| 20 | aliasServerList := config.FindAlias(ac.alias, ac.configuration) |
| 21 | if len(aliasServerList) != 0 { |
| 22 | ac.ListAlias() |
| 23 | utils.Errorf( |
| 24 | "The alias \"%s\" has already been set, try another alias or delete it and set again.\n", |
| 25 | ac.alias) |
| 26 | return false |
| 27 | } |
| 28 | var beSetCount int |
| 29 | for index, server := range ac.configuration.ServerLists { |
| 30 | if server.IP == ac.targetIP { |
| 31 | ac.configuration.ServerLists[index].Alias = ac.alias |
| 32 | utils.Infof( |
| 33 | "The server %s@%s:%s's alias \"%s\" will be set.\n", |
| 34 | server.User, server.IP, server.Port, ac.alias) |
| 35 | beSetCount++ |
| 36 | } |
| 37 | } |
| 38 | if beSetCount == 0 { |
| 39 | utils.Warnf("No matching server for IP: %s\n", ac.targetIP) |
| 40 | return false |
| 41 | } |
| 42 | if err := config.UpdateConfig(ac.configuration); err == nil { |
| 43 | utils.Infof("%d cache information has been changed.\n", beSetCount) |
| 44 | return true |
| 45 | } |
| 46 | utils.Errorln("Main config update failed.") |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | // ListAlias prints all configured aliases, or only those matching the configured alias filter. |
| 51 | func (ac *AliasController) ListAlias() { |