findMaxCommonVersion finds the max common version between protocol versions supported by a plugin and those supported by the CLI. If all plugin versions are lower than min CLI supported version, it returns -1. If all plugin versions are higher than max CLI supported version, it returns -2. In this w
(pluginSupported []int, cliSupported []int)
| 109 | // if -1, the source needs to be updated or the CLI downgraded; |
| 110 | // if -2, the CLI needs to be updated or the source downgraded. |
| 111 | func findMaxCommonVersion(pluginSupported []int, cliSupported []int) int { |
| 112 | if len(pluginSupported) == 0 { |
| 113 | return -1 |
| 114 | } |
| 115 | |
| 116 | minCLISupported, maxCLISupported := math.MaxInt32, -1 |
| 117 | for _, v := range cliSupported { |
| 118 | if v < minCLISupported { |
| 119 | minCLISupported = v |
| 120 | } |
| 121 | if v > maxCLISupported { |
| 122 | maxCLISupported = v |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | minVersion := math.MaxInt32 |
| 127 | maxCommon := -1 |
| 128 | for _, v := range pluginSupported { |
| 129 | if v < minVersion { |
| 130 | minVersion = v |
| 131 | } |
| 132 | if v > maxCommon && slices.Contains(cliSupported, v) { |
| 133 | maxCommon = v |
| 134 | } |
| 135 | } |
| 136 | if maxCommon == -1 && minVersion > maxCLISupported { |
| 137 | return -2 |
| 138 | } |
| 139 | return maxCommon |
| 140 | } |
| 141 | |
| 142 | func parseShard(cmd *cobra.Command) (*shard, error) { |
| 143 | shardFlag, err := cmd.Flags().GetString("shard") |
no outgoing calls