66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"isle/bootstrap"
|
|
"isle/daemon/network"
|
|
)
|
|
|
|
func pickNetwork(
|
|
ctx context.Context,
|
|
networkLoader network.Loader,
|
|
networks map[string]joinedNetwork,
|
|
) (
|
|
joinedNetwork, error,
|
|
) {
|
|
if len(networks) == 0 {
|
|
return joinedNetwork{}, ErrNoNetwork
|
|
}
|
|
|
|
creationParams, err := networkLoader.Loadable(ctx)
|
|
if err != nil {
|
|
return joinedNetwork{}, fmt.Errorf("getting loadable networks: %w", err)
|
|
}
|
|
|
|
var (
|
|
networkSearchStr = getNetworkSearchStr(ctx)
|
|
matchingNetworkIDs = make([]string, 0, len(networks))
|
|
)
|
|
|
|
for _, creationParam := range creationParams {
|
|
if networkSearchStr == "" || creationParam.Matches(networkSearchStr) {
|
|
matchingNetworkIDs = append(matchingNetworkIDs, creationParam.ID)
|
|
}
|
|
}
|
|
|
|
if len(matchingNetworkIDs) == 0 {
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
|
} else if len(matchingNetworkIDs) > 1 {
|
|
return joinedNetwork{}, ErrMultipleMatchingNetworks
|
|
}
|
|
|
|
return networks[matchingNetworkIDs[0]], nil
|
|
}
|
|
|
|
func alreadyJoined(
|
|
ctx context.Context,
|
|
networks map[string]joinedNetwork,
|
|
creationParams bootstrap.CreationParams,
|
|
) (
|
|
bool, error,
|
|
) {
|
|
for networkID, network := range networks {
|
|
existingCreationParams, err := network.GetNetworkCreationParams(ctx)
|
|
if err != nil {
|
|
return false, fmt.Errorf(
|
|
"getting creation params of network %q: %w", networkID, err,
|
|
)
|
|
} else if existingCreationParams.Conflicts(creationParams) {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|